Guarantor: ViewModels / Database storages fixes +ReportLogic and following implementation were created.

This commit is contained in:
Yuee Shiness 2023-04-07 13:09:55 +04:00
parent 81673b0767
commit a31e821be3
11 changed files with 160 additions and 8 deletions

View File

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

View File

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

View File

@ -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<ReportConsignmentComponentViewModel> GetProducts();
List<ReportRequestComponentViewModel> GetPCs();
void SaveProductsToExcelFile(ReportBindingModel model);
void SavePCsToExcelFile(ReportBindingModel model);
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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<int,(IComponentModel,int)> PCComponents { get; }

View File

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

View File

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

View File

@ -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<ProductViewModel> 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<ProductViewModel> GetFullList()