Add contact controller
This commit is contained in:
53
Repositories/MongoDBContactUsRepository.cs
Normal file
53
Repositories/MongoDBContactUsRepository.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 MongoDBContactUsRepository : IContactRepository
|
||||
{
|
||||
private const string databaseName = "wedding";
|
||||
private const string collectionName = "contacts";
|
||||
private readonly IMongoCollection<Contact> familyCollection;
|
||||
private readonly FilterDefinitionBuilder<Contact> filterBuilder = Builders<Contact>.Filter;
|
||||
|
||||
public MongoDBContactUsRepository(IMongoClient mongoClient)
|
||||
{
|
||||
IMongoDatabase database = mongoClient.GetDatabase(databaseName);
|
||||
familyCollection = database.GetCollection<Contact>(collectionName);
|
||||
}
|
||||
|
||||
public async Task CreateContactAsync(Contact contact)
|
||||
{
|
||||
await familyCollection.InsertOneAsync(contact);
|
||||
}
|
||||
|
||||
public async Task DeleteContactAsync(Guid id)
|
||||
{
|
||||
var filter = filterBuilder.Eq(contact => contact.Id, id);
|
||||
await familyCollection.DeleteOneAsync(filter);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Contact>> GetContactsAsync()
|
||||
{
|
||||
var res = await familyCollection.FindAsync(new BsonDocument());
|
||||
return await res.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Contact> GetContactAsync(Guid id)
|
||||
{
|
||||
var filter = filterBuilder.Eq(contact => contact.Id, id);
|
||||
var res = await familyCollection.FindAsync(filter);
|
||||
return await res.SingleOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateContactAsync(Contact contact)
|
||||
{
|
||||
var filter = filterBuilder.Eq(exisitingContact => exisitingContact.Id, contact.Id);
|
||||
await familyCollection.ReplaceOneAsync(filter, contact);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user