Compare commits
3 Commits
19d3158342
...
1ae74a6fdb
Author | SHA1 | Date | |
---|---|---|---|
|
1ae74a6fdb | ||
|
5337beeb65 | ||
|
db1fdee078 |
@ -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)
|
||||
{
|
||||
context.Components.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
context.Components.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,108 @@
|
||||
using ComputerHardwareStoreContracts.BindingModels;
|
||||
using ComputerHardwareStoreContracts.SearchModels;
|
||||
using ComputerHardwareStoreContracts.StorageContracts;
|
||||
using ComputerHardwareStoreContracts.ViewModels;
|
||||
using ComputerHardwareStoreDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ComputerHardwareStoreDatabaseImplement.Implements
|
||||
{
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
using var context = new ComputerHardwareStoreDBContext();
|
||||
return context.Orders
|
||||
.Include(o => o.Products)
|
||||
.ThenInclude(o => o.Product)
|
||||
.Select(o => o.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
using var context = new ComputerHardwareStoreDBContext();
|
||||
return context.Orders
|
||||
.Include(o => o.Products)
|
||||
.ThenInclude(o => o.Product)
|
||||
.Where(o =>
|
||||
(model.Id.HasValue && o.Id == model.Id) ||
|
||||
((model.DateFrom.HasValue && model.DateTo.HasValue) &&
|
||||
((model.DateFrom <= o.DateCreate && o.DateCreate <= model.DateTo) ||
|
||||
(o.DateImplement.HasValue && o.DateCreate < model.DateFrom && model.DateFrom < o.DateImplement)))
|
||||
)
|
||||
.Select(o => o.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new ComputerHardwareStoreDBContext();
|
||||
return context.Orders
|
||||
.Include(o => o.Products)
|
||||
.ThenInclude(o => o.Product)
|
||||
.FirstOrDefault(o => o.Id == model.Id)?
|
||||
.GetViewModel;
|
||||
|
||||
}
|
||||
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
using var context = new ComputerHardwareStoreDBContext();
|
||||
var newOrder = Order.Create(context, model);
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Orders.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
return newOrder.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new ComputerHardwareStoreDBContext();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var order = context.Orders
|
||||
.Include(o => o.Products)
|
||||
.ThenInclude(o => o.Product)
|
||||
.FirstOrDefault(o => o.Id == model.Id);
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
order.Update(model);
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return order.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new ComputerHardwareStoreDBContext();
|
||||
var element = context.Orders
|
||||
.Include(o => o.Products)
|
||||
.ThenInclude(o => o.Product)
|
||||
.FirstOrDefault(o => o.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Orders.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
using ComputerHardwareStoreContracts.BindingModels;
|
||||
using ComputerHardwareStoreContracts.SearchModels;
|
||||
using ComputerHardwareStoreContracts.StorageContracts;
|
||||
using ComputerHardwareStoreContracts.ViewModels;
|
||||
using ComputerHardwareStoreDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ComputerHardwareStoreDatabaseImplement.Implements
|
||||
{
|
||||
public class ProductStorage : IProductStorage
|
||||
{
|
||||
public List<ProductViewModel> GetFullList()
|
||||
{
|
||||
using var context = new ComputerHardwareStoreDBContext();
|
||||
return context.Products
|
||||
.Include(p => p.Components)
|
||||
.ThenInclude(p => p.Component)
|
||||
.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))
|
||||
.Include(p => p.Components)
|
||||
.ThenInclude(p => p.Component)
|
||||
.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
|
||||
.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;
|
||||
}
|
||||
|
||||
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 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
|
||||
.Include(p => p.Components)
|
||||
.ThenInclude(p => p.Component)
|
||||
.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
|
||||
.Include(p => p.Components)
|
||||
.ThenInclude(p => p.Component)
|
||||
.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