2024-03-05 12:53:08 +04:00
|
|
|
|
using DinerContracts.BindingModels;
|
|
|
|
|
using DinerContracts.ViewModels;
|
|
|
|
|
using DinerDataModels.Models;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2024-05-12 15:13:03 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2024-03-05 12:53:08 +04:00
|
|
|
|
|
|
|
|
|
namespace DinerDataBaseImplement.Models
|
|
|
|
|
{
|
2024-05-12 15:13:03 +04:00
|
|
|
|
[DataContract]
|
2024-03-05 12:53:08 +04:00
|
|
|
|
public class Snack : ISnackModel
|
|
|
|
|
{
|
2024-05-12 15:13:03 +04:00
|
|
|
|
[DataMember]
|
2024-03-05 12:53:08 +04:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[Required]
|
2024-05-12 15:13:03 +04:00
|
|
|
|
[DataMember]
|
2024-03-05 12:53:08 +04:00
|
|
|
|
public string SnackName { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
2024-05-12 15:13:03 +04:00
|
|
|
|
[DataMember]
|
2024-03-05 12:53:08 +04:00
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _snackComponents = null;
|
|
|
|
|
[NotMapped]
|
2024-05-12 15:13:03 +04:00
|
|
|
|
[DataMember]
|
2024-03-05 12:53:08 +04:00
|
|
|
|
public Dictionary<int, (IComponentModel, int)> SnackComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_snackComponents == null)
|
|
|
|
|
{
|
|
|
|
|
_snackComponents = Components
|
|
|
|
|
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
|
|
|
|
}
|
|
|
|
|
return _snackComponents;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("SnackId")]
|
|
|
|
|
public virtual List<SnackComponent> Components { get; set; } = new();
|
|
|
|
|
[ForeignKey("SnackId")]
|
|
|
|
|
public virtual List<Order> Orders { get; set; } = new();
|
|
|
|
|
public static Snack Create(DinerDatabase context, SnackBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Snack()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
SnackName = model.SnackName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Components = model.SnackComponents.Select(x => new SnackComponent
|
|
|
|
|
{
|
|
|
|
|
Component = context.Components.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(SnackBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
SnackName = model.SnackName;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
}
|
|
|
|
|
public SnackViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
SnackName = SnackName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
SnackComponents = SnackComponents
|
|
|
|
|
};
|
|
|
|
|
public void UpdateComponents(DinerDatabase context, SnackBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var snackComponents = context.SnackComponents.Where(rec => rec.SnackId == model.Id).ToList();
|
|
|
|
|
if (snackComponents != null && snackComponents.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
|
|
|
|
context.SnackComponents.RemoveRange(snackComponents.Where(rec => !model.SnackComponents.ContainsKey(rec.ComponentId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
foreach (var updateComponent in snackComponents)
|
|
|
|
|
{
|
|
|
|
|
updateComponent.Count = model.SnackComponents[updateComponent.ComponentId].Item2;
|
|
|
|
|
model.SnackComponents.Remove(updateComponent.ComponentId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var snack = context.Snacks.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.SnackComponents)
|
|
|
|
|
{
|
|
|
|
|
context.SnackComponents.Add(new SnackComponent
|
|
|
|
|
{
|
|
|
|
|
Snack = snack,
|
|
|
|
|
Component = context.Components.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_snackComponents = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|