From c1e1972d3a1ea264c4bf776d644f2c99d6fe8162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 4 Mar 2023 20:45:41 +0400 Subject: [PATCH] fix --- .../ClientStorage.cs | 7 +++++-- ConfectioneryDatabaseImplement/Order.cs | 3 ++- .../OrderStorage.cs | 19 ++++++++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ConfectioneryDatabaseImplement/ClientStorage.cs b/ConfectioneryDatabaseImplement/ClientStorage.cs index 09e7a8b..03092da 100644 --- a/ConfectioneryDatabaseImplement/ClientStorage.cs +++ b/ConfectioneryDatabaseImplement/ClientStorage.cs @@ -16,7 +16,8 @@ namespace ConfectioneryDatabaseImplement.Implements if (res != null) { context.Clients.Remove(res); - } + context.SaveChanges(); + } return res?.GetViewModel; } @@ -70,7 +71,8 @@ namespace ConfectioneryDatabaseImplement.Implements if (res != null) { context.Clients.Add(res); - } + context.SaveChanges(); + } return res?.GetViewModel; } @@ -79,6 +81,7 @@ namespace ConfectioneryDatabaseImplement.Implements using var context = new ConfectioneryDatabase(); var res = context.Clients.FirstOrDefault(x => x.Id == model.Id); res?.Update(model); + context.SaveChanges(); return res?.GetViewModel; } } diff --git a/ConfectioneryDatabaseImplement/Order.cs b/ConfectioneryDatabaseImplement/Order.cs index 4d1289b..9859bee 100644 --- a/ConfectioneryDatabaseImplement/Order.cs +++ b/ConfectioneryDatabaseImplement/Order.cs @@ -84,7 +84,8 @@ namespace ConfectioneryDatabaseImplement.Models return new() { PastryName = context.Pastries.FirstOrDefault(x => x.Id == PastryId)?.PastryName ?? string.Empty, - ClientFIO = Client.ClientFIO, + // ?????? + ClientFIO = Client?.ClientFIO ?? context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, PastryId = PastryId, Count = Count, Sum = Sum, diff --git a/ConfectioneryDatabaseImplement/OrderStorage.cs b/ConfectioneryDatabaseImplement/OrderStorage.cs index 9192987..e962a50 100644 --- a/ConfectioneryDatabaseImplement/OrderStorage.cs +++ b/ConfectioneryDatabaseImplement/OrderStorage.cs @@ -3,6 +3,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System.Security.Cryptography.X509Certificates; namespace ConfectioneryDatabaseImplement.Implements @@ -12,7 +13,7 @@ namespace ConfectioneryDatabaseImplement.Implements public OrderViewModel? Delete(OrderBindingModel model) { using var context = new ConfectioneryDatabase(); - var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); + var element = context.Orders.Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id); if (element != null) { context.Orders.Remove(element); @@ -29,7 +30,10 @@ namespace ConfectioneryDatabaseImplement.Implements { return null; } - return context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + return context.Orders + .Include(x => x.Client) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) @@ -44,13 +48,15 @@ namespace ConfectioneryDatabaseImplement.Implements { return context.Orders .Where(x => model.DateFrom <= x.DateCreate.Date && x.DateCreate <= model.DateTo) - .Select(x => x.GetViewModel) + .Include(x => x.Client) + .Select(x => x.GetViewModel) .ToList(); } if (model.ClientId.HasValue) { return context.Orders - .Where(x => x.Client.Id == model.ClientId) + .Where(x => x.Client.Id == model.ClientId) + .Include(x => x.Client) .Select(x => x.GetViewModel) .ToList(); } @@ -61,6 +67,7 @@ namespace ConfectioneryDatabaseImplement.Implements { using var context = new ConfectioneryDatabase(); return context.Orders + .Include(x => x.Client) .Select(x => x.GetViewModel) .ToList(); } @@ -81,7 +88,9 @@ namespace ConfectioneryDatabaseImplement.Implements public OrderViewModel? Update(OrderBindingModel model) { using var context = new ConfectioneryDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null;