diff --git a/ComputerStoreBusinessLogic/BusinessLogic/ProductLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/ProductLogic.cs index cdbbf90..e7101fa 100644 --- a/ComputerStoreBusinessLogic/BusinessLogic/ProductLogic.cs +++ b/ComputerStoreBusinessLogic/BusinessLogic/ProductLogic.cs @@ -99,9 +99,8 @@ namespace ComputerStoreBusinessLogic.BusinessLogic if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Invalid product's name", nameof(model)); } if (model.Price <= 0) { throw new ArgumentNullException("Invalid product's price", nameof(model)); } if (string.IsNullOrEmpty(model.EmployeeID.ToString())) { throw new ArgumentNullException("Invalid Product's employee ID", nameof(model)); } - if (string.IsNullOrEmpty(model.ConsignmentID.ToString())) { throw new ArgumentNullException("Invalid Product's consignment ID", nameof(model)); } - _logger.LogInformation("Product. ConsignmentID: {ConsignmentID}. EmployeeID: {EmployeeID}. product:{product}. Cost:{ Cost}. ID: { ID} ",model.ConsignmentID,model.EmployeeID, model.Name, model.Price, model.ID); + _logger.LogInformation("Product. EmployeeID: {EmployeeID}. product:{product}. Cost:{ Cost}. ID: { ID} ",model.EmployeeID, model.Name, model.Price, model.ID); var element = _productStorage.GetElement(new ProductSearchModel { diff --git a/ComputerStoreBusinessLogic/BusinessLogic/ReportLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/ReportLogic.cs index 0aa478a..ceb389e 100644 --- a/ComputerStoreBusinessLogic/BusinessLogic/ReportLogic.cs +++ b/ComputerStoreBusinessLogic/BusinessLogic/ReportLogic.cs @@ -14,54 +14,78 @@ namespace ComputerStoreBusinessLogic.BusinessLogic { private readonly IPCStorage _pcStorage; private readonly IProductStorage _productStorage; + private readonly IConsignmentStorage _consignmentStorage; + private readonly IRequestStorage _requestStorage; - public ReportLogic(IPCStorage pcStorage, IProductStorage productStorage) + public ReportLogic(IPCStorage pcStorage, IProductStorage productStorage, IConsignmentStorage consignmentStorage, IRequestStorage requestStorage) { _pcStorage = pcStorage; _productStorage = productStorage; + _consignmentStorage = consignmentStorage; + _requestStorage = requestStorage; } public List GetPCs() { var pcs = _pcStorage.GetFullList(); var list = new List(); + var requests = _requestStorage.GetFullList(); foreach (var pc in pcs) { var record = new ReportRequestComponentViewModel { PCName = pc.Name, Components = new List<(string Component, int count)>(), + Requests = new List(), TotalAmount = 0 }; + + foreach (var request in requests) + { + record.Requests.Add(request.RequestProducts.FirstOrDefault(x => x.ID == pc.ID).Select(x => x.ID)); + } + foreach (var component in pc.PCComponents) { - record.Components.Add(new(component.Value.Item1.Name, component.Value.Item2)); - record.TotalAmount += component.Value.Item2; + record.Components.Add(new(component.Value.Item1.Name, component.Value.Item2)); } + record.TotalAmount = record.Requests.Count; list.Add(record); } return list; } - public List GetProducts() + public List GetProducts() { var products = _productStorage.GetFullList(); - var list = new List(); + var list = new List(); + var consignments = _consignmentStorage.GetFullList(); + + foreach (var product in products) { - var record = new ReportConsignmentComponentViewModel + var record = new ReportProductComponentViewModel { ProductName = product.Name, Components = new List<(string Component, int count)>(), + Consignments = new List(), TotalAmount = 0 }; + + foreach (var consignment in consignments) + { + record.Consignments.Add(consignment.ConsignmentProducts.FirstOrDefault(x => x.ID == product.ID).Select(x => x.ID)); + } + foreach (var component in product.ProductComponents) { record.Components.Add(new(component.Value.Item1.Name, component.Value.Item2)); - record.TotalAmount += component.Value.Item2; } + record.TotalAmount = record.Consignments.Count; list.Add(record); } + + return list; } diff --git a/ComputerStoreContracts/BindingModels/ProductBindingModel.cs b/ComputerStoreContracts/BindingModels/ProductBindingModel.cs index b93a962..aab0fd9 100644 --- a/ComputerStoreContracts/BindingModels/ProductBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/ProductBindingModel.cs @@ -15,7 +15,6 @@ namespace ComputerStoreContracts.BindingModels public double Price { get; set; } public int EmployeeID { get; set; } - public int ConsignmentID { get; set; } public Dictionary ProductComponents { get; set; } = new(); } diff --git a/ComputerStoreContracts/BusinessLogicContracts/IReportLogic.cs b/ComputerStoreContracts/BusinessLogicContracts/IReportLogic.cs index 413e26a..be65c47 100644 --- a/ComputerStoreContracts/BusinessLogicContracts/IReportLogic.cs +++ b/ComputerStoreContracts/BusinessLogicContracts/IReportLogic.cs @@ -10,7 +10,7 @@ namespace ComputerStoreContracts.BusinessLogicContracts { public interface IReportLogic { - List GetProducts(); + List GetProducts(); List GetPCs(); void SaveProductsToExcelFile(ReportBindingModel model); diff --git a/ComputerStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerStoreContracts/SearchModels/ProductSearchModel.cs index a18f064..2a78115 100644 --- a/ComputerStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerStoreContracts/SearchModels/ProductSearchModel.cs @@ -10,7 +10,6 @@ namespace ComputerStoreContracts.SearchModels { public int? ID { get; set; } public string? Name { get; set; } - public int? ConsignmentID { get; set; } public int? EmployeeID { get; set; } } } diff --git a/ComputerStoreContracts/ViewModels/ProductViewModel.cs b/ComputerStoreContracts/ViewModels/ProductViewModel.cs index 720c0b4..81c6286 100644 --- a/ComputerStoreContracts/ViewModels/ProductViewModel.cs +++ b/ComputerStoreContracts/ViewModels/ProductViewModel.cs @@ -23,9 +23,6 @@ namespace ComputerStoreContracts.ViewModels [DisplayName("Employee's username")] public string EmployeeUsername { get; set; } = string.Empty; - [DisplayName("Consignment ID")] - public int ConsignmentID { get; set; } - public Dictionary ProductComponents { get; set; } = new(); diff --git a/ComputerStoreContracts/ViewModels/ReportConsignmentComponentViewModel.cs b/ComputerStoreContracts/ViewModels/ReportProductComponentViewModel.cs similarity index 77% rename from ComputerStoreContracts/ViewModels/ReportConsignmentComponentViewModel.cs rename to ComputerStoreContracts/ViewModels/ReportProductComponentViewModel.cs index 00104a8..10d16f5 100644 --- a/ComputerStoreContracts/ViewModels/ReportConsignmentComponentViewModel.cs +++ b/ComputerStoreContracts/ViewModels/ReportProductComponentViewModel.cs @@ -6,10 +6,12 @@ using System.Threading.Tasks; namespace ComputerStoreContracts.ViewModels { - public class ReportConsignmentComponentViewModel + public class ReportProductComponentViewModel { public string ProductName { get; set; } = string.Empty; public int TotalAmount { get; set; } public List<(string Component, int count)> Components { get; set; } = new List<(string Component, int count)>(); + + public List Consignments { get; set; } = new List(); } } diff --git a/ComputerStoreContracts/ViewModels/ReportRequestComponentViewModel.cs b/ComputerStoreContracts/ViewModels/ReportRequestComponentViewModel.cs index f1cd4d3..22c1b4b 100644 --- a/ComputerStoreContracts/ViewModels/ReportRequestComponentViewModel.cs +++ b/ComputerStoreContracts/ViewModels/ReportRequestComponentViewModel.cs @@ -11,5 +11,6 @@ namespace ComputerStoreContracts.ViewModels public string PCName { get; set; } = string.Empty; public int TotalAmount { get; set; } public List<(string Component, int count)> Components { get; set; } = new List<(string Component, int count)> (); + public List Requests { get; set; } = new List (); } } diff --git a/ComputerStoreDataModels/Models/IProductModel.cs b/ComputerStoreDataModels/Models/IProductModel.cs index 56b94ff..5399350 100644 --- a/ComputerStoreDataModels/Models/IProductModel.cs +++ b/ComputerStoreDataModels/Models/IProductModel.cs @@ -11,7 +11,6 @@ namespace ComputerStoreDataModels.Models public string Name { get; } public double Price { get; } public int EmployeeID { get; } - public int ConsignmentID { get; } Dictionary ProductComponents { get; } } } diff --git a/ComputerStoreDatabaseImplement/ComputerStoreDatabase.cs b/ComputerStoreDatabaseImplement/ComputerStoreDatabase.cs index 544f232..ba8786f 100644 --- a/ComputerStoreDatabaseImplement/ComputerStoreDatabase.cs +++ b/ComputerStoreDatabaseImplement/ComputerStoreDatabase.cs @@ -24,6 +24,6 @@ namespace ComputerStoreDatabaseImplement public virtual DbSet PCs { get; set; } public virtual DbSet Employees { get; set; } public virtual DbSet RequestComponents { get; set; } - public virtual DbSet ConsignmentComponents { get; set; } + public virtual DbSet ConsignmentComponents { get; set; } } } diff --git a/ComputerStoreDatabaseImplement/Models/Component.cs b/ComputerStoreDatabaseImplement/Models/Component.cs index 4a30638..02f5bc0 100644 --- a/ComputerStoreDatabaseImplement/Models/Component.cs +++ b/ComputerStoreDatabaseImplement/Models/Component.cs @@ -23,7 +23,7 @@ namespace ComputerStoreDatabaseImplement.Models public double Price { get; private set; } [ForeignKey("ComponentID")] - public virtual List ConsignmentComponents { get; private set; } = new(); + public virtual List ConsignmentComponents { get; private set; } = new(); [ForeignKey("ComponentID")] public virtual List RequestComponents { get; private set; } = new(); diff --git a/ComputerStoreDatabaseImplement/Models/PC.cs b/ComputerStoreDatabaseImplement/Models/PC.cs index 62fb725..56d6eea 100644 --- a/ComputerStoreDatabaseImplement/Models/PC.cs +++ b/ComputerStoreDatabaseImplement/Models/PC.cs @@ -91,8 +91,8 @@ namespace ComputerStoreDatabaseImplement.Models context.SaveChanges(); foreach(var updateComponent in pcComponents) { - updateComponent.Count = model.PCComponents[updateComponent.ID].Item2; - model.PCComponents.Remove(updateComponent.ID); + updateComponent.Count = model.PCComponents[updateComponent.PCID].Item2; + model.PCComponents.Remove(updateComponent.PCID); } context.SaveChanges(); } diff --git a/ComputerStoreDatabaseImplement/Models/Product.cs b/ComputerStoreDatabaseImplement/Models/Product.cs index ed957fc..4591788 100644 --- a/ComputerStoreDatabaseImplement/Models/Product.cs +++ b/ComputerStoreDatabaseImplement/Models/Product.cs @@ -25,26 +25,27 @@ namespace ComputerStoreDatabaseImplement.Models [Required] public int EmployeeID { get; private set; } - [Required] - public int ConsignmentID { get; private set; } - private Dictionary? _consignmentComponents = null; + private Dictionary? _productComponents = null; [NotMapped] public Dictionary ProductComponents { get { - if(_consignmentComponents == null) + if(_productComponents == null) { - _consignmentComponents = Components.ToDictionary(recPC => recPC.ComponentID, recPC => (recPC.Component as IComponentModel, recPC.Count)); + _productComponents = Components.ToDictionary(recPC => recPC.ComponentID, recPC => (recPC.Component as IComponentModel, recPC.Count)); } - return _consignmentComponents; + return _productComponents; } } [ForeignKey("ProductID")] - public virtual List Components { get; set; } = new(); + public virtual List Components { get; set; } = new(); + + [ForeignKey("ProductID")] + public virtual List Consignments { get; set; } = new(); public virtual Employee Employee { get; set; } public static Product Create(ComputerStoreDatabase context, ProductBindingModel model) @@ -55,8 +56,7 @@ namespace ComputerStoreDatabaseImplement.Models Name = model.Name, Price = model.Price, EmployeeID = model.EmployeeID, - ConsignmentID = model.ConsignmentID, - Components = model.ProductComponents.Select(x => new ConsignmentComponent + Components = model.ProductComponents.Select(x => new ProductComponent { Component = context.Components.First(y => y.ID == x.Key), Count = x.Value.Item2 @@ -77,16 +77,15 @@ namespace ComputerStoreDatabaseImplement.Models Price = Price, EmployeeID = EmployeeID, EmployeeUsername = Employee.Username, - ConsignmentID = ConsignmentID, ProductComponents = ProductComponents }; public void UpdateComponents(ComputerStoreDatabase context, ProductBindingModel model) { - var productComponents = context.ConsignmentComponents.Where(rec => rec.ProductID == model.ID && rec.ConsignmentID == model.ConsignmentID).ToList(); + var productComponents = context.ConsignmentComponents.Where(rec => rec.ProductID == model.ID).ToList(); if(productComponents != null && productComponents.Count > 0) { - context.ConsignmentComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID) && rec.ConsignmentID == model.ConsignmentID)); + context.ConsignmentComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID))); context.SaveChanges(); foreach(var updateComponent in productComponents) { @@ -99,16 +98,15 @@ namespace ComputerStoreDatabaseImplement.Models var product = context.Products.First(x => x.ID == ID); foreach(var pc in model.ProductComponents) { - context.ConsignmentComponents.Add(new ConsignmentComponent + context.ConsignmentComponents.Add(new ProductComponent { Product = product, Component = context.Components.First(x => x.ID == pc.Key), - Consignment = context.Consignments.First(x => x.ID == model.ConsignmentID), Count = pc.Value.Item2 }); context.SaveChanges(); } - _consignmentComponents = null; + _productComponents = null; } } diff --git a/ComputerStoreDatabaseImplement/Models/ConsignmentComponent.cs b/ComputerStoreDatabaseImplement/Models/ProductComponent.cs similarity index 76% rename from ComputerStoreDatabaseImplement/Models/ConsignmentComponent.cs rename to ComputerStoreDatabaseImplement/Models/ProductComponent.cs index 996cae0..c6eb408 100644 --- a/ComputerStoreDatabaseImplement/Models/ConsignmentComponent.cs +++ b/ComputerStoreDatabaseImplement/Models/ProductComponent.cs @@ -7,13 +7,10 @@ using System.Threading.Tasks; namespace ComputerStoreDatabaseImplement.Models { - public class ConsignmentComponent + public class ProductComponent { public int ID { get; set; } - [Required] - public int ConsignmentID { get; set; } - [Required] public int ComponentID { get; set; } @@ -25,8 +22,6 @@ namespace ComputerStoreDatabaseImplement.Models public virtual Product Product { get; set; } = new(); public virtual Component Component { get; set; } = new(); - public virtual Consignment Consignment { get; set; } = new(); - } }