using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; using SushiBarDatabaseImplement.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SushiBarDatabaseImplement.Implements { public class CookStorage : ICookStorage { public CookViewModel? Delete(CookBindingModel model) { using var context = new SushiBarDatabase(); var element = context.Cooks.FirstOrDefault(x => x.Id == model.Id); if (element != null) { context.Cooks.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public CookViewModel? GetElement(CookSearchModel model) { using var context = new SushiBarDatabase(); return context.Cooks.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CookName)) && x.CookName == model.CookName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel; } public List GetFilteredList(CookSearchModel model) { if (string.IsNullOrEmpty(model.CookName)) { return new(); } using var context = new SushiBarDatabase(); return context.Cooks.Where(x => x.CookName.Contains(model.CookName)).Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new SushiBarDatabase(); return context.Cooks.Select(x => x.GetViewModel).ToList(); } 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.GetViewModel; } public CookViewModel? Update(CookBindingModel model) { using var context = new SushiBarDatabase(); var component = context.Cooks.FirstOrDefault(x => x.Id == model.Id); if (component == null) { return null; } component.Update(model); context.SaveChanges(); return component.GetViewModel; } } }