productStorage

This commit is contained in:
bekodeg 2024-04-30 20:42:42 +04:00
parent 0a6730a722
commit 2f7003d3b7
2 changed files with 99 additions and 15 deletions

View File

@ -12,7 +12,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
{
using var context = new ComputerHardwareStoreDBContext();
return context.Components
.Select(x => x.GetViewModel)
.Select(c => c.GetViewModel)
.ToList();
}
@ -20,12 +20,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
{
if (string.IsNullOrEmpty(model.Name))
{
return new ();
return new();
}
using var context = new ComputerHardwareStoreDBContext();
return context.Components
.Where(x => x.Name.Contains(model.Name))
.Select(x => x.GetViewModel)
.Where(c => c.Name.Contains(model.Name))
.Select(c => c.GetViewModel)
.ToList();
}
@ -37,10 +37,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
}
using var context = new ComputerHardwareStoreDBContext();
return context.Components
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name)
&& x.Name == model.Name)
|| (model.Id.HasValue && x.Id == model.Id))
.FirstOrDefault(c =>
(!string.IsNullOrEmpty(model.Name) && c .Name == model.Name) ||
(model.Id.HasValue && c.Id == model.Id))
?.GetViewModel;
}
@ -59,7 +58,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var component = context.Components.FirstOrDefault(x => x.Id == model.Id);
var component = context.Components.FirstOrDefault(c => c.Id == model.Id);
if (component == null)
{
return null;
@ -72,14 +71,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
var element = context.Components.FirstOrDefault(c => c.Id == model.Id);
if (element == null)
{
return null;
}
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,85 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class ProductStorage : IProductStorage
{
public List<ProductViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Products
.Select(p => p.GetViewModel)
.ToList();
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new ComputerHardwareStoreDBContext();
return context.Products
.Where(p => p.Name.Contains(model.Name))
.Select(p => p.GetViewModel)
.ToList();
}
public ProductViewModel? GetElement(ProductSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.Products
.FirstOrDefault(c =>
(!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) ||
(model.Id.HasValue && c.Id == model.Id))
?.GetViewModel;
}
public ProductViewModel? Insert(ProductBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var newProduct = Product.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Products.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public ProductViewModel? Update(ProductBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var product = context.Products.FirstOrDefault(p => p.Id == model.Id);
if (product == null)
{
return null;
}
context.Update(model);
context.SaveChanges();
return product.GetViewModel;
}
public ProductViewModel? Delete(ProductBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var product = context.Products.FirstOrDefault(p => p.Id == model.Id);
if (product == null)
{
return null;
}
context.Products.Remove(product);
context.SaveChanges();
return product.GetViewModel;
}
}
}