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 Dictionary<int, (IComponentModel, int)> AssemblyComponents { get; set; } = new();
public Dictionary<int, IComponentModel> AssemblyComponents { 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 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("Категория")]
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("Гарантия (мес.)")]
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>
Dictionary<int, (IComponentModel, int)> AssemblyComponents { get; }
Dictionary<int, IComponentModel> AssemblyComponents { get; }
}
}

View File

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

View File

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

View File

@ -106,7 +106,7 @@ namespace ComputerShopDatabaseImplement.Implements
ComponentName = x.ComponentName,
ComponentCost = x.Cost,
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();

View File

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

View File

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

View File

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

View File

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

View File

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