Edit AssemblyComponent and ProductComponent entities
This commit is contained in:
parent
1f7fb4f849
commit
4e68232dfd
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,6 @@
|
||||
/// <summary>
|
||||
/// Список комплектующих
|
||||
/// </summary>
|
||||
Dictionary<int, (IComponentModel, int)> AssemblyComponents { get; }
|
||||
Dictionary<int, IComponentModel> AssemblyComponents { get; }
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@
|
||||
/// <summary>
|
||||
/// Список комплектующих
|
||||
/// </summary>
|
||||
Dictionary<int, (IComponentModel, int)> ProductComponents { get; }
|
||||
Dictionary<int, IComponentModel> ProductComponents { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Привязка товара к партии товаров
|
||||
|
@ -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)
|
||||
|
@ -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();
|
||||
|
@ -37,6 +37,7 @@ namespace ComputerShopDatabaseImplement.Implements
|
||||
.ToList();
|
||||
}
|
||||
|
||||
// Search by UserId by default
|
||||
return Context.Products
|
||||
.Include(x => x.Shipment)
|
||||
.Include(x => x.Components)
|
||||
|
@ -28,18 +28,18 @@ 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
|
||||
{
|
||||
if (_assemblyComponents == null)
|
||||
{
|
||||
_assemblyComponents = Components.ToDictionary(
|
||||
AsmComp => AsmComp.ComponentId,
|
||||
AsmComp => (AsmComp.Component as IComponentModel, AsmComp.Count)
|
||||
AsmComp => AsmComp.ComponentId,
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
@ -69,14 +73,12 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
if (!string.IsNullOrEmpty(Model.AssemblyName))
|
||||
{
|
||||
AssemblyName = Model.AssemblyName;
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(Model.Category))
|
||||
{
|
||||
Category = Model.Category;
|
||||
}
|
||||
|
||||
Price = Model.Price;
|
||||
}
|
||||
|
||||
public AssemblyViewModel ViewModel => new()
|
||||
@ -91,17 +93,22 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
|
||||
public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model)
|
||||
{
|
||||
var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList();
|
||||
// Сначала подсчитывается новая цена, т.к. 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();
|
||||
|
||||
foreach (var ComponentToUpdate in AssemblyComponents)
|
||||
// После этого в 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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,37 +94,44 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
|
||||
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();
|
||||
// Сначала подсчитывается новая цена, т.к. Model.ProductComponents далее может измениться
|
||||
double NewPrice = Context.Components
|
||||
.Where(x => Model.ProductComponents.ContainsKey(x.Id))
|
||||
.Sum(x => x.Cost);
|
||||
|
||||
foreach (var ComponentToUpdate in ProductComponents)
|
||||
{
|
||||
ComponentToUpdate.Count = Model.ProductComponents[ComponentToUpdate.ComponentId].Item2;
|
||||
Model.ProductComponents.Remove(ComponentToUpdate.ComponentId);
|
||||
}
|
||||
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)));
|
||||
Context.SaveChanges();
|
||||
|
||||
Context.SaveChanges();
|
||||
}
|
||||
// После этого в Model.ProductComponents останутся только те компоненты, записей о которых еще нет в БД
|
||||
foreach (var ComponentToUpdate in ProductComponents)
|
||||
{
|
||||
Model.ProductComponents.Remove(ComponentToUpdate.ComponentId);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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),
|
||||
});
|
||||
|
||||
_productComponents = null;
|
||||
}
|
||||
Context.SaveChanges();
|
||||
}
|
||||
|
||||
Price = NewPrice;
|
||||
Context.SaveChanges();
|
||||
|
||||
_productComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -2,17 +2,12 @@
|
||||
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
|
||||
{
|
||||
public class User : IUserModel
|
||||
public class User : IUserModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user