2024-03-13 10:24:19 +04:00
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDataModels.Models;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2024-06-18 21:09:07 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2024-03-13 10:24:19 +04:00
|
|
|
|
|
|
|
|
|
namespace SushiBarDatabaseImplement.Models
|
|
|
|
|
{
|
2024-06-18 21:09:07 +04:00
|
|
|
|
[DataContract]
|
|
|
|
|
public class Sushi : ISushiModel
|
2024-03-13 10:24:19 +04:00
|
|
|
|
{
|
2024-06-18 21:09:07 +04:00
|
|
|
|
[DataMember]
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[DataMember]
|
|
|
|
|
[Required]
|
2024-03-13 10:24:19 +04:00
|
|
|
|
public string SushiName { get; set; } = string.Empty;
|
2024-06-18 21:09:07 +04:00
|
|
|
|
[DataMember]
|
|
|
|
|
[Required]
|
2024-03-13 10:24:19 +04:00
|
|
|
|
public double Price { get; set; }
|
2024-06-18 21:09:07 +04:00
|
|
|
|
|
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _sushiComponents = null;
|
|
|
|
|
[NotMapped]
|
2024-03-13 10:24:19 +04:00
|
|
|
|
public Dictionary<int, (IComponentModel, int)> SushiComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_sushiComponents == null)
|
|
|
|
|
{
|
|
|
|
|
_sushiComponents = Components
|
|
|
|
|
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
|
|
|
|
(recPC.Component as IComponentModel, recPC.Count));
|
|
|
|
|
}
|
|
|
|
|
return _sushiComponents;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("SushiId")]
|
|
|
|
|
public virtual List<SushiComponent> Components { get; set; } = new();
|
|
|
|
|
[ForeignKey("SushiId")]
|
|
|
|
|
public virtual List<Order> Orders { get; set; } = new();
|
|
|
|
|
public static Sushi Create(SushiBarDatabase context, SushiBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Sushi()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
SushiName = model.SushiName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Components = model.SushiComponents.Select(x =>
|
|
|
|
|
new SushiComponent
|
|
|
|
|
{
|
|
|
|
|
Component = context.Components.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(SushiBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
SushiName = model.SushiName;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
}
|
|
|
|
|
public SushiViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
SushiName = SushiName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
SushiComponents = SushiComponents
|
|
|
|
|
};
|
|
|
|
|
public void UpdateComponents(SushiBarDatabase context, SushiBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var sushiComponents = context.SushiComponents.Where(rec =>
|
|
|
|
|
rec.SushiId == model.Id).ToList();
|
|
|
|
|
if (sushiComponents != null && sushiComponents.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
|
|
|
|
context.SushiComponents.RemoveRange(sushiComponents.Where(rec =>
|
|
|
|
|
!model.SushiComponents.ContainsKey(rec.ComponentId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
foreach (var updateComponent in sushiComponents)
|
|
|
|
|
{
|
|
|
|
|
updateComponent.Count = model.SushiComponents[updateComponent.ComponentId].Item2;
|
|
|
|
|
model.SushiComponents.Remove(updateComponent.ComponentId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var sushi = context.Sushis.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.SushiComponents)
|
|
|
|
|
{
|
|
|
|
|
context.SushiComponents.Add(new SushiComponent
|
|
|
|
|
{
|
|
|
|
|
Sushi = sushi,
|
|
|
|
|
Component = context.Components.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_sushiComponents = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|