SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonDatabaseImplement/Implements/PositionStorage.cs

87 lines
2.8 KiB
C#
Raw Normal View History

2023-03-28 21:24:52 +04:00
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonDatabaseImplement.Implements
{
public class PositionStorage : IPositionStorage
{
public PositionViewModel? Delete(PositionBindingModel model)
{
using var context = new NewdbContext();
var element = context.Positions
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Positions.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public PositionViewModel? GetElement(PositionSearchModel model)
{
using var context = new NewdbContext();
if (model.Id.HasValue)
return context.Positions
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
if (!string.IsNullOrEmpty(model.Name))
return context.Positions
.FirstOrDefault(x => x.Name.Equals(model.Name))?
.GetViewModel;
return null;
}
public List<PositionViewModel> GetFilteredList(PositionSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new NewdbContext();
return context.Positions
.Where(x => x.Name.Contains(model.Name))
.Select(x => x.GetViewModel)
.ToList();
}
public List<PositionViewModel> GetFullList()
{
using var context = new NewdbContext();
return context.Positions
.Select(x => x.GetViewModel)
.ToList();
}
public PositionViewModel? Insert(PositionBindingModel model)
{
var newPosition = Position.Create(model);
if (newPosition == null)
{
return null;
}
using var context = new NewdbContext();
context.Positions.Add(newPosition);
context.SaveChanges();
return newPosition.GetViewModel;
}
public PositionViewModel? Update(PositionBindingModel model)
{
using var context = new NewdbContext();
var element = context.Positions
.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return element.GetViewModel;
}
}
}