106 lines
3.6 KiB
C#
106 lines
3.6 KiB
C#
using DinerContracts.BindingModels;
|
|
using DinerContracts.ViewModels;
|
|
using DinerDataModels.Models;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
|
|
namespace DinerDataBaseImplement.Models
|
|
{
|
|
[DataContract]
|
|
public class Snack : ISnackModel
|
|
{
|
|
[Required]
|
|
[DataMember]
|
|
public string ProductName { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
[DataMember]
|
|
public double Price { get; set; }
|
|
|
|
[DataMember]
|
|
public int ID { get; set; }
|
|
|
|
private Dictionary<int, (IFoodModel, int)>? _productComponents = null;
|
|
|
|
[NotMapped]
|
|
[DataMember]
|
|
public Dictionary<int, (IFoodModel, int)> ProductComponents
|
|
{
|
|
get
|
|
{
|
|
if (_productComponents == null)
|
|
{
|
|
_productComponents = Components
|
|
.ToDictionary(recPC => recPC.ComponentID, recPC => (recPC.Component as IFoodModel, recPC.Count));
|
|
}
|
|
return _productComponents;
|
|
}
|
|
}
|
|
[ForeignKey("SnackID")]
|
|
public virtual List<SnackFood> Components { get; set; } = new();
|
|
|
|
[ForeignKey("SnackID")]
|
|
public virtual List<Order> Orders { get; set; } = new();
|
|
public static Snack? Create(DinerDatabaseBy7Work context,SnackBindingModel model)
|
|
{
|
|
return new Snack()
|
|
{
|
|
ID = model.ID,
|
|
ProductName = model.ProductName,
|
|
Price = model.Price,
|
|
Components = model.ProductComponents.Select(x => new SnackFood
|
|
{
|
|
Component = context.Components.First(y => y.ID == x.Key),
|
|
Count = x.Value.Item2
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public void Update(SnackBindingModel model)
|
|
{
|
|
ProductName = model.ProductName;
|
|
Price = model.Price;
|
|
}
|
|
public SnackViewModel GetViewModel => new()
|
|
{
|
|
ID = ID,
|
|
ProductName = ProductName,
|
|
Price = Price,
|
|
ProductComponents = ProductComponents
|
|
};
|
|
|
|
public void UpdateComponents(DinerDatabaseBy7Work context, SnackBindingModel model) {
|
|
var productComponents = context.ProductComponents.Where(rec => rec.SnackID == model.ID).ToList();
|
|
if (ProductComponents != null && ProductComponents.Count > 0) {
|
|
context.ProductComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID)));
|
|
context.SaveChanges();
|
|
foreach (var updateComponent in productComponents) {
|
|
updateComponent.Count = model.ProductComponents[updateComponent.ComponentID].Item2;
|
|
model.ProductComponents.Remove(updateComponent.ComponentID);
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
var product = context.Snacks.First(x => x.ID == ID);
|
|
foreach (var pc in model.ProductComponents) {
|
|
context.ProductComponents.Add(new SnackFood
|
|
{
|
|
Product = product,
|
|
Component = context.Components.First(x => x.ID == pc.Key),
|
|
Count = pc.Value.Item2
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_productComponents = null;
|
|
}
|
|
}
|
|
}
|