2024-03-28 23:34:06 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ConfectioneryContracts.BindingModels;
|
|
|
|
|
using ConfectioneryContracts.ViewModels;
|
|
|
|
|
using ConfectioneryDataModels.Models;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2024-06-04 08:48:13 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2024-03-28 23:34:06 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ConfectioneryDatabaseImplement.Models
|
|
|
|
|
{
|
2024-06-04 08:48:13 +04:00
|
|
|
|
[DataContract]
|
|
|
|
|
public class Pastry : IPastryModel
|
|
|
|
|
{
|
|
|
|
|
[DataMember]
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[DataMember]
|
|
|
|
|
[Required]
|
|
|
|
|
public string PastryName { get; set; } = string.Empty;
|
|
|
|
|
[DataMember]
|
|
|
|
|
[Required]
|
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _pastryComponents = null;
|
|
|
|
|
[DataMember]
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IComponentModel, int)> PastryComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_pastryComponents == null)
|
|
|
|
|
{
|
|
|
|
|
_pastryComponents = Components
|
|
|
|
|
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
|
|
|
|
(recPC.Component as IComponentModel, recPC.Count));
|
|
|
|
|
}
|
|
|
|
|
return _pastryComponents;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("PastryId")]
|
|
|
|
|
public virtual List<PastryComponent> Components { get; set; } = new();
|
|
|
|
|
[ForeignKey("PastryId")]
|
|
|
|
|
public virtual List<Order> Orders { get; set; } = new();
|
|
|
|
|
public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Pastry()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
PastryName = model.PastryName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Components = model.PastryComponents.Select(x => new PastryComponent
|
|
|
|
|
{
|
|
|
|
|
Component = context.Components.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(PastryBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
PastryName = model.PastryName;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
}
|
|
|
|
|
public PastryViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
PastryName = PastryName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
PastryComponents = PastryComponents
|
|
|
|
|
};
|
|
|
|
|
public void UpdateComponents(ConfectioneryDatabase context, PastryBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var pastryComponents = context.PastryComponents.Where(rec => rec.PastryId == model.Id).ToList();
|
|
|
|
|
if (pastryComponents != null && pastryComponents.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
|
|
|
|
context.PastryComponents.RemoveRange(pastryComponents.Where(rec => !model.PastryComponents.ContainsKey(rec.ComponentId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
foreach (var updateComponent in pastryComponents)
|
|
|
|
|
{
|
|
|
|
|
updateComponent.Count = model.PastryComponents[updateComponent.ComponentId].Item2;
|
|
|
|
|
model.PastryComponents.Remove(updateComponent.ComponentId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var pastry = context.Pastrys.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.PastryComponents)
|
|
|
|
|
{
|
|
|
|
|
context.PastryComponents.Add(new PastryComponent
|
|
|
|
|
{
|
|
|
|
|
Pastry = pastry,
|
|
|
|
|
Component = context.Components.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_pastryComponents = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-28 23:34:06 +04:00
|
|
|
|
}
|