From ed2f4e0e6991b18eb844d5451bccfaa16719cb47 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Fri, 10 Mar 2023 11:11:14 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=84=D0=B8=D0=BA=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs index be227d3..3e93b67 100644 --- a/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs +++ b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs @@ -21,9 +21,13 @@ namespace ShipyardDataBaseImplement.Implements .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { + var deletedElement = context.Orders + .Include(x => x.Ship) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; context.Orders.Remove(element); context.SaveChanges(); - return element.GetViewModel; + return deletedElement; } return null; } @@ -36,6 +40,7 @@ namespace ShipyardDataBaseImplement.Implements } using var context = new ShipyardDataBase(); return context.Orders + .Include(x=>x.Ship) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; } @@ -48,19 +53,11 @@ namespace ShipyardDataBaseImplement.Implements } using var context = new ShipyardDataBase(); return context.Orders + .Include(x=>x.Ship) .Where(x => x.Id == model.Id) .Select(x => x.GetViewModel) .ToList(); } - private static OrderViewModel GetViewModel(Order order) - { - var viewModel = order.GetViewModel; - using var context = new ShipyardDataBase(); - var element = context.Ships - .FirstOrDefault(x => x.Id == order.ShipId); - viewModel.ShipName = element.ShipName; - return viewModel; - } public List GetFullList() { @@ -81,7 +78,10 @@ namespace ShipyardDataBaseImplement.Implements using var context = new ShipyardDataBase(); context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; + return context.Orders + .Include(x => x.Ship) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) @@ -94,7 +94,10 @@ namespace ShipyardDataBaseImplement.Implements } order.Update(model); context.SaveChanges(); - return order.GetViewModel; + return context.Orders + .Include(x => x.Ship) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } } }