using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.ViewModels; using SushiBarDatabaseImplement.Models; namespace SushiBarDatabaseImplement.Storages { public class CookStorage { public List GetFullList() { using var Context = new SushiBarDatabase(); return Context.Cooks.Select(x => x.ViewModel).ToList(); } public List 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; } } }