CourseWork_CompShop/ComputerShopProvider/ComputerShopDatabaseImplement/Models/Assembly.cs

101 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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; }
private Dictionary<int, (IComponentModel, int)>? _assemblyComponents =
null;
[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;
}
}
[ForeignKey("AssemblyId")]
public virtual List<AssemblyComponent> Components { get; set; } = new();
[ForeignKey("AssemblyId")]
public virtual List<AssemblyOrder> Orders { get; set; } = new();
public static Assembly Create(ComputerShopDatabase context, AssemblyBindingModel model)
{
return new Assembly()
{
Id = model.Id,
AssemblyName = model.AssemblyName,
Price = model.Price,
Components = model.AssemblyComponents.Select(x => new
AssemblyComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(AssemblyBindingModel model)
{
AssemblyName = model.AssemblyName;
Price = model.Price;
}
public AssemblyViewModel GetViewModel => new()
{
Id = Id,
AssemblyName = AssemblyName,
Price = Price,
AssemblyComponents = AssemblyComponents
};
public void UpdateComponents(ComputerShopDatabase context, AssemblyBindingModel model)
{
var assemblyComponents = context.AssemblyComponents.Where(rec =>
rec.Id == model.Id).ToList();
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;
}
}
}