94 lines
3.7 KiB
C#
94 lines
3.7 KiB
C#
using ComputersShopDataModels.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;
|
||
using ComputersShopContracts.BindingModels;
|
||
using ComputersShopContracts.ViewModels;
|
||
|
||
namespace ComputersShopDatabaseImplements.Models
|
||
{
|
||
public class Computer: IComputerModel
|
||
{
|
||
public int Id { get; set; }
|
||
[Required]
|
||
public string ComputerName { get; set; } = string.Empty;
|
||
[Required]
|
||
public double Price { get; set; }
|
||
private Dictionary<int, (IComponentModel, int)>? _computerComponents = null;
|
||
[NotMapped]
|
||
public Dictionary<int, (IComponentModel, int)> ComputerComponents
|
||
{
|
||
get
|
||
{
|
||
if (_computerComponents == null)
|
||
{
|
||
_computerComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
||
(recPC.Component as IComponentModel, recPC.Count));
|
||
}
|
||
return _computerComponents;
|
||
}
|
||
}
|
||
[ForeignKey("ComputerId")]
|
||
public virtual List<ComputerComponent> Components { get; set; } = new();
|
||
[ForeignKey("ComputerId")]
|
||
public virtual List<Order> Orders { get; set; } = new();
|
||
public static Computer Create(ComputersShopDatabase context, ComputerBindingModel model)
|
||
{
|
||
return new Computer()
|
||
{
|
||
Id = model.Id,
|
||
ComputerName = model.ComputerName,
|
||
Price = model.Price,
|
||
Components = model.ComputerComponents.Select(x => new ComputerComponent
|
||
{
|
||
Component = context.Components.First(y => y.Id == x.Key),
|
||
Count = x.Value.Item2
|
||
}).ToList()
|
||
};
|
||
}
|
||
public void Update(ComputerBindingModel model)
|
||
{
|
||
ComputerName = model.ComputerName;
|
||
Price = model.Price;
|
||
}
|
||
public ComputerViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
ComputerName = ComputerName,
|
||
Price = Price,
|
||
ComputerComponents = ComputerComponents
|
||
};
|
||
public void UpdateComponents(ComputersShopDatabase context, ComputerBindingModel model)
|
||
{
|
||
var computerComponents = context.ComputerComponents.Where(rec => rec.ComputerId == model.Id).ToList();
|
||
if (computerComponents != null && computerComponents.Count > 0)
|
||
{ // удалили те, которых нет в модели
|
||
context.ComputerComponents.RemoveRange(computerComponents.Where(rec => !model.ComputerComponents.ContainsKey(rec.ComponentId)));
|
||
context.SaveChanges();
|
||
// обновили количество у существующих записей
|
||
foreach (var updateComponent in computerComponents)
|
||
{
|
||
updateComponent.Count = model.ComputerComponents[updateComponent.ComponentId].Item2;
|
||
model.ComputerComponents.Remove(updateComponent.ComponentId);
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
var computer = context.Computers.First(x => x.Id == Id);
|
||
foreach (var pc in model.ComputerComponents)
|
||
{
|
||
context.ComputerComponents.Add(new ComputerComponent
|
||
{
|
||
Computer = computer,
|
||
Component = context.Components.First(x => x.Id == pc.Key),
|
||
Count = pc.Value.Item2
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_computerComponents = null;
|
||
}
|
||
}
|
||
} |