Guarantor: PC and Product models were changed + database models.

This commit is contained in:
Yuee Shiness 2023-04-06 17:00:40 +04:00
parent 377a803ac1
commit e65286870e
19 changed files with 430 additions and 17 deletions

View File

@ -23,7 +23,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
}
public bool Create(EmployeeBindingModel model)
{
CheckUser(model);
CheckEmployee(model);
if (_employeeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
@ -34,7 +34,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
public bool Update(EmployeeBindingModel model)
{
CheckUser(model);
CheckEmployee(model);
if (_employeeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
@ -45,7 +45,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
public bool Delete(EmployeeBindingModel model)
{
CheckUser(model, false);
CheckEmployee(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_employeeStorage.Delete(model) == null)
{
@ -87,7 +87,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
return list;
}
public void CheckUser(EmployeeBindingModel model, bool withParams = true)
public void CheckEmployee(EmployeeBindingModel model, bool withParams = true)
{
if (model == null)
{
@ -106,7 +106,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
throw new ArgumentNullException("Invalid password of user", nameof(model.Password));
}
_logger.LogInformation("Client. Email:{ Email}. ID: { ID} ", model.Username, model.ID);
_logger.LogInformation("Client. Username:{ Username}. ID: { ID} ", model.Username, model.ID);
var element = _employeeStorage.GetElement(new EmployeeSearchModel
{

View File

@ -98,8 +98,10 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
if (!withParams) { return; }
if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Invalid PC's name", nameof(model)); }
if (model.Price <= 0) { throw new ArgumentNullException("Invalid PC's price", nameof(model)); }
if (string.IsNullOrEmpty(model.EmployeeID.ToString())) { throw new ArgumentNullException("Invalid PC's employee ID", nameof(model)); }
if (string.IsNullOrEmpty(model.RequestID.ToString())) { throw new ArgumentNullException("Invalid PC's request ID", nameof(model)); }
_logger.LogInformation("PC. PCName:{PCName}. Cost:{ Cost}. ID: { ID} ", model.Name, model.Price, model.ID);
_logger.LogInformation("PC. RequestID: {RequestID}. EmployeeID: {EmployeeID}. PCName:{PCName}. Cost:{ Cost}. ID: { ID} ",model.RequestID,model.EmployeeID, model.Name, model.Price, model.ID);
var element = _PCStorage.GetElement(new PCSearchModel
{

View File

@ -98,8 +98,10 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
if (!withParams) { return; }
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. product:{product}. Cost:{ Cost}. ID: { ID} ", model.Name, model.Price, model.ID);
_logger.LogInformation("Product. ConsignmentID: {ConsignmentID}. EmployeeID: {EmployeeID}. product:{product}. Cost:{ Cost}. ID: { ID} ",model.ConsignmentID,model.EmployeeID, model.Name, model.Price, model.ID);
var element = _productStorage.GetElement(new ProductSearchModel
{

View File

@ -14,10 +14,10 @@ namespace ComputerStoreContracts.BindingModels
public double Price { get; set; }
public int EmployeeID { get; set; }
public int RequestID { get; set; }
public Dictionary<int, (IComponentModel, int)> PCComponents { get; set; } = new();
}
}

View File

@ -14,8 +14,9 @@ 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,5 +10,6 @@ namespace ComputerStoreContracts.SearchModels
{
public int? ID { get; set; }
public string? Name { get; set; }
public int? RequestID { get; set; }
}
}

View File

@ -10,5 +10,6 @@ namespace ComputerStoreContracts.SearchModels
{
public int? ID { get; set; }
public string? Name { get; set; }
public int? ConsignmentID { get; set; }
}
}

View File

@ -18,9 +18,11 @@ namespace ComputerStoreContracts.ViewModels
[DisplayName("PC's price")]
public double Price { get; set; }
[DisplayName("Employee ID")]
public int EmployeeID { get; set; }
[DisplayName("Request ID")]
public int RequestID { get; set; }
public Dictionary<int, (IComponentModel, int)> PCComponents { get; set; } = new();

View File

@ -18,8 +18,14 @@ namespace ComputerStoreContracts.ViewModels
[DisplayName("Product's price")]
public double Price { get; set; }
[DisplayName("Employee ID")]
public int EmployeeID { get; set; }
[DisplayName("Consignment ID")]
public int ConsignmentID { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new();
}
}

View File

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

View File

@ -8,8 +8,10 @@ namespace ComputerStoreDataModels.Models
{
public interface IProductModel : IID
{
string Name { get; }
double Price { get; }
public string Name { get; }
public double Price { get; }
public int EmployeeID { get; }
public int ConsignmentID { get; }
Dictionary<int,(IComponentModel,int)> ProductComponents { get; }
}
}

View File

@ -0,0 +1,29 @@
using ComputerStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement
{
public class ComputerStoreDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Server=localhost\SQLEXPRESS;Initial Catalog=DressAtelierDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { get; set; }
public virtual DbSet<Product> Products { get; set; }
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; }
}
}

View File

@ -6,6 +6,15 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComputerStoreContracts\ComputerStoreContracts.csproj" />
<ProjectReference Include="..\ComputerStoreDataModels\ComputerStoreDataModels.csproj" />

View File

@ -0,0 +1,63 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int ID { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public double Price { get; private set; }
[ForeignKey("ComponentID")]
public virtual List<ConsignmentComponent> ConsignmentComponents { get; private set; } = new();
[ForeignKey("ComponentID")]
public virtual List<RequestComponent> RequestComponents { get; private set; } = new();
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
ID = model.ID,
Name = model.Name,
Price = model.Price
};
}
public void Update(ComponentBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
Price = model.Price;
}
public ComponentViewModel GetViewModel => new()
{
ID = ID,
Name = Name,
Price = Price
};
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Models
{
public class ConsignmentComponent
{
public int ID { get; set; }
[Required]
public int ConsignmentID { get; set; }
[Required]
public int ComponentID { get; set; }
[Required]
public int ProductID { get; set; }
[Required]
public int Count { get; set; }
public virtual Product Product { get; set; } = new();
public virtual Component Component { get; set; } = new();
public virtual Consignment Consignment { get; set; } = new();
}
}

View File

@ -23,8 +23,11 @@ namespace ComputerStoreDatabaseImplement.Models
public string? LastName { get; private set; } = string.Empty;
public string? MiddleName { get; private set; } = string.Empty;
//[ForeignKey("EmployeeID")]
//public virtual List<PC> PCs { get; set; } = new();
[ForeignKey("EmployeeID")]
public virtual List<PC> PCs { get; set; } = new();
[ForeignKey("EmployeeID")]
public virtual List<Product> Products { get; set; } = new();
public static Employee? Create(EmployeeBindingModel? model)
{

View File

@ -0,0 +1,115 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Models
{
public class PC : IPCModel
{
public int ID { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public double Price { get; private set; }
[Required]
public int EmployeeID { get; private set; }
[Required]
public int RequestID { get; private set; }
private Dictionary<int, (IComponentModel, int)>? _pcComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> PCComponents
{
get
{
if(_pcComponents == null)
{
_pcComponents = Components.ToDictionary(recPC => recPC.ComponentID, recPC => (recPC.Component as IComponentModel, recPC.Count));
}
return _pcComponents;
}
}
[ForeignKey("PCID")]
public virtual List<RequestComponent> Components { get; set; } = new();
public virtual Employee Employee { get; set; }
public static PC Create(ComputerStoreDatabase context, PCBindingModel model)
{
return new PC()
{
ID = model.ID,
Name = model.Name,
Price = model.Price,
EmployeeID = model.EmployeeID,
RequestID = model.RequestID,
Components = model.PCComponents.Select(x => new RequestComponent
{
Component = context.Components.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(PCBindingModel model)
{
Name = model.Name;
Price = model.Price;
}
public PCViewModel GetViewModel => new()
{
ID = ID,
Name = Name,
Price = Price,
EmployeeID = EmployeeID,
RequestID = RequestID,
PCComponents = PCComponents
};
public void UpdateComponents(ComputerStoreDatabase context, PCBindingModel model)
{
var pcComponents = context.RequestComponents.Where(rec => rec.PCID == model.ID && rec.RequestID == model.RequestID).ToList();
if(pcComponents != null && pcComponents.Count > 0)
{
context.RequestComponents.RemoveRange(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID));
context.SaveChanges();
foreach(var updateComponent in pcComponents)
{
updateComponent.Count = model.PCComponents[updateComponent.ID].Item2;
model.PCComponents.Remove(updateComponent.ID);
}
context.SaveChanges();
}
var pc = context.PCs.First(rec => model.ID == ID);
foreach(var pcc in model.PCComponents )
{
context.RequestComponents.Add(new RequestComponent
{
PC = pc,
Component = context.Components.First(x=> x.ID == pcc.Key),
Request = context.Requests.First(x => x.ID == model.RequestID),
Count = pcc.Value.Item2
});
context.SaveChanges();
}
_pcComponents = null;
}
}
}

View File

@ -0,0 +1,114 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Models
{
public class Product : IProductModel
{
public int ID { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[Required]
public int EmployeeID { get; set; }
[Required]
public int ConsignmentID { get; private set; }
private Dictionary<int, (IComponentModel, int)>? _consignmentComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> ProductComponents
{
get
{
if(_consignmentComponents == null)
{
_consignmentComponents = Components.ToDictionary(recPC => recPC.ComponentID, recPC => (recPC.Component as IComponentModel, recPC.Count));
}
return _consignmentComponents;
}
}
[ForeignKey("ProductID")]
public virtual List<ConsignmentComponent> Components { get; set; } = new();
public virtual Employee Employee { get; set; }
public static Product Create(ComputerStoreDatabase context, ProductBindingModel model)
{
return new Product()
{
ID = model.ID,
Name = model.Name,
Price = model.Price,
EmployeeID = model.EmployeeID,
ConsignmentID = model.ConsignmentID,
Components = model.ProductComponents.Select(x => new ConsignmentComponent
{
Component = context.Components.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ProductBindingModel model)
{
Name = model.Name;
Price = model.Price;
}
public ProductViewModel GetViewModel => new()
{
ID = ID,
Name = Name,
Price = Price,
EmployeeID = EmployeeID,
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();
if(productComponents != null && productComponents.Count > 0)
{
context.ConsignmentComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID) && rec.ConsignmentID == model.ConsignmentID));
context.SaveChanges();
foreach(var updateComponent in productComponents)
{
updateComponent.Count = model.ProductComponents[updateComponent.ID].Item2;
model.ProductComponents.Remove(updateComponent.ID);
}
context.SaveChanges();
}
var product = context.Products.First(x => x.ID == ID);
foreach(var pc in model.ProductComponents)
{
context.ConsignmentComponents.Add(new ConsignmentComponent
{
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;
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Models
{
public class RequestComponent
{
public int ID { get; set; }
[Required]
public int RequestID { get; set; }
[Required]
public int ComponentID { get; set; }
[Required]
public int PCID { get; set; }
[Required]
public int Count { get; set; }
public virtual PC PC { get; set; } = new();
public virtual Component Component { get; set; } = new();
public virtual Request Request { get; set; } = new();
}
}