productStorage
This commit is contained in:
parent
0a6730a722
commit
2f7003d3b7
@ -12,7 +12,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
|
||||
{
|
||||
using var context = new ComputerHardwareStoreDBContext();
|
||||
return context.Components
|
||||
.Select(x => x.GetViewModel)
|
||||
.Select(c => c.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
@ -24,8 +24,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user