diff --git a/Course/BusinessLogic/BusinessLogic/ProductLogic.cs b/Course/BusinessLogic/BusinessLogic/ProductLogic.cs index 04b07b0..ba3cb3b 100644 --- a/Course/BusinessLogic/BusinessLogic/ProductLogic.cs +++ b/Course/BusinessLogic/BusinessLogic/ProductLogic.cs @@ -62,6 +62,10 @@ namespace BusinessLogic.BusinessLogic { throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Cost)); } + if (model.ProductDetails.Count == 0) + { + throw new ArgumentNullException("Количество деталей в изделии должно быть больше 0", nameof(model.ProductDetails)); + } _logger.LogInformation("Product. ProductName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id); var elem = _productStorage.GetElement(new ProductSearchModel { diff --git a/Course/Contracts/BindingModels/ProductionBindingModel.cs b/Course/Contracts/BindingModels/ProductionBindingModel.cs index 8fee8ac..5e76313 100644 --- a/Course/Contracts/BindingModels/ProductionBindingModel.cs +++ b/Course/Contracts/BindingModels/ProductionBindingModel.cs @@ -8,7 +8,7 @@ namespace Contracts.BindingModels public int UserId { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } - public Dictionary ProductionDetails { get; set; } = new(); + public Dictionary ProductionDetails { get; set; } = new(); } } diff --git a/Course/DatabaseImplement/Implements/ProductionStorage.cs b/Course/DatabaseImplement/Implements/ProductionStorage.cs index 9c3dd90..fb1ab2f 100644 --- a/Course/DatabaseImplement/Implements/ProductionStorage.cs +++ b/Course/DatabaseImplement/Implements/ProductionStorage.cs @@ -1,4 +1,4 @@ -using Contracts.BindingModels; + using Contracts.BindingModels; using Contracts.SearchModels; using Contracts.StoragesContracts; using Contracts.ViewModels; @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore; namespace DatabaseImplement.Implements { - public class ProductionionStorage : IProductionStorage + public class ProductionStorage : IProductionStorage { public ProductionViewModel? Delete(ProductionBindingModel model) { @@ -15,6 +15,7 @@ namespace DatabaseImplement.Implements var newProduction = context.Productions.FirstOrDefault(x => x.Id == model.Id); if (newProduction == null) return null; + newProduction.UpdateDetails(context, model); context.Productions.Remove(newProduction); context.SaveChanges(); return newProduction.GetViewModel; @@ -60,6 +61,7 @@ namespace DatabaseImplement.Implements if (newProduction == null) return null; newProduction.Update(model); + newProduction.UpdateDetails(context, model); context.SaveChanges(); return newProduction.GetViewModel; } diff --git a/Course/DatabaseImplement/Models/Production.cs b/Course/DatabaseImplement/Models/Production.cs index 6dccffe..86e83db 100644 --- a/Course/DatabaseImplement/Models/Production.cs +++ b/Course/DatabaseImplement/Models/Production.cs @@ -68,7 +68,7 @@ namespace DatabaseImplement.Models var productionDetails = context.DetailProductions.Where(rec => rec.ProductionId == model.Id).ToList(); if (productionDetails != null && productionDetails.Count > 0) { - context.DetailProductions.RemoveRange(productionDetails.Where(rec => !model.ProductionDetails.ContainsKey(rec.DetailId))); + context.DetailProductions.RemoveRange(productionDetails.Where(rec => model.ProductionDetails.ContainsKey(rec.ProductionId))); context.SaveChanges(); } var production = context.Productions.First(x => x.Id == model.Id); diff --git a/Course/TestingDatabase/Program.cs b/Course/TestingDatabase/Program.cs index cd9d0a8..bd22dc9 100644 --- a/Course/TestingDatabase/Program.cs +++ b/Course/TestingDatabase/Program.cs @@ -10,8 +10,16 @@ using DataModels.Models; DetailStorage detailStorage = new DetailStorage(); ImplementerStorage implementationStorage = new ImplementerStorage(); -ProductionionStorage productionionStorage = new ProductionionStorage(); +ProductionStorage productionionStorage = new ProductionStorage(); ProductStorage productStorage = new ProductStorage(); -var i = productStorage.GetFullList(); -Console.WriteLine(i); \ No newline at end of file +productionionStorage.Insert(new() +{ + UserId = 1, + Name = "Test", + Cost = 1, + ProductionDetails = new() + { + {2, detailStorage.GetElement(new() { Id = 2,}) }, {3, detailStorage.GetElement(new() {Id = 3})} + } +}); \ No newline at end of file