100 lines
3.5 KiB
C#
Raw Normal View History

2024-03-22 09:14:00 +04:00
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.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace DinerDataBaseImplement.Models
{
public class Snack : ISnackModel
{
[Required]
2024-05-05 18:00:10 +04:00
public string ProductName { get; set; } = string.Empty;
2024-03-22 09:14:00 +04:00
[Required]
2024-05-05 18:00:10 +04:00
public double Price { get; set; }
2024-03-22 09:14:00 +04:00
2024-05-05 18:00:10 +04:00
public int ID { get; set; }
2024-03-22 09:14:00 +04:00
private Dictionary<int, (IFoodModel, int)>? _productComponents = null;
[NotMapped]
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;
}
}
2024-05-05 18:00:10 +04:00
[ForeignKey("SnackID")]
2024-03-22 09:14:00 +04:00
public virtual List<SnackFood> Components { get; set; } = new();
2024-05-11 22:14:52 +04:00
[ForeignKey("SnackID")]
public virtual List<Order> Orders { get; set; } = new();
2024-05-16 18:05:13 +04:00
public static Snack? Create(DinerDatabaseBy7Work context,SnackBindingModel model)
2024-03-22 09:14:00 +04:00
{
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
};
2024-05-16 18:05:13 +04:00
public void UpdateComponents(DinerDatabaseBy7Work context, SnackBindingModel model) {
2024-05-05 18:00:10 +04:00
var productComponents = context.ProductComponents.Where(rec => rec.SnackID == model.ID).ToList();
2024-03-22 09:14:00 +04:00
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();
}
2024-05-11 22:14:52 +04:00
var product = context.Snacks.First(x => x.ID == ID);
2024-03-22 09:14:00 +04:00
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;
}
}
}