PIbd-22_Shabunov_O.A._SushiBar/SushiBarDatabaseImplement/Storages/CookStorage.cs
2024-05-16 17:13:55 +04:00

84 lines
2.3 KiB
C#

using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
namespace SushiBarDatabaseImplement.Storages
{
public class CookStorage
{
public List<CookViewModel> GetFullList()
{
using var Context = new SushiBarDatabase();
return Context.Cooks.Select(x => x.ViewModel).ToList();
}
public List<CookViewModel> GetFilteredList(CookSearchModel Model)
{
using var Context = new SushiBarDatabase();
if (Model.EmploymentDateFrom.HasValue)
{
return Context.Cooks
.Where(x => x.EmploymentDate >= Model.EmploymentDateFrom && x.EmploymentDate <= Model.EmploymentDateTo)
.Select(x => x.ViewModel)
.ToList();
}
return new();
}
public CookViewModel? GetElement(CookSearchModel Model)
{
if (!Model.Id.HasValue)
return null;
using var Context = new SushiBarDatabase();
return Context.Cooks
.FirstOrDefault(x => x.Id == Model.Id)?.ViewModel;
}
public CookViewModel? Insert(CookBindingModel Model)
{
var NewCook = Cook.Create(Model);
if (NewCook == null)
return null;
using var Context = new SushiBarDatabase();
Context.Cooks.Add(NewCook);
Context.SaveChanges();
return NewCook.ViewModel;
}
public CookViewModel? Update(CookBindingModel Model)
{
using var Context = new SushiBarDatabase();
var Cook = Context.Cooks.FirstOrDefault(x => x.Id == Model.Id);
if (Cook == null)
return null;
Cook.Update(Model);
Context.SaveChanges();
return Cook.ViewModel;
}
public CookViewModel? Delete(CookBindingModel Model)
{
using var Context = new SushiBarDatabase();
var Cook = Context.Cooks.FirstOrDefault(rec => rec.Id == Model.Id);
if (Cook == null)
return null;
Context.Cooks.Remove(Cook);
Context.SaveChanges();
return Cook.ViewModel;
}
}
}