From 3c6b43efd8ca7855ed451f6d8710eb2fb8aa0d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=A5=D0=B0=D1=80=D0=BB?= =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B2?= Date: Mon, 3 Apr 2023 10:00:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=BD=D1=8F=D1=82=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs index 0e5b831..d50cfec 100644 --- a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs @@ -14,16 +14,13 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements { public class OrderStorage : IOrderStorage { - public OrderViewModel? GetElement(OrderSearchModel model) + public List GetFullList() { - if (!model.Id.HasValue) - { - return null; - } using var context = new AbstractFoodOrdersDatabase(); - return context.Orders.Include(x => x.Dish).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Orders + .Select(x => AccessDishStorage(x.GetViewModel, context)) + .ToList(); } - public List GetFilteredList(OrderSearchModel model) { if (!model.Id.HasValue) @@ -33,17 +30,20 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements using var context = new AbstractFoodOrdersDatabase(); return context.Orders .Where(x => x.Id == model.Id) - .Include(x => x.Dish) - .Select(x => x.GetViewModel) + .Select(x => AccessDishStorage(x.GetViewModel, context)) .ToList(); } - - public List GetFullList() + public OrderViewModel? GetElement(OrderSearchModel model) { + if (!model.Id.HasValue) + { + return new(); + } using var context = new AbstractFoodOrdersDatabase(); - return context.Orders.Include(x => x.Dish).Select(x => x.GetViewModel).ToList(); + return AccessDishStorage(context.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel, context); } - public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); @@ -54,9 +54,8 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements using var context = new AbstractFoodOrdersDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return context.Orders.Include(x => x.Dish).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + return AccessDishStorage(newOrder.GetViewModel, context); } - public OrderViewModel? Update(OrderBindingModel model) { using var context = new AbstractFoodOrdersDatabase(); @@ -67,7 +66,7 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - return context.Orders.Include(x => x.Dish).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return AccessDishStorage(order.GetViewModel, context); } public OrderViewModel? Delete(OrderBindingModel model) { @@ -77,9 +76,16 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements { context.Orders.Remove(element); context.SaveChanges(); - return element.GetViewModel; + return AccessDishStorage(element.GetViewModel, context); } return null; } + static OrderViewModel AccessDishStorage(OrderViewModel model, AbstractFoodOrdersDatabase context) + { + if (model == null) return model; + string? dishName = context.Dishes.FirstOrDefault(x => x.Id == model.DishId)?.DishName; + if (dishName != null) model.DishName = dishName; + return model; + } } }