productStorage

This commit is contained in:
bekodeg 2024-04-30 21:00:26 +04:00
parent db1fdee078
commit 5337beeb65

View File

@ -3,6 +3,7 @@ using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts; using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models; using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement.Implements namespace ComputerHardwareStoreDatabaseImplement.Implements
{ {
@ -12,6 +13,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
{ {
using var context = new ComputerHardwareStoreDBContext(); using var context = new ComputerHardwareStoreDBContext();
return context.Products return context.Products
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.Select(p => p.GetViewModel) .Select(p => p.GetViewModel)
.ToList(); .ToList();
} }
@ -25,6 +28,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
using var context = new ComputerHardwareStoreDBContext(); using var context = new ComputerHardwareStoreDBContext();
return context.Products return context.Products
.Where(p => p.Name.Contains(model.Name)) .Where(p => p.Name.Contains(model.Name))
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.Select(p => p.GetViewModel) .Select(p => p.GetViewModel)
.ToList(); .ToList();
} }
@ -37,10 +42,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
} }
using var context = new ComputerHardwareStoreDBContext(); using var context = new ComputerHardwareStoreDBContext();
return context.Products return context.Products
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.FirstOrDefault(c => .FirstOrDefault(c =>
(!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) || (!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) ||
(model.Id.HasValue && c.Id == model.Id)) (model.Id.HasValue && c.Id == model.Id))?
?.GetViewModel; .GetViewModel;
} }
public ProductViewModel? Insert(ProductBindingModel model) public ProductViewModel? Insert(ProductBindingModel model)
@ -53,13 +60,20 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
} }
context.Products.Add(newProduct); context.Products.Add(newProduct);
context.SaveChanges(); 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) public ProductViewModel? Update(ProductBindingModel model)
{ {
using var context = new ComputerHardwareStoreDBContext(); 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) if (product == null)
{ {
return null; return null;
@ -72,7 +86,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
public ProductViewModel? Delete(ProductBindingModel model) public ProductViewModel? Delete(ProductBindingModel model)
{ {
using var context = new ComputerHardwareStoreDBContext(); 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) if (product == null)
{ {
return null; return null;