131 lines
3.4 KiB
C#
131 lines
3.4 KiB
C#
using MongoDB.Driver;
|
|
|
|
using TextSender_API.Models;
|
|
|
|
namespace TextSender_API.Repositories;
|
|
|
|
|
|
public class ContactCreationQueueRepository : BaseRepository
|
|
{
|
|
#region Fields
|
|
private readonly IMongoCollection<ContactCreationQueue> _bookCollection;
|
|
#endregion
|
|
|
|
|
|
#region Constructors
|
|
public ContactCreationQueueRepository(string connectionString)
|
|
{
|
|
this._connectionString = connectionString;
|
|
this._tableName = "contactCreationQueue";
|
|
var client = this.InitializeClient();
|
|
this._bookCollection = client.GetDatabase(this._databaseName).GetCollection<ContactCreationQueue>(this._tableName);
|
|
}
|
|
#endregion
|
|
|
|
#region Metods
|
|
public Tuple<bool, ContactCreationQueue> Exists(string id)
|
|
{
|
|
var allQueue = this.RetrieveAll();
|
|
var result = allQueue.FirstOrDefault(q => q.Id!.Equals(id));
|
|
|
|
if (result != null)
|
|
{
|
|
return new Tuple<bool, ContactCreationQueue>(true, result!);
|
|
}
|
|
else
|
|
{
|
|
return new Tuple<bool, ContactCreationQueue>(false, new ContactCreationQueue());
|
|
}
|
|
}
|
|
|
|
public void Create(ContactCreationQueue queue)
|
|
{
|
|
this._bookCollection.InsertOne(queue);
|
|
}
|
|
|
|
public List<ContactCreationQueue> RetrieveAll()
|
|
{
|
|
return this._bookCollection.Find(_ => true).ToList();
|
|
}
|
|
|
|
public void Update(ContactCreationQueue queue)
|
|
{
|
|
var filter = Builders<ContactCreationQueue>.Filter.Eq(q => q.Id, queue.Id);
|
|
this._bookCollection.ReplaceOne(filter, queue);
|
|
}
|
|
|
|
public void Delete(ContactCreationQueue queue)
|
|
{
|
|
var filter = Builders<ContactCreationQueue>.Filter.Eq(q => q.Id, queue.Id);
|
|
this._bookCollection.DeleteOne(filter);
|
|
}
|
|
|
|
private MongoClient InitializeClient()
|
|
{
|
|
return new MongoClient(this._connectionString);
|
|
}
|
|
#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
|
|
} |