initial commit
This commit is contained in:
20
Repositories/IFamilyRepository.cs
Normal file
20
Repositories/IFamilyRepository.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using weddingapi.Models;
|
||||
|
||||
namespace weddingapi.Repositories
|
||||
{
|
||||
public interface IFamilyRepository
|
||||
{
|
||||
Task<Family> GetFamilyAsync(Guid id);
|
||||
|
||||
Task<IEnumerable<Family>> GetFamiliesAsync();
|
||||
|
||||
Task CreateFamilyAsync(Family family);
|
||||
|
||||
Task UpdateFamilyAsync(Family family);
|
||||
|
||||
Task DeleteFamilyAsync(Guid id);
|
||||
}
|
||||
}
|
||||
44
Repositories/InMemFamilyRepository.cs
Normal file
44
Repositories/InMemFamilyRepository.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
// using System;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Linq;
|
||||
// using weddingapi.Models;
|
||||
|
||||
// namespace weddingapi.Repositories
|
||||
// {
|
||||
// public class InMemFamilyRepository : IFamilyRepository
|
||||
// {
|
||||
// private readonly List<Family> families = new()
|
||||
// {
|
||||
// new Family { Id = Guid.NewGuid(), Name = "Lol", CreatedDate = DateTimeOffset.UtcNow },
|
||||
// new Family { Id = Guid.NewGuid(), Name = "Lol1", CreatedDate = DateTimeOffset.UtcNow },
|
||||
// new Family { Id = Guid.NewGuid(), Name = "Lol2", CreatedDate = DateTimeOffset.UtcNow }
|
||||
// };
|
||||
|
||||
// public IEnumerable<Family> GetFamiliesAsync()
|
||||
// {
|
||||
// return families;
|
||||
// }
|
||||
|
||||
// public Family GetFamilyAsync(Guid id)
|
||||
// {
|
||||
// return families.Where(family => family.Id == id).SingleOrDefault();
|
||||
// }
|
||||
|
||||
// public void CreateFamilyAsync(Family family)
|
||||
// {
|
||||
// this.families.Add(family);
|
||||
// }
|
||||
|
||||
// public void UpdateFamilyAsync(Family family)
|
||||
// {
|
||||
// var index = families.FindIndex(existingFamily => existingFamily.Id == family.Id);
|
||||
// families[index] = family;
|
||||
// }
|
||||
|
||||
// public void DeleteFamilyAsync(Guid id)
|
||||
// {
|
||||
// var index = families.FindIndex(existingFamily => existingFamily.Id == id);
|
||||
// families.RemoveAt(index);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
53
Repositories/MongoDBFamilyRepository.cs
Normal file
53
Repositories/MongoDBFamilyRepository.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user