Compare commits

...

3 Commits

7 changed files with 439 additions and 13 deletions

View File

@ -7,6 +7,8 @@ namespace ComputerShopContracts.BindingModels
public int Id { get; set; }
public int UserId { get; set; }
public int? ShipmentId { get; set; }
public string ProductName { get; set; } = string.Empty;
@ -15,7 +17,5 @@ namespace ComputerShopContracts.BindingModels
public int Warranty { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new();
public int? ShipmentId { get; set; }
}
}

View File

@ -9,6 +9,11 @@ namespace ComputerShopContracts.ViewModels
public int UserId { get; set; }
public int? ShipmentId { get; set; }
[DisplayName("Поставщик")]
public string? ProviderName { get; set; }
[DisplayName("Название товара")]
public string ProductName { get; set; } = string.Empty;
@ -19,10 +24,5 @@ namespace ComputerShopContracts.ViewModels
public int Warranty { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new();
public int? ShipmentId { get; set; }
[DisplayName("Поставщик")]
public string ProviderName { get; set; } = string.Empty;
}
}

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

@ -1,4 +1,5 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -63,6 +64,64 @@ namespace ComputerShopDatabaseImplement.Models
};
}
// TODO: Update(), ViewModel, UpdateComponents()
public void Update(AssemblyBindingModel Model)
{
if (!string.IsNullOrEmpty(Model.AssemblyName))
{
AssemblyName = Model.AssemblyName;
}
if (!string.IsNullOrEmpty(Model.Category))
{
Category = Model.Category;
}
Cost = Model.Cost;
}
public AssemblyViewModel ViewModel => new()
{
Id = Id,
UserId = UserId,
AssemblyName = AssemblyName,
Cost = Cost,
Category = Category,
AssemblyComponents = AssemblyComponents,
};
public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model)
{
var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList();
if (AssemblyComponents != null && AssemblyComponents.Count > 0)
{
Context.AssemblyComponents
.RemoveRange(AssemblyComponents
.Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId)));
Context.SaveChanges();
foreach (var ComponentToUpdate in AssemblyComponents)
{
ComponentToUpdate.Count = Model.AssemblyComponents[ComponentToUpdate.ComponentId].Item2;
Model.AssemblyComponents.Remove(ComponentToUpdate.ComponentId);
}
Context.SaveChanges();
}
var CurrentAssembly = Context.Assemblies.First(x => x.Id == Id);
foreach (var AssemblyComponent in Model.AssemblyComponents)
{
Context.AssemblyComponents.Add(new AssemblyComponent
{
Assembly = CurrentAssembly,
Component = Context.Components.First(x => x.Id == AssemblyComponent.Key),
Count = AssemblyComponent.Value.Item2
});
Context.SaveChanges();
}
_assemblyComponents = null;
}
}
}

View File

@ -1,4 +1,5 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -12,6 +13,10 @@ namespace ComputerShopDatabaseImplement.Models
[Required]
public int UserId { get; set; }
public int? ShipmentId { get; set; }
public virtual Shipment? Shipment { get; set; }
[Required]
public string ProductName { get; set; } = string.Empty;
@ -21,10 +26,6 @@ namespace ComputerShopDatabaseImplement.Models
[Required]
public int Warranty { get; set; }
public int? ShipmentId { get; set; }
public virtual Shipment? Shipment { get; set; }
[ForeignKey("ProductId")]
public virtual List<ProductComponent> Components { get; set; } = new();
@ -53,6 +54,7 @@ namespace ComputerShopDatabaseImplement.Models
{
Id = Model.Id,
UserId = Model.UserId,
ShipmentId = Model.ShipmentId,
ProductName = Model.ProductName,
Cost = Model.Cost,
Warranty = Model.Warranty,
@ -61,8 +63,65 @@ namespace ComputerShopDatabaseImplement.Models
Component = Context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
ShipmentId = Model.ShipmentId,
};
}
public void Update(ProductBindingModel Model)
{
if (!string.IsNullOrEmpty(Model.ProductName))
{
ProductName = Model.ProductName;
}
ShipmentId = Model.ShipmentId;
Cost = Model.Cost;
Warranty = Model.Warranty;
}
public ProductViewModel ViewModel => new()
{
Id = Id,
UserId = UserId,
ShipmentId = ShipmentId,
ProviderName = Shipment?.ProviderName,
Cost = Cost,
Warranty = Warranty,
ProductComponents = ProductComponents,
};
public void UpdateComponents(ComputerShopDatabase Context, ProductBindingModel Model)
{
var ProductComponents = Context.ProductComponents.Where(x => x.ProductId == Model.Id).ToList();
if (ProductComponents != null && ProductComponents.Count > 0)
{
Context.ProductComponents
.RemoveRange(ProductComponents
.Where(x => !Model.ProductComponents.ContainsKey(x.ComponentId)));
Context.SaveChanges();
foreach (var ComponentToUpdate in ProductComponents)
{
ComponentToUpdate.Count = Model.ProductComponents[ComponentToUpdate.ComponentId].Item2;
Model.ProductComponents.Remove(ComponentToUpdate.ComponentId);
}
Context.SaveChanges();
}
var CurrentProduct = Context.Products.First(x => x.Id == Id);
foreach (var ProductComponent in Model.ProductComponents)
{
Context.ProductComponents.Add(new ProductComponent
{
Product = CurrentProduct,
Component = Context.Components.First(x => x.Id == ProductComponent.Key),
Count = ProductComponent.Value.Item2
});
Context.SaveChanges();
}
_productComponents = null;
}
}
}