2024-04-25 00:58:28 +04:00
|
|
|
|
using ComputerShopContracts.BindingModels;
|
2024-04-30 23:48:10 +04:00
|
|
|
|
using ComputerShopContracts.ViewModels;
|
2024-04-25 00:58:28 +04:00
|
|
|
|
using ComputerShopDataModels.Models;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
|
|
|
|
|
namespace ComputerShopDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Assembly : IAssemblyModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public int UserId { get; private set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string AssemblyName { get; private set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
2024-05-01 13:56:07 +04:00
|
|
|
|
public double Price { get; private set; }
|
2024-04-25 00:58:28 +04:00
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string Category { get; private set; } = string.Empty;
|
|
|
|
|
|
2024-04-30 23:03:49 +04:00
|
|
|
|
[ForeignKey("AssemblyId")]
|
2024-04-30 23:14:08 +04:00
|
|
|
|
public virtual List<Request> Requests { get; set; } = new();
|
2024-04-25 00:58:28 +04:00
|
|
|
|
|
2024-04-30 23:03:49 +04:00
|
|
|
|
[ForeignKey("AssemblyId")]
|
2024-04-30 23:14:08 +04:00
|
|
|
|
public virtual List<AssemblyComponent> Components { get; set; } = new();
|
2024-04-30 23:03:49 +04:00
|
|
|
|
|
2024-04-25 00:58:28 +04:00
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _assemblyComponents;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IComponentModel, int)> AssemblyComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_assemblyComponents == null)
|
|
|
|
|
{
|
|
|
|
|
_assemblyComponents = Components.ToDictionary(
|
|
|
|
|
AsmComp => AsmComp.ComponentId,
|
|
|
|
|
AsmComp => (AsmComp.Component as IComponentModel, AsmComp.Count)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _assemblyComponents;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Assembly Create(ComputerShopDatabase Context, AssemblyBindingModel Model)
|
|
|
|
|
{
|
|
|
|
|
return new()
|
|
|
|
|
{
|
|
|
|
|
Id = Model.Id,
|
|
|
|
|
UserId = Model.UserId,
|
|
|
|
|
AssemblyName = Model.AssemblyName,
|
2024-05-01 13:56:07 +04:00
|
|
|
|
Price = Model.Price,
|
2024-04-25 00:58:28 +04:00
|
|
|
|
Category = Model.Category,
|
|
|
|
|
Components = Model.AssemblyComponents.Select(x => new AssemblyComponent
|
|
|
|
|
{
|
|
|
|
|
Component = Context.Components.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:48:10 +04:00
|
|
|
|
public void Update(AssemblyBindingModel Model)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(Model.AssemblyName))
|
|
|
|
|
{
|
|
|
|
|
AssemblyName = Model.AssemblyName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(Model.Category))
|
|
|
|
|
{
|
|
|
|
|
Category = Model.Category;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 13:56:07 +04:00
|
|
|
|
Price = Model.Price;
|
2024-04-30 23:48:10 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 00:41:26 +04:00
|
|
|
|
public AssemblyViewModel ViewModel => new()
|
2024-04-30 23:48:10 +04:00
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
UserId = UserId,
|
|
|
|
|
AssemblyName = AssemblyName,
|
2024-05-01 13:56:07 +04:00
|
|
|
|
Price = Price,
|
2024-04-30 23:48:10 +04:00
|
|
|
|
Category = Category,
|
|
|
|
|
AssemblyComponents = AssemblyComponents,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model)
|
|
|
|
|
{
|
|
|
|
|
var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList();
|
|
|
|
|
if (AssemblyComponents != null && AssemblyComponents.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
Context.AssemblyComponents
|
|
|
|
|
.RemoveRange(AssemblyComponents
|
|
|
|
|
.Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId)));
|
|
|
|
|
Context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
foreach (var ComponentToUpdate in AssemblyComponents)
|
|
|
|
|
{
|
|
|
|
|
ComponentToUpdate.Count = Model.AssemblyComponents[ComponentToUpdate.ComponentId].Item2;
|
|
|
|
|
Model.AssemblyComponents.Remove(ComponentToUpdate.ComponentId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var CurrentAssembly = Context.Assemblies.First(x => x.Id == Id);
|
|
|
|
|
foreach (var AssemblyComponent in Model.AssemblyComponents)
|
|
|
|
|
{
|
|
|
|
|
Context.AssemblyComponents.Add(new AssemblyComponent
|
|
|
|
|
{
|
|
|
|
|
Assembly = CurrentAssembly,
|
|
|
|
|
Component = Context.Components.First(x => x.Id == AssemblyComponent.Key),
|
|
|
|
|
Count = AssemblyComponent.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_assemblyComponents = null;
|
|
|
|
|
}
|
2024-04-25 00:58:28 +04:00
|
|
|
|
}
|
|
|
|
|
}
|