Implement DataBase storages

This commit is contained in:
ShabOl 2024-05-01 00:41:26 +04:00
parent 81b5398465
commit a2b722b517
5 changed files with 311 additions and 3 deletions

View File

@ -0,0 +1,111 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerShopDatabaseImplement.Implements
{
public class AssemblyStorage : IAssemblyStorage
{
public List<AssemblyViewModel> GetFullList()
{
using var Context = new ComputerShopDatabase();
return Context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Assembly)
.Select(x => x.ViewModel)
.ToList();
}
public List<AssemblyViewModel> GetFilteredList(AssemblySearchModel Model)
{
using var Context = new ComputerShopDatabase();
// Optional search by Category name
if (!string.IsNullOrEmpty(Model.Category))
{
return Context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Assembly)
.Where(x => x.UserId == Model.UserId && x.Category == Model.Category)
.Select(x => x.ViewModel)
.ToList();
}
return Context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Assembly)
.Where(x => x.UserId == Model.UserId)
.Select(x => x.ViewModel)
.ToList();
}
public AssemblyViewModel? GetElement(AssemblySearchModel Model)
{
using var Context = new ComputerShopDatabase();
return Context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Assembly)
.FirstOrDefault(x => x.Id == Model.Id)?
.ViewModel;
}
public AssemblyViewModel? Insert(AssemblyBindingModel Model)
{
using var Context = new ComputerShopDatabase();
var NewAssembly = Assembly.Create(Context, Model);
Context.Assemblies.Add(NewAssembly);
Context.SaveChanges();
return NewAssembly.ViewModel;
}
public AssemblyViewModel? Update(AssemblyBindingModel Model)
{
using var Context = new ComputerShopDatabase();
using var Transaction = Context.Database.BeginTransaction();
try
{
var ExistingAssembly = Context.Assemblies.FirstOrDefault(x => x.Id == Model.Id);
if (ExistingAssembly == null)
{
return null;
}
ExistingAssembly.Update(Model);
Context.SaveChanges();
ExistingAssembly.UpdateComponents(Context, Model);
Transaction.Commit();
return ExistingAssembly.ViewModel;
}
catch
{
Transaction.Rollback();
throw;
}
}
public AssemblyViewModel? Delete(AssemblyBindingModel Model)
{
using var Context = new ComputerShopDatabase();
var ExistingAssembly = Context.Assemblies.Include(x => x.Components).FirstOrDefault(x => x.Id == Model.Id);
if (ExistingAssembly == null)
{
return null;
}
Context.Assemblies.Remove(ExistingAssembly);
Context.SaveChanges();
return ExistingAssembly.ViewModel;
}
}
}

View File

@ -0,0 +1,82 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
namespace ComputerShopDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var Context = new ComputerShopDatabase();
return Context.Components
.Select(x => x.ViewModel)
.ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel Model)
{
using var Context = new ComputerShopDatabase();
return Context.Components
.Where(x => x.UserId == Model.UserId)
.Select(x => x.ViewModel)
.ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel Model)
{
using var Context = new ComputerShopDatabase();
return Context.Components
.FirstOrDefault(x => x.Id == Model.Id)?
.ViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel Model)
{
using var Context = new ComputerShopDatabase();
var NewComponent = Component.Create(Model);
Context.Components.Add(NewComponent);
Context.SaveChanges();
return NewComponent.ViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel Model)
{
using var Context = new ComputerShopDatabase();
var ExistingComponent = Context.Components.FirstOrDefault(x => x.Id == Model.Id);
if (ExistingComponent == null)
{
return null;
}
ExistingComponent.Update(Model);
Context.SaveChanges();
return ExistingComponent.ViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel Model)
{
using var Context = new ComputerShopDatabase();
var ExistingComponent = Context.Components.FirstOrDefault(x => x.Id == Model.Id);
if (ExistingComponent == null)
{
return null;
}
Context.Components.Remove(ExistingComponent);
Context.SaveChanges();
return ExistingComponent.ViewModel;
}
}
}

View File

@ -0,0 +1,115 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerShopDatabaseImplement.Implements
{
public class ProductStorage : IProductStorage
{
public List<ProductViewModel> GetFullList()
{
using var Context = new ComputerShopDatabase();
return Context.Products
.Include(x => x.Shipment)
.Include(x => x.Components)
.ThenInclude(x => x.Product)
.Select(x => x.ViewModel)
.ToList();
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel Model)
{
using var Context = new ComputerShopDatabase();
// Optional search by Shipment
if (Model.ShipmentId.HasValue)
{
return Context.Products
.Include(x => x.Shipment)
.Include(x => x.Components)
.ThenInclude(x => x.Product)
.Where(x => x.UserId == Model.UserId && x.ShipmentId == Model.ShipmentId)
.Select(x => x.ViewModel)
.ToList();
}
return Context.Products
.Include(x => x.Shipment)
.Include(x => x.Components)
.ThenInclude(x => x.Product)
.Where(x => x.UserId == Model.UserId)
.Select(x => x.ViewModel)
.ToList();
}
public ProductViewModel? GetElement(ProductSearchModel Model)
{
using var Context = new ComputerShopDatabase();
return Context.Products
.Include(x => x.Shipment)
.Include(x => x.Components)
.ThenInclude(x => x.Product)
.FirstOrDefault(x => x.Id == Model.Id)?
.ViewModel;
}
public ProductViewModel? Insert(ProductBindingModel Model)
{
using var Context = new ComputerShopDatabase();
var NewProduct = Product.Create(Context, Model);
Context.Products.Add(NewProduct);
Context.SaveChanges();
return NewProduct.ViewModel;
}
public ProductViewModel? Update(ProductBindingModel Model)
{
using var Context = new ComputerShopDatabase();
using var Transaction = Context.Database.BeginTransaction();
try
{
var ExistingProduct = Context.Products.FirstOrDefault(x => x.Id == Model.Id);
if (ExistingProduct == null)
{
return null;
}
ExistingProduct.Update(Model);
Context.SaveChanges();
ExistingProduct.UpdateComponents(Context, Model);
Transaction.Commit();
return ExistingProduct.ViewModel;
}
catch
{
Transaction.Rollback();
throw;
}
}
public ProductViewModel? Delete(ProductBindingModel Model)
{
using var Context = new ComputerShopDatabase();
var ExistingProduct = Context.Products.Include(x => x.Components).FirstOrDefault(x => x.Id == Model.Id);
if (ExistingProduct == null)
{
return null;
}
Context.Products.Remove(ExistingProduct);
Context.SaveChanges();
return ExistingProduct.ViewModel;
}
}
}

View File

@ -79,7 +79,7 @@ namespace ComputerShopDatabaseImplement.Models
Cost = Model.Cost;
}
public AssemblyViewModel GetViewModel => new()
public AssemblyViewModel ViewModel => new()
{
Id = Id,
UserId = UserId,

View File

@ -78,15 +78,15 @@ namespace ComputerShopDatabaseImplement.Models
Warranty = Model.Warranty;
}
public ProductViewModel GetViewModel => new()
public ProductViewModel ViewModel => new()
{
Id = Id,
UserId = UserId,
ShipmentId = ShipmentId,
ProviderName = Shipment?.ProviderName,
Cost = Cost,
Warranty = Warranty,
ProductComponents = ProductComponents,
ProviderName = Shipment?.ProviderName,
};
public void UpdateComponents(ComputerShopDatabase Context, ProductBindingModel Model)