Edit AssemblyComponent and ProductComponent entities

This commit is contained in:
ShabOl 2024-05-28 14:57:17 +04:00
parent 1f7fb4f849
commit 4e68232dfd
14 changed files with 107 additions and 79 deletions

View File

@ -14,7 +14,7 @@ namespace ComputerShopContracts.BindingModels
public string Category { get; set; } = string.Empty; public string Category { get; set; } = string.Empty;
public Dictionary<int, (IComponentModel, int)> AssemblyComponents { get; set; } = new(); public Dictionary<int, IComponentModel> AssemblyComponents { get; set; } = new();
public Dictionary<int, int> Test { get; set; } = new(); public Dictionary<int, int> Test { get; set; } = new();
} }

View File

@ -16,6 +16,6 @@ namespace ComputerShopContracts.BindingModels
public int Warranty { get; set; } public int Warranty { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new(); public Dictionary<int, IComponentModel> ProductComponents { get; set; } = new();
} }
} }

View File

@ -18,6 +18,6 @@ namespace ComputerShopContracts.ViewModels
[DisplayName("Категория")] [DisplayName("Категория")]
public string Category { get; set; } = string.Empty; public string Category { get; set; } = string.Empty;
public Dictionary<int, (IComponentModel, int)> AssemblyComponents { get; set; } = new(); public Dictionary<int, IComponentModel> AssemblyComponents { get; set; } = new();
} }
} }

View File

@ -23,6 +23,6 @@ namespace ComputerShopContracts.ViewModels
[DisplayName("Гарантия (мес.)")] [DisplayName("Гарантия (мес.)")]
public int Warranty { get; set; } public int Warranty { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new(); public Dictionary<int, IComponentModel> ProductComponents { get; set; } = new();
} }
} }

View File

@ -28,6 +28,6 @@
/// <summary> /// <summary>
/// Список комплектующих /// Список комплектующих
/// </summary> /// </summary>
Dictionary<int, (IComponentModel, int)> AssemblyComponents { get; } Dictionary<int, IComponentModel> AssemblyComponents { get; }
} }
} }

View File

@ -28,7 +28,7 @@
/// <summary> /// <summary>
/// Список комплектующих /// Список комплектующих
/// </summary> /// </summary>
Dictionary<int, (IComponentModel, int)> ProductComponents { get; } Dictionary<int, IComponentModel> ProductComponents { get; }
/// <summary> /// <summary>
/// Привязка товара к партии товаров /// Привязка товара к партии товаров

View File

@ -24,7 +24,7 @@ namespace ComputerShopDatabaseImplement.Implements
{ {
using var Context = new ComputerShopDatabase(); using var Context = new ComputerShopDatabase();
// Optional search by Category name // Optional search by Category
if (!string.IsNullOrEmpty(Model.Category)) if (!string.IsNullOrEmpty(Model.Category))
{ {
return Context.Assemblies return Context.Assemblies
@ -35,6 +35,7 @@ namespace ComputerShopDatabaseImplement.Implements
.ToList(); .ToList();
} }
// Search by UserId by default
return Context.Assemblies return Context.Assemblies
.Include(x => x.Components) .Include(x => x.Components)
.ThenInclude(x => x.Component) .ThenInclude(x => x.Component)

View File

@ -106,7 +106,7 @@ namespace ComputerShopDatabaseImplement.Implements
ComponentName = x.ComponentName, ComponentName = x.ComponentName,
ComponentCost = x.Cost, ComponentCost = x.Cost,
Shipments = x.ProductComponents Shipments = x.ProductComponents
.Select(y => (y.Count, y.Product.ProductName, y.Product.Price, y.Product.Shipment!.ProviderName, y.Product.Shipment.DateShipment)) .Select(y => (0, y.Product.ProductName, y.Product.Price, y.Product.Shipment!.ProviderName, y.Product.Shipment.DateShipment))
.ToList(), .ToList(),
}) })
.ToList(); .ToList();

View File

@ -37,6 +37,7 @@ namespace ComputerShopDatabaseImplement.Implements
.ToList(); .ToList();
} }
// Search by UserId by default
return Context.Products return Context.Products
.Include(x => x.Shipment) .Include(x => x.Shipment)
.Include(x => x.Components) .Include(x => x.Components)

View File

@ -28,10 +28,10 @@ namespace ComputerShopDatabaseImplement.Models
[ForeignKey("AssemblyId")] [ForeignKey("AssemblyId")]
public virtual List<AssemblyComponent> Components { get; set; } = new(); public virtual List<AssemblyComponent> Components { get; set; } = new();
private Dictionary<int, (IComponentModel, int)>? _assemblyComponents; private Dictionary<int, IComponentModel>? _assemblyComponents;
[NotMapped] [NotMapped]
public Dictionary<int, (IComponentModel, int)> AssemblyComponents public Dictionary<int, IComponentModel> AssemblyComponents
{ {
get get
{ {
@ -39,7 +39,7 @@ namespace ComputerShopDatabaseImplement.Models
{ {
_assemblyComponents = Components.ToDictionary( _assemblyComponents = Components.ToDictionary(
AsmComp => AsmComp.ComponentId, AsmComp => AsmComp.ComponentId,
AsmComp => (AsmComp.Component as IComponentModel, AsmComp.Count) AsmComp => AsmComp.Component as IComponentModel
); );
} }
@ -49,18 +49,22 @@ namespace ComputerShopDatabaseImplement.Models
public static Assembly Create(ComputerShopDatabase Context, AssemblyBindingModel Model) public static Assembly Create(ComputerShopDatabase Context, AssemblyBindingModel Model)
{ {
return new() var Components = Model.AssemblyComponents
.Select(x => new AssemblyComponent
{
Component = Context.Components.First(y => y.Id == x.Key)
})
.ToList();
double Price = Components.Sum(x => x.Component.Cost);
return new Assembly()
{ {
Id = Model.Id, Id = Model.Id,
UserId = Model.UserId, UserId = Model.UserId,
AssemblyName = Model.AssemblyName, AssemblyName = Model.AssemblyName,
Price = Model.Price, Price = Price,
Category = Model.Category, Category = Model.Category,
Components = Model.AssemblyComponents.Select(x => new AssemblyComponent Components = Components,
{
Component = Context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
}; };
} }
@ -75,8 +79,6 @@ namespace ComputerShopDatabaseImplement.Models
{ {
Category = Model.Category; Category = Model.Category;
} }
Price = Model.Price;
} }
public AssemblyViewModel ViewModel => new() public AssemblyViewModel ViewModel => new()
@ -91,17 +93,22 @@ namespace ComputerShopDatabaseImplement.Models
public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model) public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model)
{ {
// Сначала подсчитывается новая цена, т.к. Model.AssemblyComponents далее может измениться
double NewPrice = Context.Components
.Where(x => Model.AssemblyComponents.ContainsKey(x.Id))
.Sum(x => x.Cost);
var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList(); var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList();
if (AssemblyComponents != null && AssemblyComponents.Count > 0) if (AssemblyComponents != null && AssemblyComponents.Count > 0)
{ {
// Удаление записей из таблицы AssemblyComponents тех компонентов, которых нет в модели
Context.AssemblyComponents Context.AssemblyComponents
.RemoveRange(AssemblyComponents .RemoveRange(AssemblyComponents.Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId)));
.Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId)));
Context.SaveChanges(); Context.SaveChanges();
// После этого в Model.AssemblyComponents останутся только те компоненты, записей о которых еще нет в БД
foreach (var ComponentToUpdate in AssemblyComponents) foreach (var ComponentToUpdate in AssemblyComponents)
{ {
ComponentToUpdate.Count = Model.AssemblyComponents[ComponentToUpdate.ComponentId].Item2;
Model.AssemblyComponents.Remove(ComponentToUpdate.ComponentId); Model.AssemblyComponents.Remove(ComponentToUpdate.ComponentId);
} }
@ -115,13 +122,31 @@ namespace ComputerShopDatabaseImplement.Models
{ {
Assembly = CurrentAssembly, Assembly = CurrentAssembly,
Component = Context.Components.First(x => x.Id == AssemblyComponent.Key), Component = Context.Components.First(x => x.Id == AssemblyComponent.Key),
Count = AssemblyComponent.Value.Item2
}); });
Context.SaveChanges(); Context.SaveChanges();
} }
Price = NewPrice;
Context.SaveChanges();
_assemblyComponents = null; _assemblyComponents = null;
} }
private void CalculatePrice(
ComputerShopDatabase Context,
Dictionary<int, IComponentModel> ModelComponents,
out List<AssemblyComponent> OutComponents,
out double OutPrice)
{
OutComponents = ModelComponents
.Select(x => new AssemblyComponent
{
Component = Context.Components.First(y => y.Id == x.Key)
})
.ToList();
OutPrice = Components.Sum(x => x.Component.Cost);
}
} }
} }

View File

@ -12,9 +12,6 @@ namespace ComputerShopDatabaseImplement.Models
[Required] [Required]
public int ComponentId { get; set; } public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Assembly Assembly { get; set; } = new(); public virtual Assembly Assembly { get; set; } = new();
public virtual Component Component { get; set; } = new(); public virtual Component Component { get; set; } = new();

View File

@ -29,10 +29,10 @@ namespace ComputerShopDatabaseImplement.Models
[ForeignKey("ProductId")] [ForeignKey("ProductId")]
public virtual List<ProductComponent> Components { get; set; } = new(); public virtual List<ProductComponent> Components { get; set; } = new();
private Dictionary<int, (IComponentModel, int)>? _productComponents; private Dictionary<int, IComponentModel>? _productComponents;
[NotMapped] [NotMapped]
public Dictionary<int, (IComponentModel, int)> ProductComponents public Dictionary<int, IComponentModel> ProductComponents
{ {
get get
{ {
@ -40,7 +40,7 @@ namespace ComputerShopDatabaseImplement.Models
{ {
_productComponents = Components.ToDictionary( _productComponents = Components.ToDictionary(
ProdComp => ProdComp.ComponentId, ProdComp => ProdComp.ComponentId,
ProdComp => (ProdComp.Component as IComponentModel, ProdComp.Count) ProdComp => ProdComp.Component as IComponentModel
); );
} }
@ -50,7 +50,15 @@ namespace ComputerShopDatabaseImplement.Models
public static Product Create(ComputerShopDatabase Context, ProductBindingModel Model) public static Product Create(ComputerShopDatabase Context, ProductBindingModel Model)
{ {
return new() var Components = Model.ProductComponents
.Select(x => new ProductComponent
{
Component = Context.Components.First(y => y.Id == x.Key)
})
.ToList();
double Price = Components.Sum(x => x.Component.Cost);
return new Product()
{ {
Id = Model.Id, Id = Model.Id,
UserId = Model.UserId, UserId = Model.UserId,
@ -58,11 +66,7 @@ namespace ComputerShopDatabaseImplement.Models
ProductName = Model.ProductName, ProductName = Model.ProductName,
Price = Model.Price, Price = Model.Price,
Warranty = Model.Warranty, Warranty = Model.Warranty,
Components = Model.ProductComponents.Select(x => new ProductComponent Components = Components,
{
Component = Context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
}; };
} }
@ -74,7 +78,6 @@ namespace ComputerShopDatabaseImplement.Models
} }
ShipmentId = Model.ShipmentId; ShipmentId = Model.ShipmentId;
Price = Model.Price;
Warranty = Model.Warranty; Warranty = Model.Warranty;
} }
@ -91,17 +94,22 @@ namespace ComputerShopDatabaseImplement.Models
public void UpdateComponents(ComputerShopDatabase Context, ProductBindingModel Model) public void UpdateComponents(ComputerShopDatabase Context, ProductBindingModel Model)
{ {
// Сначала подсчитывается новая цена, т.к. Model.ProductComponents далее может измениться
double NewPrice = Context.Components
.Where(x => Model.ProductComponents.ContainsKey(x.Id))
.Sum(x => x.Cost);
var ProductComponents = Context.ProductComponents.Where(x => x.ProductId == Model.Id).ToList(); var ProductComponents = Context.ProductComponents.Where(x => x.ProductId == Model.Id).ToList();
if (ProductComponents != null && ProductComponents.Count > 0) if (ProductComponents != null && ProductComponents.Count > 0)
{ {
// Удаление записей из таблицы ProductComponents тех компонентов, которых нет в модели
Context.ProductComponents Context.ProductComponents
.RemoveRange(ProductComponents .RemoveRange(ProductComponents.Where(x => !Model.ProductComponents.ContainsKey(x.ComponentId)));
.Where(x => !Model.ProductComponents.ContainsKey(x.ComponentId)));
Context.SaveChanges(); Context.SaveChanges();
// После этого в Model.ProductComponents останутся только те компоненты, записей о которых еще нет в БД
foreach (var ComponentToUpdate in ProductComponents) foreach (var ComponentToUpdate in ProductComponents)
{ {
ComponentToUpdate.Count = Model.ProductComponents[ComponentToUpdate.ComponentId].Item2;
Model.ProductComponents.Remove(ComponentToUpdate.ComponentId); Model.ProductComponents.Remove(ComponentToUpdate.ComponentId);
} }
@ -115,12 +123,14 @@ namespace ComputerShopDatabaseImplement.Models
{ {
Product = CurrentProduct, Product = CurrentProduct,
Component = Context.Components.First(x => x.Id == ProductComponent.Key), Component = Context.Components.First(x => x.Id == ProductComponent.Key),
Count = ProductComponent.Value.Item2
}); });
Context.SaveChanges(); Context.SaveChanges();
} }
Price = NewPrice;
Context.SaveChanges();
_productComponents = null; _productComponents = null;
} }
} }

View File

@ -12,9 +12,6 @@ namespace ComputerShopDatabaseImplement.Models
[Required] [Required]
public int ComponentId { get; set; } public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Product Product { get; set; } = new(); public virtual Product Product { get; set; } = new();
public virtual Component Component { get; set; } = new(); public virtual Component Component { get; set; } = new();

View File

@ -2,13 +2,8 @@
using ComputerShopContracts.ViewModels; using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Enums; using ComputerShopDataModels.Enums;
using ComputerShopDataModels.Models; using ComputerShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Models namespace ComputerShopDatabaseImplement.Models
{ {
@ -30,19 +25,21 @@ namespace ComputerShopDatabaseImplement.Models
[ForeignKey("UserId")] [ForeignKey("UserId")]
public virtual List<Shipment> Shipments { get; set; } = new(); public virtual List<Shipment> Shipments { get; set; } = new();
[ForeignKey("UserId")] [ForeignKey("UserId")]
public virtual List<Order> Orders { get; set; } = new(); public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("UserId")] [ForeignKey("UserId")]
public virtual List<Request> Requests { get; set; } = new(); public virtual List<Request> Requests { get; set; } = new();
[ForeignKey("UserId")] [ForeignKey("UserId")]
public virtual List<Assembly> Assemblies { get; set; } = new(); public virtual List<Assembly> Assemblies { get; set; } = new();
[ForeignKey("UserId")] [ForeignKey("UserId")]
public virtual List<Component> Components { get; set; } = new(); public virtual List<Component> Components { get; set; } = new();
[ForeignKey("UserId")] [ForeignKey("UserId")]
public virtual List<Product> Proucts { get; set; } = new(); public virtual List<Product> Products { get; set; } = new();
public static User Create(UserBindingModel model) public static User Create(UserBindingModel model)
{ {