PIbd-23_Sergunov_M.A._GiftShop/GiftShop/GiftShopDatabaseImplement/Models/Gift.cs

106 lines
3.6 KiB
C#
Raw Normal View History

2023-04-03 08:29:28 +04:00
using GiftShopDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
2023-06-03 01:27:48 +04:00
using System.Runtime.Serialization;
2023-04-03 08:29:28 +04:00
namespace GiftShopDatabaseImplement.Models
{
2023-06-03 01:27:48 +04:00
[DataContract]
public class Gift : IGiftModel
2023-04-03 08:29:28 +04:00
{
2023-06-03 01:27:48 +04:00
[DataMember]
public int Id { get; set; }
2023-04-03 08:29:28 +04:00
[Required]
2023-06-03 01:27:48 +04:00
[DataMember]
public string GiftName { get; set; } = string.Empty;
2023-04-03 08:29:28 +04:00
[Required]
2023-06-03 01:27:48 +04:00
[DataMember]
public double Price { get; set; }
2023-04-03 08:29:28 +04:00
private Dictionary<int, (IComponentModel, int)>? _giftComponents = null;
[NotMapped]
2023-06-03 01:27:48 +04:00
[DataMember]
public Dictionary<int, (IComponentModel, int)> GiftComponents
2023-04-03 08:29:28 +04:00
{
get
{
if (_giftComponents == null)
{
_giftComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _giftComponents;
}
}
[ForeignKey("GiftId")]
public virtual List<GiftComponent> Components { get; set; } = new();
[ForeignKey("GiftId")]
public virtual List<Order> Orders { get; set; } = new();
public static Gift Create(GiftShopDatabase context, GiftBindingModel model)
{
return new Gift()
{
Id = model.Id,
GiftName = model.GiftName,
Price = model.Price,
Components = model.GiftComponents.Select(x => new GiftComponent
{
Component = context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2
}).ToList()
};
}
public void Update(GiftBindingModel model)
{
GiftName = model.GiftName;
Price = model.Price;
}
public GiftViewModel GetViewModel => new()
{
Id = Id,
GiftName = GiftName,
Price = Price,
GiftComponents = GiftComponents
};
public void UpdateComponents(GiftShopDatabase context, GiftBindingModel model)
{
var giftComponents = context.GiftComponents.Where(rec => rec.GiftId == model.Id).ToList();
if (giftComponents != null && giftComponents.Count > 0)
{ // удалили те, которых нет в модели
context.GiftComponents.RemoveRange(giftComponents.Where
(rec => !model.GiftComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in giftComponents)
{
updateComponent.Count = model.GiftComponents[updateComponent.ComponentId].Item2;
model.GiftComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var gift = context.Gifts.First(x => x.Id == Id);
foreach (var pc in model.GiftComponents)
{
context.GiftComponents.Add(new GiftComponent
{
Gift = gift,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_giftComponents = null;
}
}
}