using SushiContracts.BindingModels; using SushiContracts.SearchModels; using SushiContracts.StoragesContracts; using SushiContracts.ViewModels; using SushiDatabaseImplement; using SushiShopDatabaseImplement.Models; namespace SushiShopDatabaseImplement.Implements { public class OrderStorage : IOrderStorage { public List GetFullList() { using var context = new SushiDatabase(); return context.Orders .Select(x => AccessSushisStorage(x.GetViewModel)) .ToList(); } public List GetFilteredList(OrderSearchModel model) { if (!model.Id.HasValue) { return new(); } using var context = new SushiDatabase(); return context.Orders .Where(x => x.Id == model.Id) .Select(x => AccessSushisStorage(x.GetViewModel)) .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) { return null; } using var context = new SushiDatabase(); return AccessSushisStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); } public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); if (newOrder == null) { return null; } using var context = new SushiDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); return AccessSushisStorage(newOrder.GetViewModel); } public OrderViewModel? Update(OrderBindingModel model) { using var context = new SushiDatabase(); var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null; } order.Update(model); context.SaveChanges(); return AccessSushisStorage(order.GetViewModel); } public OrderViewModel? Delete(OrderBindingModel model) { using var context = new SushiDatabase(); var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Orders.Remove(element); context.SaveChanges(); return AccessSushisStorage(element.GetViewModel); } return null; } public static OrderViewModel AccessSushisStorage(OrderViewModel model) { if (model == null) return null; using var context = new SushiDatabase(); foreach (var Sushies in context.Sushies) { if (Sushies.Id == model.SushiId) { model.SushiName = Sushies.SushiName; break; } } foreach (var Clients in context.Clients) { if (Clients.Id == model.ClientId) { model.ClientName = Clients.ClientName; break; } } foreach (var Employees in context.Employees) { if (Employees.Id == model.EmployeeId) { model.EmployeeName = Employees.EmployeeName; break; } } return model; } } }