Refactoring and adding new endpoint
This commit is contained in:
@@ -5,7 +5,7 @@ using TextSender_API.Models;
|
||||
namespace TextSender_API.Repositories;
|
||||
|
||||
|
||||
public class ContactCreationQueueepository : BaseRepository
|
||||
public class ContactCreationQueueRepository : BaseRepository
|
||||
{
|
||||
#region Fields
|
||||
private readonly IMongoCollection<ContactCreationQueue> _bookCollection;
|
||||
@@ -13,7 +13,7 @@ public class ContactCreationQueueepository : BaseRepository
|
||||
|
||||
|
||||
#region Constructors
|
||||
public ContactCreationQueueepository(string connectionString)
|
||||
public ContactCreationQueueRepository(string connectionString)
|
||||
{
|
||||
this._connectionString = connectionString;
|
||||
this._tableName = "contactCreationQueue";
|
||||
@@ -66,3 +66,66 @@ public class ContactCreationQueueepository : BaseRepository
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
public class ContactsRepository : BaseRepository
|
||||
{
|
||||
#region Fields
|
||||
private readonly IMongoCollection<Contact> _bookCollection;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
public ContactsRepository(string connectionString)
|
||||
{
|
||||
this._connectionString = connectionString;
|
||||
this._tableName = "contacts";
|
||||
var client = this.InitializeClient();
|
||||
this._bookCollection = client.GetDatabase(this._databaseName).GetCollection<Contact>(this._tableName);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Metods
|
||||
public Tuple<bool, Contact> Exists(string id)
|
||||
{
|
||||
var allContacts= this.RetrieveAll();
|
||||
var result = allContacts.FirstOrDefault(c => c.Id!.Equals(id));
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
return new Tuple<bool, Contact>(true, result!);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Tuple<bool, Contact>(false, new Contact());
|
||||
}
|
||||
}
|
||||
|
||||
public void Create(Contact contact)
|
||||
{
|
||||
this._bookCollection.InsertOne(contact);
|
||||
}
|
||||
|
||||
public List<Contact> RetrieveAll()
|
||||
{
|
||||
return this._bookCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public void Update(Contact contact)
|
||||
{
|
||||
var filter = Builders<Contact>.Filter.Eq(c => c.Id, contact.Id);
|
||||
this._bookCollection.ReplaceOne(filter, contact);
|
||||
}
|
||||
|
||||
public void Delete(Contact contact)
|
||||
{
|
||||
var filter = Builders<Contact>.Filter.Eq(c => c.Id, contact.Id);
|
||||
this._bookCollection.DeleteOne(filter);
|
||||
}
|
||||
|
||||
private MongoClient InitializeClient()
|
||||
{
|
||||
return new MongoClient(this._connectionString);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user