Реализовал хранилища БД
This commit is contained in:
parent
1814173da3
commit
1230cbb950
@ -23,7 +23,7 @@ namespace InternetShopDatabase.Models
|
||||
ImagePath = ImagePath,
|
||||
ProductNames = ProductNames,
|
||||
};
|
||||
public static Order? Create(OrderBindingModel model)
|
||||
public static Order Create(OrderBindingModel model)
|
||||
{
|
||||
return new Order()
|
||||
{
|
||||
|
@ -15,7 +15,7 @@ namespace InternetShopDatabase.Models
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
};
|
||||
public static Product? Create(ProductBindingModel model)
|
||||
public static Product Create(ProductBindingModel model)
|
||||
{
|
||||
return new Product()
|
||||
{
|
||||
|
75
InternetShop/InternetShopDatabase/Storages/OrderStorage.cs
Normal file
75
InternetShop/InternetShopDatabase/Storages/OrderStorage.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using InternetShopContracts.DataBindingModels;
|
||||
using InternetShopContracts.DataSearchModels;
|
||||
using InternetShopContracts.DataViewModels;
|
||||
using InternetShopContracts.StorageContracts;
|
||||
using InternetShopDatabase.Models;
|
||||
|
||||
namespace InternetShopDatabase.Storages
|
||||
{
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new InternetShopDatabase();
|
||||
var item = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (item != null)
|
||||
{
|
||||
context.Orders.Remove(item);
|
||||
context.SaveChanges();
|
||||
return item.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
{
|
||||
return GetFilteredList(model).FirstOrDefault();
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
var elements = GetFullList();
|
||||
if (model.Id != null)
|
||||
{
|
||||
elements = elements.Where(x => x.Id == model.Id).ToList();
|
||||
}
|
||||
if (model.CustomerFIO != null)
|
||||
{
|
||||
elements = elements.Where(x => x.CustomerFIO.Contains(model.CustomerFIO)).ToList();
|
||||
}
|
||||
if (model.CustomerEmail != null)
|
||||
{
|
||||
elements = elements.Where(x => x.CustomerEmail.Contains(model.CustomerEmail)).ToList();
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
using var context = new InternetShopDatabase();
|
||||
return context.Orders.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
using var context = new InternetShopDatabase();
|
||||
var item = Order.Create(model);
|
||||
context.Orders.Add(item);
|
||||
context.SaveChanges();
|
||||
return item.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new InternetShopDatabase();
|
||||
var item = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (item == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
item.Update(model);
|
||||
context.SaveChanges();
|
||||
return item.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
71
InternetShop/InternetShopDatabase/Storages/ProductStorage.cs
Normal file
71
InternetShop/InternetShopDatabase/Storages/ProductStorage.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using InternetShopContracts.DataBindingModels;
|
||||
using InternetShopContracts.DataSearchModels;
|
||||
using InternetShopContracts.DataViewModels;
|
||||
using InternetShopContracts.StorageContracts;
|
||||
using InternetShopDatabase.Models;
|
||||
|
||||
namespace InternetShopDatabase.Storages
|
||||
{
|
||||
public class ProductStorage : IProductStorage
|
||||
{
|
||||
public ProductViewModel? Delete(ProductBindingModel model)
|
||||
{
|
||||
using var context = new InternetShopDatabase();
|
||||
var item = context.Products.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (item != null)
|
||||
{
|
||||
context.Products.Remove(item);
|
||||
context.SaveChanges();
|
||||
return item.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ProductViewModel? GetElement(ProductSearchModel model)
|
||||
{
|
||||
return GetFilteredList(model).FirstOrDefault();
|
||||
}
|
||||
|
||||
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
|
||||
{
|
||||
var elements = GetFullList();
|
||||
if (model.Id != null)
|
||||
{
|
||||
elements = elements.Where(x => x.Id == model.Id).ToList();
|
||||
}
|
||||
if (model.Name != null)
|
||||
{
|
||||
elements = elements.Where(x => x.Name.Contains(model.Name)).ToList();
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
|
||||
public List<ProductViewModel> GetFullList()
|
||||
{
|
||||
using var context = new InternetShopDatabase();
|
||||
return context.Products.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public ProductViewModel Insert(ProductBindingModel model)
|
||||
{
|
||||
using var context = new InternetShopDatabase();
|
||||
var item = Product.Create(model);
|
||||
context.Products.Add(item);
|
||||
context.SaveChanges();
|
||||
return item.GetViewModel;
|
||||
}
|
||||
|
||||
public ProductViewModel? Update(ProductBindingModel model)
|
||||
{
|
||||
using var context = new InternetShopDatabase();
|
||||
var item = context.Products.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (item == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
item.Update(model);
|
||||
context.SaveChanges();
|
||||
return item.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user