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.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;