Guarantor: PC and Product models were changed + database models.
This commit is contained in:
63
ComputerStoreDatabaseImplement/Models/Component.cs
Normal file
63
ComputerStoreDatabaseImplement/Models/Component.cs
Normal 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
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
115
ComputerStoreDatabaseImplement/Models/PC.cs
Normal file
115
ComputerStoreDatabaseImplement/Models/PC.cs
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
114
ComputerStoreDatabaseImplement/Models/Product.cs
Normal file
114
ComputerStoreDatabaseImplement/Models/Product.cs
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
30
ComputerStoreDatabaseImplement/Models/RequestComponent.cs
Normal file
30
ComputerStoreDatabaseImplement/Models/RequestComponent.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user