Fix JSON interface deserialization error

This commit is contained in:
ShabOl 2024-05-28 18:48:09 +04:00
parent 527af10bbc
commit fb2938e682
11 changed files with 21 additions and 51 deletions

View File

@ -102,9 +102,6 @@ namespace ComputerShopBusinessLogic.BusinessLogics
if (string.IsNullOrEmpty(Model.Category))
throw new ArgumentException($"У сборки отсутствует категория");
if (Model.Price < 0)
throw new ArgumentException("Цена сборки должна быть больше 0", nameof(Model.Price));
var Element = _assemblyStorage.GetElement(new AssemblySearchModel
{
AssemblyName = Model.AssemblyName

View File

@ -110,9 +110,6 @@ namespace ComputerShopBusinessLogic.BusinessLogics
if (string.IsNullOrEmpty(Model.ProductName))
throw new ArgumentException($"У товара отсутствует название");
if (Model.Price < 0)
throw new ArgumentException("Цена товара должна быть больше 0", nameof(Model.Price));
if (Model.Warranty <= 0)
throw new ArgumentException("Гарантия на товар должна быть больше 0", nameof(Model.Warranty));

View File

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

View File

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

View File

@ -3,7 +3,7 @@ using System.ComponentModel;
namespace ComputerShopContracts.ViewModels
{
public class AssemblyViewModel : IAssemblyModel
public class AssemblyViewModel : IAssemblyModel
{
public int Id { get; set; }
@ -18,6 +18,6 @@ namespace ComputerShopContracts.ViewModels
[DisplayName("Категория")]
public string Category { get; set; } = string.Empty;
public Dictionary<int, IComponentModel> AssemblyComponents { get; set; } = new();
}
public Dictionary<int, ComponentViewModel> AssemblyComponents { get; set; } = new();
}
}

View File

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

View File

@ -24,10 +24,5 @@
/// Категория
/// </summary>
string Category { get; }
/// <summary>
/// Список комплектующих
/// </summary>
Dictionary<int, IComponentModel> AssemblyComponents { get; }
}
}

View File

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

View File

@ -28,10 +28,10 @@ namespace ComputerShopDatabaseImplement.Models
[ForeignKey("AssemblyId")]
public virtual List<AssemblyComponent> Components { get; set; } = new();
private Dictionary<int, IComponentModel>? _assemblyComponents;
private Dictionary<int, Component>? _assemblyComponents;
[NotMapped]
public Dictionary<int, IComponentModel> AssemblyComponents
public Dictionary<int, Component> AssemblyComponents
{
get
{
@ -39,7 +39,7 @@ namespace ComputerShopDatabaseImplement.Models
{
_assemblyComponents = Components.ToDictionary(
AsmComp => AsmComp.ComponentId,
AsmComp => AsmComp.Component as IComponentModel
AsmComp => AsmComp.Component
);
}
@ -88,13 +88,14 @@ namespace ComputerShopDatabaseImplement.Models
AssemblyName = AssemblyName,
Price = Price,
Category = Category,
AssemblyComponents = AssemblyComponents,
AssemblyComponents = AssemblyComponents.ToDictionary(x => x.Key, x => x.Value.ViewModel),
};
public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model)
{
// Сначала подсчитывается новая цена, т.к. Model.AssemblyComponents далее может измениться
double NewPrice = Context.Components
// Сначала подсчитывается новая цена, т.к. Model.AssemblyComponents далее может измениться
double NewPrice = Context.Components
.ToList()
.Where(x => Model.AssemblyComponents.ContainsKey(x.Id))
.Sum(x => x.Cost);
@ -132,21 +133,5 @@ namespace ComputerShopDatabaseImplement.Models
_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

@ -29,10 +29,10 @@ namespace ComputerShopDatabaseImplement.Models
[ForeignKey("ProductId")]
public virtual List<ProductComponent> Components { get; set; } = new();
private Dictionary<int, IComponentModel>? _productComponents;
private Dictionary<int, Component>? _productComponents;
[NotMapped]
public Dictionary<int, IComponentModel> ProductComponents
public Dictionary<int, Component> ProductComponents
{
get
{
@ -40,7 +40,7 @@ namespace ComputerShopDatabaseImplement.Models
{
_productComponents = Components.ToDictionary(
ProdComp => ProdComp.ComponentId,
ProdComp => ProdComp.Component as IComponentModel
ProdComp => ProdComp.Component
);
}
@ -64,7 +64,7 @@ namespace ComputerShopDatabaseImplement.Models
UserId = Model.UserId,
ShipmentId = Model.ShipmentId,
ProductName = Model.ProductName,
Price = Model.Price,
Price = Price,
Warranty = Model.Warranty,
Components = Components,
};
@ -87,15 +87,17 @@ namespace ComputerShopDatabaseImplement.Models
UserId = UserId,
ShipmentId = ShipmentId,
ProviderName = Shipment?.ProviderName,
ProductName = ProductName,
Price = Price,
Warranty = Warranty,
ProductComponents = ProductComponents,
ProductComponents = ProductComponents.ToDictionary(x => x.Key, x => x.Value.ViewModel),
};
public void UpdateComponents(ComputerShopDatabase Context, ProductBindingModel Model)
{
// Сначала подсчитывается новая цена, т.к. Model.ProductComponents далее может измениться
double NewPrice = Context.Components
.ToList()
.Where(x => Model.ProductComponents.ContainsKey(x.Id))
.Sum(x => x.Cost);

View File

@ -58,6 +58,7 @@ namespace ComputerShopRestApi.Controllers
{
try
{
if (Model.ShipmentId == 0) Model.ShipmentId = null;
_productLogic.Create(Model);
}
catch (Exception ex)