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 23:35:33 +04:00
|
|
|
|
//public class Dish : IDishModel
|
|
|
|
|
//{
|
|
|
|
|
// public int Id { get; private set; }
|
|
|
|
|
// [Required]
|
|
|
|
|
// public string DishName { get; private set; } = string.Empty;
|
|
|
|
|
// [Required]
|
|
|
|
|
// public double Cost { get; private set; }
|
|
|
|
|
// [Required]
|
|
|
|
|
// public int ManagerId { get; private set; }
|
|
|
|
|
// private Dictionary<int, (IProductModel, int)>? _dishProduct = null;
|
|
|
|
|
// public Dictionary<int, (IProductModel, int)> DishProduct
|
|
|
|
|
// {
|
|
|
|
|
// get
|
|
|
|
|
// {
|
|
|
|
|
// if (_dishProduct == null)
|
|
|
|
|
// {
|
|
|
|
|
// _dishProduct = Product
|
|
|
|
|
// .ToDictionary(recPC => recPC.ProductId, recPC => (recPC.Product as IProductModel, recPC.CountProductProduct));
|
|
|
|
|
// }
|
|
|
|
|
// return _dishProduct;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// [ForeignKey("DishId")]
|
|
|
|
|
// public virtual List<DishProduct> Product { get; set; } = new();
|
|
|
|
|
// public static Dish? Create(CanteenDatabase context, DishBindingModel model)
|
|
|
|
|
// {
|
|
|
|
|
// return new Dish()
|
|
|
|
|
// {
|
|
|
|
|
// Id = model.Id,
|
|
|
|
|
// DishName = model.DishName,
|
|
|
|
|
// Cost = model.Cost,
|
|
|
|
|
// Product = model.DishProduct.Select(x => new DishProduct
|
|
|
|
|
// {
|
|
|
|
|
// Product = context.Product.First(y => y.Id == x.Key)
|
|
|
|
|
// }).ToList()
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public void Update(DishBindingModel model)
|
|
|
|
|
// {
|
|
|
|
|
// DishName = model.DishName;
|
|
|
|
|
// Cost = model.Cost;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// public DishViewModel GetViewModel => new()
|
|
|
|
|
// {
|
|
|
|
|
// Id = Id,
|
|
|
|
|
// DishName = DishName,
|
|
|
|
|
// Cost = Cost,
|
|
|
|
|
// DishProduct = DishProduct,
|
|
|
|
|
// ManagerId = ManagerId
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// public Dictionary<int, (IProductModel, int)> DishProduct => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
// public void UpdateProduct(CanteenDatabase context, DishBindingModel model)
|
|
|
|
|
// {
|
|
|
|
|
// var dishProduct = context.DishProduct.Where(record => record.ProductId == model.Id).ToList();
|
|
|
|
|
// if (dishProduct != null && dishProduct.CountProduct > 0)
|
|
|
|
|
// {
|
|
|
|
|
// context.DishProduct.RemoveRange(dishProduct.Where(record => !model.DishProduct.ContainsKey(record.ProductId)));
|
|
|
|
|
// context.SaveChanges();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// var product = context.Product.First(x => x.Id == Id);
|
|
|
|
|
// foreach (var pc in model.DishProduct)
|
|
|
|
|
// {
|
|
|
|
|
// context.DishProduct.Add(new DishProduct
|
|
|
|
|
// {
|
|
|
|
|
// Product = product,
|
|
|
|
|
// Dish = context.Dishes.First(x => x.Id == pc.Key),
|
|
|
|
|
// });
|
|
|
|
|
// context.SaveChanges();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// _dishProduct = null;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
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
|
|
|
|
}
|
|
|
|
|
}
|