CourseWork_CompShop/ComputerShopProvider/ComputerShopDatabaseImplement/Models/Assembly.cs

112 lines
4.3 KiB
C#
Raw Normal View History

2023-04-05 19:08:51 +04:00
using ComputerShopDataModels.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
namespace ComputerShopDatabaseImplement.Models
{
internal class Assembly : IAssemblyModel
{
public int Id { get; set; }
[Required]
public string AssemblyName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
2023-04-09 11:30:54 +04:00
[Required]
public int ClientId { get; private set; }
private Dictionary<int, (IComponentModel, int)>? _assemblyComponents = null;
2023-04-05 19:08:51 +04:00
[NotMapped]
public Dictionary<int, (IComponentModel, int)> AssemblyComponents
{
get
{
if (_assemblyComponents == null)
{
_assemblyComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _assemblyComponents;
}
}
2023-05-18 18:27:12 +04:00
private Dictionary<int, (IOrderModel, int)>? _assemblyOrders = null;
2023-04-05 19:08:51 +04:00
[ForeignKey("AssemblyId")]
public virtual List<AssemblyComponent> Components { get; set; } = new();
[ForeignKey("AssemblyId")]
2023-04-07 16:53:49 +04:00
public virtual List<AssemblyOrder> Orders { get; set; } = new();
2023-04-09 11:30:54 +04:00
public virtual Client Client { get; set; }
2023-04-05 19:08:51 +04:00
public static Assembly Create(ComputerShopDatabase context, AssemblyBindingModel model)
{
return new Assembly()
{
Id = model.Id,
AssemblyName = model.AssemblyName,
Price = model.Price,
2023-04-09 11:30:54 +04:00
ClientId = model.ClientId,
2023-04-05 19:08:51 +04:00
Components = model.AssemblyComponents.Select(x => new
AssemblyComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
2023-05-18 18:33:33 +04:00
}).ToList(),
Orders = model.AssemblyOrders.Select(x => new
AssemblyOrder
{
Order = context.Orders.First(y => y.Id == x.Key),
Count = x.Value.Item2
2023-04-05 19:08:51 +04:00
}).ToList()
};
}
public void Update(AssemblyBindingModel model)
{
AssemblyName = model.AssemblyName;
Price = model.Price;
}
public AssemblyViewModel GetViewModel => new()
{
Id = Id,
AssemblyName = AssemblyName,
Price = Price,
2023-04-09 11:30:54 +04:00
AssemblyComponents = AssemblyComponents,
ClientId = ClientId
2023-04-05 19:08:51 +04:00
};
public void UpdateComponents(ComputerShopDatabase context, AssemblyBindingModel model)
{
2023-04-09 11:30:54 +04:00
var assemblyComponents = context.AssemblyComponents.Where(rec => rec.Id == model.Id).ToList();
2023-04-05 19:08:51 +04:00
if (assemblyComponents != null && assemblyComponents.Count > 0)
{ // удалили те, которых нет в модели
context.AssemblyComponents.RemoveRange(assemblyComponents.Where(rec
=> !model.AssemblyComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in assemblyComponents)
{
updateComponent.Count = model.AssemblyComponents[updateComponent.ComponentId].Item2;
model.AssemblyComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var assembly = context.Assemblies.First(x => x.Id == Id);
foreach (var pc in model.AssemblyComponents)
{
context.AssemblyComponents.Add(new AssemblyComponent
{
Assembly = assembly,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_assemblyComponents = null;
}
}
}