2023-04-07 18:58:07 +04:00
|
|
|
|
using CanteenContracts.BindingModels;
|
|
|
|
|
using CanteenContracts.View;
|
|
|
|
|
using CanteenDataModels.Models;
|
|
|
|
|
using System;
|
2023-04-07 10:55:40 +04:00
|
|
|
|
using System.Collections.Generic;
|
2023-04-07 18:58:07 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.Globalization;
|
2023-04-07 10:55:40 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CanteenDatabaseImplement.Models
|
|
|
|
|
{
|
2023-04-07 18:58:07 +04:00
|
|
|
|
public class Dish : IDishModel
|
2023-04-07 10:55:40 +04:00
|
|
|
|
{
|
2023-04-07 23:35:33 +04:00
|
|
|
|
public int Id { get; set; }
|
2023-04-09 00:34:25 +04:00
|
|
|
|
[Required]
|
2023-04-07 23:35:33 +04:00
|
|
|
|
public string DishName { get; set; } = string.Empty;
|
2023-04-09 00:34:25 +04:00
|
|
|
|
[Required]
|
2023-04-07 23:35:33 +04:00
|
|
|
|
public double Price { get; set; }
|
2023-04-09 00:34:25 +04:00
|
|
|
|
[Required]
|
2023-04-07 23:35:33 +04:00
|
|
|
|
public int ManagerId { get; set; }
|
|
|
|
|
|
2023-04-07 18:58:07 +04:00
|
|
|
|
private Dictionary<int, (IProductModel, int)>? _dishProducts = null;
|
2023-04-07 23:35:33 +04:00
|
|
|
|
[NotMapped]
|
2023-04-09 00:34:25 +04:00
|
|
|
|
public Dictionary<int, (IProductModel, int)> DishProducts
|
2023-04-07 18:58:07 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_dishProducts == null)
|
|
|
|
|
{
|
|
|
|
|
_dishProducts = Products
|
2023-04-09 00:34:25 +04:00
|
|
|
|
.ToDictionary(recDP => recDP.DishId, recDP => (recDP.Product as IProductModel, recDP.CountProducts));
|
2023-04-07 18:58:07 +04:00
|
|
|
|
}
|
|
|
|
|
return _dishProducts;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-07 23:35:33 +04:00
|
|
|
|
|
2023-04-07 18:58:07 +04:00
|
|
|
|
[ForeignKey("DishId")]
|
|
|
|
|
public virtual List<DishProduct> Products { get; set; } = new();
|
2023-04-09 00:34:25 +04:00
|
|
|
|
[ForeignKey("DishId")]
|
|
|
|
|
public virtual List<OrderDish> Orders { get; set; } = new();
|
|
|
|
|
public virtual Manager Manager { get; set; }
|
2023-04-07 23:35:33 +04:00
|
|
|
|
|
|
|
|
|
public static Dish Create(CanteenDatabase context, DishBindingModel model)
|
2023-04-07 18:58:07 +04:00
|
|
|
|
{
|
2023-04-07 23:35:33 +04:00
|
|
|
|
return new Dish
|
2023-04-07 18:58:07 +04:00
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
DishName = model.DishName,
|
2023-04-07 23:35:33 +04:00
|
|
|
|
Price = model.Price,
|
|
|
|
|
ManagerId = model.ManagerId,
|
2023-04-09 00:34:25 +04:00
|
|
|
|
Products = model.DishProducts.Select(x => new DishProduct
|
2023-04-07 18:58:07 +04:00
|
|
|
|
{
|
2023-04-07 23:35:33 +04:00
|
|
|
|
Product = context.Products.First(y => y.Id == x.Key),
|
2023-04-09 00:34:25 +04:00
|
|
|
|
CountProducts = x.Value.Item2
|
2023-04-07 18:58:07 +04:00
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(DishBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
DishName = model.DishName;
|
2023-04-07 23:35:33 +04:00
|
|
|
|
Price = model.Price;
|
|
|
|
|
ManagerId = model.ManagerId;
|
2023-04-07 18:58:07 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DishViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
DishName = DishName,
|
2023-04-07 23:35:33 +04:00
|
|
|
|
Price = Price,
|
|
|
|
|
ManagerId = ManagerId,
|
2023-04-09 00:34:25 +04:00
|
|
|
|
DishProducts = DishProducts
|
2023-04-07 18:58:07 +04:00
|
|
|
|
};
|
|
|
|
|
|
2023-04-09 00:34:25 +04:00
|
|
|
|
|
2023-04-07 23:35:33 +04:00
|
|
|
|
public void UpdateDishProduct(CanteenDatabase context, DishBindingModel model)
|
2023-04-07 18:58:07 +04:00
|
|
|
|
{
|
2023-04-09 00:34:25 +04:00
|
|
|
|
var dishProduct = context.DishProduct.Where(rec => rec.DishId == model.Id).ToList();
|
2023-04-07 23:35:33 +04:00
|
|
|
|
if (dishProduct != null && (dishProduct.Count > 0))
|
2023-04-07 18:58:07 +04:00
|
|
|
|
{
|
2023-04-09 00:34:25 +04:00
|
|
|
|
context.DishProduct.RemoveRange(dishProduct.Where(rec => !model.DishProducts.ContainsKey(rec.ProductId)));
|
2023-04-07 23:35:33 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
foreach (var updateProduct in dishProduct)
|
|
|
|
|
{
|
2023-04-09 00:34:25 +04:00
|
|
|
|
updateProduct.CountProducts = model.DishProducts[updateProduct.ProductId].Item2;
|
|
|
|
|
model.DishProducts.Remove(updateProduct.ProductId);
|
2023-04-07 23:35:33 +04:00
|
|
|
|
}
|
2023-04-07 18:58:07 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2023-04-07 23:35:33 +04:00
|
|
|
|
var dish = context.Dishes.First(x => x.Id == Id);
|
2023-04-09 00:34:25 +04:00
|
|
|
|
foreach (var dp in model.DishProducts)
|
2023-04-07 18:58:07 +04:00
|
|
|
|
{
|
2023-04-09 00:34:25 +04:00
|
|
|
|
context.DishProduct.Add(new DishProduct
|
2023-04-07 18:58:07 +04:00
|
|
|
|
{
|
2023-04-07 23:35:33 +04:00
|
|
|
|
Dish = dish,
|
|
|
|
|
Product = context.Products.First(x => x.Id == dp.Key),
|
2023-04-09 00:34:25 +04:00
|
|
|
|
CountProducts = dp.Value.Item2
|
2023-04-07 18:58:07 +04:00
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_dishProducts = null;
|
|
|
|
|
}
|
2023-04-07 10:55:40 +04:00
|
|
|
|
}
|
|
|
|
|
}
|