Реализовал хранилища БД

This commit is contained in:
Никита Потапов 2024-11-20 12:14:16 +04:00
parent 1814173da3
commit 1230cbb950
4 changed files with 148 additions and 2 deletions

View File

@ -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()
{

View File

@ -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()
{

View 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;
}
}
}

View 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;
}
}
}