using CanteenContracts.BindingModels; using CanteenContracts.SearchModel; using CanteenContracts.StoragesContracts; using CanteenContracts.View; using CanteenDatabaseImplement.Models; using CanteenDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CanteenDatabaseImplement.Implements { public class CookStorage : ICookStorage { public CookViewModel? GetElement(CookSearchModel model) { if (!model.Id.HasValue) { return null; } using var context = new CanteenDatabase(); return context.Cooks.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; } public List GetFilteredList(CookSearchModel model) { using var context = new CanteenDatabase(); return context.Cooks .Where(x => !model.Id.HasValue || x.Id == model.Id) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { using var context = new CanteenDatabase(); 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 CanteenDatabase(); context.Cooks.Add(newCook); context.SaveChanges(); return newCook.GetViewModel; } public CookViewModel? Update(CookBindingModel model) { using var context = new CanteenDatabase(); var cook = context.Cooks.FirstOrDefault(x => x.Id == model.Id); if (cook == null) { return null; } cook.Update(model); context.SaveChanges(); return cook.GetViewModel; } public CookViewModel? Delete(CookBindingModel model) { using var context = new CanteenDatabase(); var cook = context.Cooks.FirstOrDefault(x => x.Id == model.Id); if (cook != null) { context.Cooks.Remove(cook); context.SaveChanges(); return cook.GetViewModel; } return null; } } }