PIbd-23_Ivanov_V.N._Pizzeria/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs

105 lines
3.5 KiB
C#
Raw Permalink Normal View History

2024-03-04 23:06:34 +04:00
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
2024-04-03 17:52:29 +04:00
using System.Runtime.Serialization;
2024-03-04 23:06:34 +04:00
namespace PizzeriaDatabaseImplement.Models
{
2024-04-03 17:52:29 +04:00
[DataContract]
2024-03-04 23:06:34 +04:00
public class Pizza : IPizzaModel
{
2024-04-03 17:52:29 +04:00
[DataMember]
2024-03-04 23:06:34 +04:00
public int Id { get; set; }
2024-04-03 17:52:29 +04:00
[DataMember]
2024-03-04 23:06:34 +04:00
[Required]
public string PizzaName { get; set; } = string.Empty;
2024-04-03 17:52:29 +04:00
[DataMember]
2024-03-04 23:06:34 +04:00
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _pizzaComponents = null;
2024-04-03 17:52:29 +04:00
[DataMember]
2024-03-04 23:06:34 +04:00
[NotMapped]
public Dictionary<int, (IComponentModel, int)> PizzaComponents
{
get
{
if (_pizzaComponents == null)
{
_pizzaComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _pizzaComponents;
}
}
[ForeignKey("PizzaId")]
public virtual List<PizzaComponent> Components { get; set; } = new();
[ForeignKey("PizzaId")]
public virtual List<Order> Orders { get; set; } = new();
2024-03-21 13:21:25 +04:00
public static Pizza Create(PizzeriaDatabase context, PizzaBindingModel model)
2024-03-04 23:06:34 +04:00
{
return new Pizza()
{
Id = model.Id,
PizzaName = model.PizzaName,
Price = model.Price,
Components = model.PizzaComponents.Select(x => new PizzaComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(PizzaBindingModel model)
{
PizzaName = model.PizzaName;
Price = model.Price;
}
public PizzaViewModel GetViewModel => new()
{
Id = Id,
PizzaName = PizzaName,
Price = Price,
PizzaComponents = PizzaComponents
};
public void UpdateComponents(PizzeriaDatabase context, PizzaBindingModel model)
{
var pizzaComponents = context.PizzaComponents.Where(rec => rec.PizzaId == model.Id).ToList();
if (pizzaComponents != null && pizzaComponents.Count > 0)
{
context.PizzaComponents.RemoveRange(pizzaComponents.Where(rec => !model.PizzaComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in pizzaComponents)
{
updateComponent.Count = model.PizzaComponents[updateComponent.ComponentId].Item2;
model.PizzaComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var pizza = context.Pizzas.First(x => x.Id == Id);
foreach (var pc in model.PizzaComponents)
{
context.PizzaComponents.Add(new PizzaComponent
{
Pizza = pizza,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_pizzaComponents = null;
}
}
}