Coursework_ComputerStore_Li.../ComputerStoreDatabaseImplement/Models/PC.cs

113 lines
3.8 KiB
C#
Raw Normal View History

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]
2023-05-18 02:56:09 +04:00
public int RequestID { get; private set; }
[NotMapped]
2023-05-18 02:56:09 +04:00
public Dictionary<int, (IComponentModel, int)> PCComponents { get; private set; } = new();
public virtual Employee Employee { get; set; }
2023-05-18 02:56:09 +04:00
public virtual Request Request { get; set; }
public static PC Create(ComputerStoreDatabase context, PCBindingModel model)
2023-05-17 17:57:25 +04:00
{
return new PC()
{
ID = model.ID,
Name = model.Name,
Price = model.Price,
EmployeeID = model.EmployeeID,
RequestID = model.RequestID,
2023-05-18 02:56:09 +04:00
PCComponents = model.PCComponents
};
}
2023-05-17 17:57:25 +04:00
public static void EnterPCID(ComputerStoreDatabase context, PC model)
{
foreach(var rc in context.RequestComponents.Where(x => x.RequestID == model.RequestID))
{
rc.PCID = model.ID;
}
context.SaveChanges();
}
public void Update(PCBindingModel model)
{
Name = model.Name;
Price = model.Price;
2023-05-18 02:56:09 +04:00
PCComponents = model.PCComponents;
}
public PCViewModel GetViewModel => new()
{
ID = ID,
Name = Name,
Price = Price,
EmployeeID = EmployeeID,
EmployeeUsername = Employee.Username,
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)
{
2023-05-17 17:57:25 +04:00
if(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID).Any())
{
context.RequestComponents.RemoveRange(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID));
context.SaveChanges();
}
2023-05-17 18:34:28 +04:00
2023-05-17 18:36:32 +04:00
foreach(var updateComponent in pcComponents.Where(x => model.PCComponents.ContainsKey(x.ComponentID) && x.RequestID == model.RequestID))
{
2023-05-17 17:57:25 +04:00
updateComponent.Count = model.PCComponents[updateComponent.ComponentID].Item2;
model.PCComponents.Remove(updateComponent.ComponentID);
}
context.SaveChanges();
}
foreach(var pcc in model.PCComponents )
{
context.RequestComponents.Add(new RequestComponent
{
Component = context.Components.First(x=> x.ID == pcc.Key),
Request = context.Requests.First(x => x.ID == model.RequestID),
Count = pcc.Value.Item2
});
context.SaveChanges();
}
2023-05-18 02:56:09 +04:00
}
}
}