106 lines
3.7 KiB
C#
106 lines
3.7 KiB
C#
|
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; }
|
|||
|
|
|||
|
|
|||
|
[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,
|
|||
|
// BuildName = model.BuildName,
|
|||
|
// Price = model.Price,
|
|||
|
// 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,
|
|||
|
// BuildComponents = BuildComponents
|
|||
|
//};
|
|||
|
|
|||
|
//public void UpdateComponents(HardwareShopDatabase context, BuildBindingModel model)
|
|||
|
//{
|
|||
|
// var buildComponents = context.BuildComponents.Where(rec => rec.BuildId == model.Id).ToList();
|
|||
|
// if (buildComponents != null && buildComponents.Count > 0)
|
|||
|
// { // удалили те в бд, которых нет в модели
|
|||
|
// context.BuildComponents.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.BuildComponents.Add(new BuildComponent
|
|||
|
// {
|
|||
|
// Build = build,
|
|||
|
// Component = context.Components.First(x => x.Id == dc.Key),
|
|||
|
// Count = dc.Value.Item2
|
|||
|
// });
|
|||
|
// context.SaveChanges();
|
|||
|
// }
|
|||
|
// _buildComponents = null;
|
|||
|
//}
|
|||
|
|
|||
|
}
|
|||
|
}
|