productStorage
This commit is contained in:
parent
0a6730a722
commit
2f7003d3b7
@ -12,7 +12,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
using var context = new ComputerHardwareStoreDBContext();
|
using var context = new ComputerHardwareStoreDBContext();
|
||||||
return context.Components
|
return context.Components
|
||||||
.Select(x => x.GetViewModel)
|
.Select(c => c.GetViewModel)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,12 +20,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(model.Name))
|
if (string.IsNullOrEmpty(model.Name))
|
||||||
{
|
{
|
||||||
return new ();
|
return new();
|
||||||
}
|
}
|
||||||
using var context = new ComputerHardwareStoreDBContext();
|
using var context = new ComputerHardwareStoreDBContext();
|
||||||
return context.Components
|
return context.Components
|
||||||
.Where(x => x.Name.Contains(model.Name))
|
.Where(c => c.Name.Contains(model.Name))
|
||||||
.Select(x => x.GetViewModel)
|
.Select(c => c.GetViewModel)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,10 +37,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
|
|||||||
}
|
}
|
||||||
using var context = new ComputerHardwareStoreDBContext();
|
using var context = new ComputerHardwareStoreDBContext();
|
||||||
return context.Components
|
return context.Components
|
||||||
.FirstOrDefault(x =>
|
.FirstOrDefault(c =>
|
||||||
(!string.IsNullOrEmpty(model.Name)
|
(!string.IsNullOrEmpty(model.Name) && c .Name == model.Name) ||
|
||||||
&& x.Name == model.Name)
|
(model.Id.HasValue && c.Id == model.Id))
|
||||||
|| (model.Id.HasValue && x.Id == model.Id))
|
|
||||||
?.GetViewModel;
|
?.GetViewModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +58,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
|
|||||||
public ComponentViewModel? Update(ComponentBindingModel model)
|
public ComponentViewModel? Update(ComponentBindingModel model)
|
||||||
{
|
{
|
||||||
using var context = new ComputerHardwareStoreDBContext();
|
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)
|
if (component == null)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
@ -72,14 +71,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
|
|||||||
public ComponentViewModel? Delete(ComponentBindingModel model)
|
public ComponentViewModel? Delete(ComponentBindingModel model)
|
||||||
{
|
{
|
||||||
using var context = new ComputerHardwareStoreDBContext();
|
using var context = new ComputerHardwareStoreDBContext();
|
||||||
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
|
var element = context.Components.FirstOrDefault(c => c.Id == model.Id);
|
||||||
if (element != null)
|
if (element == null)
|
||||||
{
|
{
|
||||||
context.Components.Remove(element);
|
return null;
|
||||||
context.SaveChanges();
|
|
||||||
return element.GetViewModel;
|
|
||||||
}
|
}
|
||||||
return null;
|
context.Components.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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