87 lines
3.2 KiB
C#
87 lines
3.2 KiB
C#
|
using Amazon.Auth.AccessControlPolicy;
|
|||
|
using BeautySaloonContracts.BindingModels;
|
|||
|
using BeautySaloonContracts.SearchModels;
|
|||
|
using BeautySaloonContracts.StoragesContracts;
|
|||
|
using BeautySaloonContracts.ViewModels;
|
|||
|
using MongoDB.Bson;
|
|||
|
using MongoDB.Driver;
|
|||
|
|
|||
|
namespace BeautySaloonNoSQLDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class EmployeeStorage : IEmployeeStorage
|
|||
|
{
|
|||
|
private readonly NewdbContext _source;
|
|||
|
public EmployeeStorage()
|
|||
|
{
|
|||
|
_source = NewdbContext.GetInstance();
|
|||
|
}
|
|||
|
public EmployeeViewModel? Delete(EmployeeBindingModel model)
|
|||
|
{
|
|||
|
var element = _source.Employees
|
|||
|
.FindOneAndDelete(new BsonDocument("id", model.Id));
|
|||
|
if (element != null)
|
|||
|
return element.GetViewModel;
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
|
|||
|
{
|
|||
|
if (model.Id.HasValue)
|
|||
|
return _source.Employees
|
|||
|
.Find(new BsonDocument("id", model.Id)).FirstOrDefault()?.GetViewModel;
|
|||
|
if (!string.IsNullOrEmpty(model.Phone))
|
|||
|
return _source.Employees
|
|||
|
.Find(new BsonDocument("phone", model.Phone)).FirstOrDefault()?.GetViewModel;
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
|
|||
|
{
|
|||
|
if (model.Id.HasValue)
|
|||
|
return _source.Employees
|
|||
|
.Find(new BsonDocument("id", model.Id)).ToList().Select(x => x.GetViewModel).ToList();
|
|||
|
if (!string.IsNullOrEmpty(model.PositionName))
|
|||
|
return _source.Employees
|
|||
|
.Find(new BsonDocument("position", model.PositionName)).ToList().Select(x => x.GetViewModel).ToList();
|
|||
|
if (!string.IsNullOrEmpty(model.Surname))
|
|||
|
{
|
|||
|
var filter = Builders<Employee>.Filter.Regex("surname", new BsonRegularExpression(model.Surname));
|
|||
|
return _source.Employees.Find(filter).ToList().Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
public List<EmployeeViewModel> GetFullList()
|
|||
|
{
|
|||
|
var filterDefinition = Builders<Employee>.Filter.Empty;
|
|||
|
return _source.Employees.Find(filterDefinition).ToList().Select(x => x.GetViewModel).ToList();
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public EmployeeViewModel? Insert(EmployeeBindingModel model)
|
|||
|
{
|
|||
|
model.Id = _source.Employees.AsQueryable()
|
|||
|
.Count() > 0 ? _source.Employees.AsQueryable().Max(x => x.Id) + 1 : 1;
|
|||
|
var newElement = Employee.Create(model);
|
|||
|
if (newElement == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
_source.Employees.InsertOne(newElement);
|
|||
|
return newElement.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public EmployeeViewModel? Update(EmployeeBindingModel model)
|
|||
|
{
|
|||
|
var element = _source.Employees.Find(new BsonDocument("id", model.Id)).FirstOrDefault();
|
|||
|
if (element == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
element.Update(model);
|
|||
|
_source.Employees.FindOneAndReplace(new BsonDocument("id", model.Id), element);
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|