79 lines
2.8 KiB
C#
79 lines
2.8 KiB
C#
|
using BeautySaloonContracts.BindingModels;
|
|||
|
using BeautySaloonContracts.SearchModels;
|
|||
|
using BeautySaloonContracts.StoragesContracts;
|
|||
|
using BeautySaloonContracts.ViewModels;
|
|||
|
using MongoDB.Bson;
|
|||
|
using MongoDB.Driver;
|
|||
|
|
|||
|
namespace BeautySaloonNoSQLDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class ClientStorage : IClientStorage
|
|||
|
{
|
|||
|
private readonly NewdbContext _source;
|
|||
|
public ClientStorage()
|
|||
|
{
|
|||
|
_source = NewdbContext.GetInstance();
|
|||
|
}
|
|||
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|||
|
{
|
|||
|
var element = _source.Clients
|
|||
|
.FindOneAndDelete(new BsonDocument("id", model.Id));
|
|||
|
if (element != null)
|
|||
|
return element.GetViewModel;
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|||
|
{
|
|||
|
if (model.Id.HasValue)
|
|||
|
return _source.Clients
|
|||
|
.Find(new BsonDocument("id", model.Id)).FirstOrDefault()?.GetViewModel;
|
|||
|
if (!string.IsNullOrEmpty(model.Phone))
|
|||
|
return _source.Clients
|
|||
|
.Find(new BsonDocument("phone", model.Phone)).FirstOrDefault()?.GetViewModel;
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(model.Surname))
|
|||
|
{
|
|||
|
var filter = Builders<Client>.Filter.Regex("surname", new BsonRegularExpression(model.Surname));
|
|||
|
return _source.Clients.Find(filter).ToList().Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
public List<ClientViewModel> GetFullList()
|
|||
|
{
|
|||
|
var filterDefinition = Builders<Client>.Filter.Empty;
|
|||
|
return _source.Clients.Find(filterDefinition).ToList().Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public ClientViewModel? Insert(ClientBindingModel model)
|
|||
|
{
|
|||
|
model.Id = _source.Clients.AsQueryable()
|
|||
|
.Count() > 0 ? _source.Clients.AsQueryable().Max(x => x.Id) + 1 : 1;
|
|||
|
var newElement = Client.Create(model);
|
|||
|
if (newElement == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
_source.Clients.InsertOne(newElement);
|
|||
|
return newElement.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public ClientViewModel? Update(ClientBindingModel model)
|
|||
|
{
|
|||
|
var element = _source.Clients.Find(new BsonDocument("id", model.Id)).FirstOrDefault();
|
|||
|
if (element == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
element.Update(model);
|
|||
|
_source.Clients.FindOneAndReplace(new BsonDocument("id", model.Id), element);
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|