diff --git a/ComputerStoreBusinessLogic/BusinessLogic/ReportLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/ReportLogic.cs new file mode 100644 index 0000000..0aa478a --- /dev/null +++ b/ComputerStoreBusinessLogic/BusinessLogic/ReportLogic.cs @@ -0,0 +1,78 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.BusinessLogicContracts; +using ComputerStoreContracts.StorageContracts; +using ComputerStoreContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreBusinessLogic.BusinessLogic +{ + public class ReportLogic : IReportLogic + { + private readonly IPCStorage _pcStorage; + private readonly IProductStorage _productStorage; + + public ReportLogic(IPCStorage pcStorage, IProductStorage productStorage) + { + _pcStorage = pcStorage; + _productStorage = productStorage; + } + + public List GetPCs() + { + var pcs = _pcStorage.GetFullList(); + var list = new List(); + foreach (var pc in pcs) + { + var record = new ReportRequestComponentViewModel + { + PCName = pc.Name, + Components = new List<(string Component, int count)>(), + TotalAmount = 0 + }; + foreach (var component in pc.PCComponents) + { + record.Components.Add(new(component.Value.Item1.Name, component.Value.Item2)); + record.TotalAmount += component.Value.Item2; + } + list.Add(record); + } + return list; + } + + public List GetProducts() + { + var products = _productStorage.GetFullList(); + var list = new List(); + foreach (var product in products) + { + var record = new ReportConsignmentComponentViewModel + { + ProductName = product.Name, + Components = new List<(string Component, int count)>(), + TotalAmount = 0 + }; + foreach (var component in product.ProductComponents) + { + record.Components.Add(new(component.Value.Item1.Name, component.Value.Item2)); + record.TotalAmount += component.Value.Item2; + } + list.Add(record); + } + return list; + } + + public void SaveProductsToExcelFile(ReportBindingModel model) + { + //will be implemented in the future! + } + + public void SavePCsToExcelFile(ReportBindingModel model) + { + //will be implemented in the future! + } + } +} diff --git a/ComputerStoreContracts/BindingModels/ReportBindingModel.cs b/ComputerStoreContracts/BindingModels/ReportBindingModel.cs new file mode 100644 index 0000000..3b099b0 --- /dev/null +++ b/ComputerStoreContracts/BindingModels/ReportBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreContracts.BindingModels +{ + public class ReportBindingModel + { + public string FileName { get; set; } = string.Empty; + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set;} + } +} diff --git a/ComputerStoreContracts/BusinessLogicContracts/IReportLogic.cs b/ComputerStoreContracts/BusinessLogicContracts/IReportLogic.cs new file mode 100644 index 0000000..413e26a --- /dev/null +++ b/ComputerStoreContracts/BusinessLogicContracts/IReportLogic.cs @@ -0,0 +1,19 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreContracts.BusinessLogicContracts +{ + public interface IReportLogic + { + List GetProducts(); + List GetPCs(); + + void SaveProductsToExcelFile(ReportBindingModel model); + void SavePCsToExcelFile(ReportBindingModel model); + } +} diff --git a/ComputerStoreContracts/SearchModels/PCSearchModel.cs b/ComputerStoreContracts/SearchModels/PCSearchModel.cs index bc9b3b4..3599813 100644 --- a/ComputerStoreContracts/SearchModels/PCSearchModel.cs +++ b/ComputerStoreContracts/SearchModels/PCSearchModel.cs @@ -11,5 +11,6 @@ namespace ComputerStoreContracts.SearchModels public int? ID { get; set; } public string? Name { get; set; } public int? RequestID { get; set; } + public int? EmployeeID { get; set; } } } diff --git a/ComputerStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerStoreContracts/SearchModels/ProductSearchModel.cs index 557bbee..a18f064 100644 --- a/ComputerStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerStoreContracts/SearchModels/ProductSearchModel.cs @@ -11,5 +11,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/ReportConsignmentComponentViewModel.cs b/ComputerStoreContracts/ViewModels/ReportConsignmentComponentViewModel.cs new file mode 100644 index 0000000..00104a8 --- /dev/null +++ b/ComputerStoreContracts/ViewModels/ReportConsignmentComponentViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreContracts.ViewModels +{ + public class ReportConsignmentComponentViewModel + { + 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)>(); + } +} diff --git a/ComputerStoreContracts/ViewModels/ReportRequestComponentViewModel.cs b/ComputerStoreContracts/ViewModels/ReportRequestComponentViewModel.cs new file mode 100644 index 0000000..f1cd4d3 --- /dev/null +++ b/ComputerStoreContracts/ViewModels/ReportRequestComponentViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerStoreContracts.ViewModels +{ + public class ReportRequestComponentViewModel + { + 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)> (); + } +} diff --git a/ComputerStoreDataModels/Models/IPCModel.cs b/ComputerStoreDataModels/Models/IPCModel.cs index b9504db..390e344 100644 --- a/ComputerStoreDataModels/Models/IPCModel.cs +++ b/ComputerStoreDataModels/Models/IPCModel.cs @@ -8,8 +8,8 @@ namespace ComputerStoreDataModels.Models { public interface IPCModel : IID { - string Name { get; } - double Price { get; } + public string Name { get; } + public double Price { get; } public int EmployeeID { get; } public int RequestID { get; } Dictionary PCComponents { get; } diff --git a/ComputerStoreDatabaseImplement/ComputerStoreDatabase.cs b/ComputerStoreDatabaseImplement/ComputerStoreDatabase.cs index a6c16e5..544f232 100644 --- a/ComputerStoreDatabaseImplement/ComputerStoreDatabase.cs +++ b/ComputerStoreDatabaseImplement/ComputerStoreDatabase.cs @@ -14,7 +14,7 @@ namespace ComputerStoreDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Server=localhost\SQLEXPRESS;Initial Catalog=DressAtelierDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Server=localhost\SQLEXPRESS;Initial Catalog=ComputerStoreDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs index 6d9a1ad..05c3060 100644 --- a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs @@ -20,17 +20,21 @@ namespace ComputerStoreDatabaseImplement.Implements if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; } using var context = new ComputerStoreDatabase(); - return context.PCs.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; + return context.PCs.Include(x => x.Employee).Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; } public List GetFilteredList(PCSearchModel model) { - if (string.IsNullOrEmpty(model.Name)) + if (string.IsNullOrEmpty(model.Name) && !model.EmployeeID.HasValue) { return new(); } using var context = new ComputerStoreDatabase(); + if(model.EmployeeID.HasValue) + { + return context.PCs.Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); + } return context.PCs.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ID == model.ID).ToList().Select(x => x.GetViewModel).ToList(); } diff --git a/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs index 746c44c..82b04b5 100644 --- a/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/ProductStorage.cs @@ -20,18 +20,22 @@ namespace ComputerStoreDatabaseImplement.Implements if(string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; } using var context = new ComputerStoreDatabase(); - return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; + return context.Products.Include(x => x.Employee).Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; } public List GetFilteredList(ProductSearchModel model) { - if(string.IsNullOrEmpty(model.Name)) + if(string.IsNullOrEmpty(model.Name) && !model.EmployeeID.HasValue) { return new(); } using var context = new ComputerStoreDatabase(); - return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ID ==model.ID).Select(x => x.GetViewModel).ToList(); + if(model.EmployeeID.HasValue) + { + return context.Products.Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); + } + return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ID ==model.ID).ToList().Select(x => x.GetViewModel).ToList(); } public List GetFullList()