115 lines
3.9 KiB
C#
115 lines
3.9 KiB
C#
using HardwareShopContracts.BindingModels;
|
||
using HardwareShopContracts.ViewModels;
|
||
using HardwareShopDataModels.Models;
|
||
using System.ComponentModel;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
|
||
namespace HardwareShopDatabaseImplement.Models
|
||
{
|
||
public class Build : IBuildModel
|
||
{
|
||
public int Id { get; set; }
|
||
|
||
[Required]
|
||
public decimal Price { get; set; }
|
||
|
||
[Required]
|
||
public string BuildName { get; set; } = string.Empty;
|
||
|
||
[Required]
|
||
public int UserID { get; set; }
|
||
|
||
public virtual User User { get; set; }
|
||
|
||
[ForeignKey("BuildId")]
|
||
public virtual List<Comment>? Comments { get; set; }
|
||
|
||
[ForeignKey("BuildId")]
|
||
public virtual List<BuildComponent>? Components { get; set; }
|
||
|
||
|
||
[ForeignKey("BuildId")]
|
||
public virtual List<PurchaseBuild>? Purchases { get; set; }
|
||
|
||
|
||
private Dictionary<int, (IComponentModel, int)>? _buildComponents = null;
|
||
|
||
[NotMapped]
|
||
public Dictionary<int, (IComponentModel, int)> BuildComponents
|
||
{
|
||
get
|
||
{
|
||
if (_buildComponents == null)
|
||
{
|
||
_buildComponents = Components.ToDictionary(recBC => recBC.ComponentId, recBC => (recBC.Component as IComponentModel, recBC.Count));
|
||
}
|
||
return _buildComponents;
|
||
}
|
||
}
|
||
|
||
public static Build Create(HardwareShopDatabase context, BuildBindingModel model)
|
||
{
|
||
return new Build()
|
||
{
|
||
Id = model.Id,
|
||
Price = model.Price,
|
||
BuildName = model.BuildName,
|
||
UserID = model.UserID,
|
||
Components = model.BuildComponents.Select(x => new BuildComponent
|
||
{
|
||
Component = context.Components.First(y => y.Id == x.Key),
|
||
Count = x.Value.Item2
|
||
}).ToList()
|
||
};
|
||
}
|
||
|
||
public void Update(BuildBindingModel model)
|
||
{
|
||
BuildName = model.BuildName;
|
||
Price = model.Price;
|
||
}
|
||
|
||
public BuildViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
BuildName = BuildName,
|
||
Price = Price,
|
||
UserEmail = User.Email,
|
||
UserID = UserID,
|
||
BuildComponents = BuildComponents
|
||
};
|
||
|
||
public void UpdateComponents(HardwareShopDatabase context, BuildBindingModel model)
|
||
{
|
||
var buildComponents = context.BuildsComponents.Where(rec => rec.BuildID == model.Id).ToList();
|
||
if (buildComponents != null && buildComponents.Count > 0)
|
||
{ // удалили те в бд, которых нет в модели
|
||
context.BuildsComponents.RemoveRange(buildComponents.Where(rec => !model.BuildComponents.ContainsKey(rec.ComponentId)));
|
||
context.SaveChanges();
|
||
// обновили количество у существующих записей
|
||
foreach (var updateComponent in buildComponents)
|
||
{
|
||
updateComponent.Count = model.BuildComponents[updateComponent.ComponentId].Item2;
|
||
model.BuildComponents.Remove(updateComponent.ComponentId);
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
var build = context.Builds.First(x => x.Id == Id);
|
||
//добавляем в бд блюда которые есть в моделе, но ещё нет в бд
|
||
foreach (var dc in model.BuildComponents)
|
||
{
|
||
context.BuildsComponents.Add(new BuildComponent
|
||
{
|
||
Build = build,
|
||
Component = context.Components.First(x => x.Id == dc.Key),
|
||
Count = dc.Value.Item2
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_buildComponents = null;
|
||
}
|
||
|
||
}
|
||
}
|