using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.ViewModels; using SushiBarDatabaseImplement.Models; namespace SushiBarDatabaseImplement.Storages { public class CustomerStorage { public List GetFullList() { using var Context = new SushiBarDatabase(); return Context.Customers.Select(x => x.ViewModel).ToList(); } public List GetFilteredList(CustomerSearchModel Model) { using var Context = new SushiBarDatabase(); if (Model.SumOfAllOrders.HasValue) { return Context.Customers .Where(x => x.SumOfAllOrders >= Model.SumOfAllOrders) .Select(x => x.ViewModel) .ToList(); } return new(); } public CustomerViewModel? GetElement(CustomerSearchModel Model) { if (!Model.Id.HasValue) return null; using var Context = new SushiBarDatabase(); return Context.Customers .FirstOrDefault(x => x.Id == Model.Id)?.ViewModel; } public CustomerViewModel? Insert(CustomerBindingModel Model) { var NewCustomer = Customer.Create(Model); if (NewCustomer == null) return null; using var Context = new SushiBarDatabase(); Context.Customers.Add(NewCustomer); Context.SaveChanges(); return NewCustomer.ViewModel; } public CustomerViewModel? Update(CustomerBindingModel Model) { using var Context = new SushiBarDatabase(); var Customer = Context.Customers.FirstOrDefault(x => x.Id == Model.Id); if (Customer == null) return null; Customer.Update(Model); Context.SaveChanges(); return Customer.ViewModel; } public CustomerViewModel? Delete(CustomerBindingModel Model) { using var Context = new SushiBarDatabase(); var Customer = Context.Customers.FirstOrDefault(rec => rec.Id == Model.Id); if (Customer == null) return null; Context.Customers.Remove(Customer); Context.SaveChanges(); return Customer.ViewModel; } } }