53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using weddingapi.Models;
|
|
|
|
namespace weddingapi.Repositories
|
|
{
|
|
public class MongoDBFamilyRepository : IFamilyRepository
|
|
{
|
|
private const string databaseName = "wedding";
|
|
private const string collectionName = "families";
|
|
private readonly IMongoCollection<Family> familyCollection;
|
|
private readonly FilterDefinitionBuilder<Family> filterBuilder = Builders<Family>.Filter;
|
|
|
|
public MongoDBFamilyRepository(IMongoClient mongoClient)
|
|
{
|
|
IMongoDatabase database = mongoClient.GetDatabase(databaseName);
|
|
familyCollection = database.GetCollection<Family>(collectionName);
|
|
}
|
|
|
|
public async Task CreateFamilyAsync(Family family)
|
|
{
|
|
await familyCollection.InsertOneAsync(family);
|
|
}
|
|
|
|
public async Task DeleteFamilyAsync(Guid id)
|
|
{
|
|
var filter = filterBuilder.Eq(family => family.Id, id);
|
|
await familyCollection.DeleteOneAsync(filter);
|
|
}
|
|
|
|
public async Task<IEnumerable<Family>> GetFamiliesAsync()
|
|
{
|
|
var res = await familyCollection.FindAsync(new BsonDocument());
|
|
return await res.ToListAsync();
|
|
}
|
|
|
|
public async Task<Family> GetFamilyAsync(Guid id)
|
|
{
|
|
var filter = filterBuilder.Eq(family => family.Id, id);
|
|
var res = await familyCollection.FindAsync(filter);
|
|
return await res.SingleOrDefaultAsync();
|
|
}
|
|
|
|
public async Task UpdateFamilyAsync(Family family)
|
|
{
|
|
var filter = filterBuilder.Eq(exisitingFamily => exisitingFamily.Id, family.Id);
|
|
await familyCollection.ReplaceOneAsync(filter, family);
|
|
}
|
|
}
|
|
} |