Guarantor: Models/Logic/ReportViews fixes.

This commit is contained in:
Yuee Shiness 2023-04-07 20:03:44 +04:00
parent ee3f2ff447
commit 81454cabe4
14 changed files with 55 additions and 42 deletions

View File

@ -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
{

View File

@ -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<ReportRequestComponentViewModel> GetPCs()
{
var pcs = _pcStorage.GetFullList();
var list = new List<ReportRequestComponentViewModel>();
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<int>(),
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<ReportConsignmentComponentViewModel> GetProducts()
public List<ReportProductComponentViewModel> GetProducts()
{
var products = _productStorage.GetFullList();
var list = new List<ReportConsignmentComponentViewModel>();
var list = new List<ReportProductComponentViewModel>();
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<int>(),
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;
}

View File

@ -15,7 +15,6 @@ namespace ComputerStoreContracts.BindingModels
public double Price { get; set; }
public int EmployeeID { get; set; }
public int ConsignmentID { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new();
}

View File

@ -10,7 +10,7 @@ namespace ComputerStoreContracts.BusinessLogicContracts
{
public interface IReportLogic
{
List<ReportConsignmentComponentViewModel> GetProducts();
List<ReportProductComponentViewModel> GetProducts();
List<ReportRequestComponentViewModel> GetPCs();
void SaveProductsToExcelFile(ReportBindingModel model);

View File

@ -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; }
}
}

View File

@ -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<int, (IComponentModel, int)> ProductComponents { get; set; } = new();

View File

@ -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<int> Consignments { get; set; } = new List<int>();
}
}

View File

@ -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<int> Requests { get; set; } = new List<int> ();
}
}

View File

@ -11,7 +11,6 @@ namespace ComputerStoreDataModels.Models
public string Name { get; }
public double Price { get; }
public int EmployeeID { get; }
public int ConsignmentID { get; }
Dictionary<int,(IComponentModel,int)> ProductComponents { get; }
}
}

View File

@ -24,6 +24,6 @@ namespace ComputerStoreDatabaseImplement
public virtual DbSet<PC> PCs { get; set; }
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<RequestComponent> RequestComponents { get; set; }
public virtual DbSet<ConsignmentComponent> ConsignmentComponents { get; set; }
public virtual DbSet<ProductComponent> ConsignmentComponents { get; set; }
}
}

View File

@ -23,7 +23,7 @@ namespace ComputerStoreDatabaseImplement.Models
public double Price { get; private set; }
[ForeignKey("ComponentID")]
public virtual List<ConsignmentComponent> ConsignmentComponents { get; private set; } = new();
public virtual List<ProductComponent> ConsignmentComponents { get; private set; } = new();
[ForeignKey("ComponentID")]
public virtual List<RequestComponent> RequestComponents { get; private set; } = new();

View File

@ -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();
}

View File

@ -25,26 +25,27 @@ namespace ComputerStoreDatabaseImplement.Models
[Required]
public int EmployeeID { get; private set; }
[Required]
public int ConsignmentID { get; private set; }
private Dictionary<int, (IComponentModel, int)>? _consignmentComponents = null;
private Dictionary<int, (IComponentModel, int)>? _productComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> 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<ConsignmentComponent> Components { get; set; } = new();
public virtual List<ProductComponent> Components { get; set; } = new();
[ForeignKey("ProductID")]
public virtual List<Consignment> 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;
}
}

View File

@ -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();
}
}