From 5337beeb659e0d7e02937542640634b4242cf909 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 21:00:26 +0400 Subject: [PATCH] productStorage --- .../Implements/ProductStorage.cs | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs index a5face0..9c74297 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -3,6 +3,7 @@ using ComputerHardwareStoreContracts.SearchModels; using ComputerHardwareStoreContracts.StorageContracts; using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace ComputerHardwareStoreDatabaseImplement.Implements { @@ -12,6 +13,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements { using var context = new ComputerHardwareStoreDBContext(); return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) .Select(p => p.GetViewModel) .ToList(); } @@ -25,6 +28,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements using var context = new ComputerHardwareStoreDBContext(); return context.Products .Where(p => p.Name.Contains(model.Name)) + .Include(p => p.Components) + .ThenInclude(p => p.Component) .Select(p => p.GetViewModel) .ToList(); } @@ -37,10 +42,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) .FirstOrDefault(c => (!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) || - (model.Id.HasValue && c.Id == model.Id)) - ?.GetViewModel; + (model.Id.HasValue && c.Id == model.Id))? + .GetViewModel; } public ProductViewModel? Insert(ProductBindingModel model) @@ -53,13 +60,20 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } context.Products.Add(newProduct); context.SaveChanges(); - return newProduct.GetViewModel; + return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == newProduct.Id)? + .GetViewModel; } public ProductViewModel? Update(ProductBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + var product = context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == model.Id); if (product == null) { return null; @@ -72,7 +86,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ProductViewModel? Delete(ProductBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + var product = context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == model.Id); if (product == null) { return null;