SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonDatabaseImplement/Implements/PositionStorage.cs
2023-03-29 20:00:53 +04:00

89 lines
2.9 KiB
C#

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)
{
using var context = new NewdbContext();
model.Id = context.Positions
.Count() > 0 ? context.Positions.Max(x => x.Id) + 1 : 1;
var newPosition = Position.Create(model);
if (newPosition == null)
{
return null;
}
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;
}
}
}