partially implemented storage
This commit is contained in:
parent
6d3c7c0a5d
commit
27a19ce7f0
@ -13,7 +13,7 @@ namespace CanteenContracts.BindingModels
|
|||||||
public string DishName { get; set; } = string.Empty;
|
public string DishName { get; set; } = string.Empty;
|
||||||
public double Price { get; set; }
|
public double Price { get; set; }
|
||||||
public int ManagerId { get; set; }
|
public int ManagerId { get; set; }
|
||||||
public Dictionary<int, (IProductModel, int)> DishProducts { get; set; } = new();
|
public Dictionary<int, (IProductModel, int)> OrderDishes { get; set; } = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,6 @@ namespace CanteenContracts.BindingModels
|
|||||||
public string ProductName { get; set; } = string.Empty;
|
public string ProductName { get; set; } = string.Empty;
|
||||||
public double Price { get; set; }
|
public double Price { get; set; }
|
||||||
public int ManagerId { get; set; }
|
public int ManagerId { get; set; }
|
||||||
public Dictionary<int, ICookModel> ProductCooks { get; set; } = new Dictionary<int, ICookModel>();
|
public Dictionary<int, ICookModel> ProductCooks { get; set; } = new();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,6 @@ namespace CanteenContracts.View
|
|||||||
[DisplayName("ID менеджера")]
|
[DisplayName("ID менеджера")]
|
||||||
public int ManagerId { get; set; }
|
public int ManagerId { get; set; }
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public Dictionary<int, (IProductModel, int)> DishProducts { get; set; }
|
public Dictionary<int, (IProductModel, int)> OrderDishes { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,8 @@ namespace CanteenContracts.View
|
|||||||
public string Description { get; set; } = string.Empty;
|
public string Description { get; set; } = string.Empty;
|
||||||
[DisplayName("Сумма")]
|
[DisplayName("Сумма")]
|
||||||
public double? Sum { get; set; }
|
public double? Sum { get; set; }
|
||||||
[DisplayName("Блюда в заказе")]
|
|
||||||
public Dictionary<int, (IDishModel, int)> OrderDishes { get; set; } = new Dictionary<int, (IDishModel, int)>();
|
public Dictionary<int, (IDishModel, int)> OrderDishes { get; set; } = new Dictionary<int, (IDishModel, int)>();
|
||||||
|
[DisplayName("ID заказа")]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,6 @@ namespace CanteenDataModels.Models
|
|||||||
string DishName { get; }
|
string DishName { get; }
|
||||||
double Price { get; }
|
double Price { get; }
|
||||||
int ManagerId { get; }
|
int ManagerId { get; }
|
||||||
Dictionary<int, (IProductModel, int)> DishProducts { get; }
|
Dictionary<int, (IProductModel, int)> OrderDishes { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,16 +14,16 @@ namespace CanteenDatabaseImplement
|
|||||||
base.OnConfiguring(optionsBuilder);
|
base.OnConfiguring(optionsBuilder);
|
||||||
}
|
}
|
||||||
public virtual DbSet<Cook> Cooks { set; get; }
|
public virtual DbSet<Cook> Cooks { set; get; }
|
||||||
public virtual DbSet<OrderCook> OrderCooks { set; get; }
|
public virtual DbSet<OrderCook> OrderCook { set; get; }
|
||||||
public virtual DbSet<Dish> Dishes { set; get; }
|
public virtual DbSet<Dish> Dishes { set; get; }
|
||||||
public virtual DbSet<Lunch> Lunches { set; get; }
|
public virtual DbSet<Lunch> Lunches { set; get; }
|
||||||
public virtual DbSet<OrderLunch> OrderLunches { set; get; }
|
public virtual DbSet<OrderLunch> OrderLunch { set; get; }
|
||||||
public virtual DbSet<Manager> Managers { set; get; }
|
public virtual DbSet<Manager> Managers { set; get; }
|
||||||
public virtual DbSet<Order> Orders { set; get; }
|
public virtual DbSet<Order> Orders { set; get; }
|
||||||
public virtual DbSet<Product> Products { set; get; }
|
public virtual DbSet<Product> Products { set; get; }
|
||||||
public virtual DbSet<ProductCook> ProductCooks { set; get; }
|
public virtual DbSet<ProductCook> ProductCook { set; get; }
|
||||||
public virtual DbSet<LunchProduct> LunchProducts { set; get; }
|
public virtual DbSet<LunchProduct> LunchProduct { set; get; }
|
||||||
public virtual DbSet<DishProduct> DishProducts { set; get; }
|
public virtual DbSet<DishProduct> OrderDish { set; get; }
|
||||||
public virtual DbSet<Role> Roles { set; get; }
|
public virtual DbSet<Role> Roles { set; get; }
|
||||||
public virtual DbSet<Tableware> Tablewares { set; get; }
|
public virtual DbSet<Tableware> Tablewares { set; get; }
|
||||||
public virtual DbSet<Visitor> Visitors { set; get; }
|
public virtual DbSet<Visitor> Visitors { set; get; }
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using CanteenDataModels.Models;
|
using CanteenContracts.BindingModels;
|
||||||
|
using CanteenContracts.View;
|
||||||
|
using CanteenDataModels.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
@ -15,15 +17,45 @@ namespace CanteenDatabaseImplement.Models
|
|||||||
[Required]
|
[Required]
|
||||||
public int ManagerId { get; private set; }
|
public int ManagerId { get; private set; }
|
||||||
[Required]
|
[Required]
|
||||||
public string Name { get; private set; } = string.Empty;
|
public string FIO { get; private set; } = string.Empty;
|
||||||
[Required]
|
|
||||||
public string Surname { get; private set; } = string.Empty;
|
|
||||||
public string? Patronymic { get; private set; } = string.Empty;
|
|
||||||
[Required]
|
[Required]
|
||||||
public string Position { get; private set; } = string.Empty;
|
public string Position { get; private set; } = string.Empty;
|
||||||
[ForeignKey("CookId")]
|
[ForeignKey("CookId")]
|
||||||
public virtual List<ProductCook> Products{ get; set; } = new();
|
public virtual List<ProductCook> Products { get; set; } = new();
|
||||||
[ForeignKey("CookId")]
|
|
||||||
public virtual List<OrderCook> Orders { get; set; } = new();
|
public static Cook Create(CookBindingModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Cook
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
ManagerId = model.ManagerId,
|
||||||
|
FIO = model.FIO,
|
||||||
|
Position = model.Position
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(CookBindingModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FIO = model.FIO;
|
||||||
|
Position = model.Position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CookViewModel GetViewModel => new CookViewModel
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
ManagerId = ManagerId,
|
||||||
|
FIO = FIO,
|
||||||
|
Position = Position
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,40 +12,121 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace CanteenDatabaseImplement.Models
|
namespace CanteenDatabaseImplement.Models
|
||||||
{
|
{
|
||||||
|
//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;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
public class Dish : IDishModel
|
public class Dish : IDishModel
|
||||||
{
|
{
|
||||||
public int Id { get; private set; }
|
public int Id { get; set; }
|
||||||
[Required]
|
public string DishName { get; set; } = string.Empty;
|
||||||
public string DishName { get; private set; } = string.Empty;
|
public double Price { get; set; }
|
||||||
[Required]
|
public int ManagerId { get; set; }
|
||||||
public double Cost { get; private set; }
|
|
||||||
[Required]
|
|
||||||
public int ManagerId { get; private set; }
|
|
||||||
private Dictionary<int, (IProductModel, int)>? _dishProducts = null;
|
private Dictionary<int, (IProductModel, int)>? _dishProducts = null;
|
||||||
public Dictionary<int, (IProductModel, int)> DishProduct
|
[NotMapped]
|
||||||
|
public Dictionary<int, (IProductModel, int)> OrderDishes
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_dishProducts == null)
|
if (_dishProducts == null)
|
||||||
{
|
{
|
||||||
_dishProducts = Products
|
_dishProducts = Products
|
||||||
.ToDictionary(recPC => recPC.ProductId, recPC => (recPC.Product as IProductModel, recPC.CountProduct));
|
.ToDictionary(recDP => recDP.DishesId, recDP => (recDP.Product as IProductModel, recDP.CountProduct));
|
||||||
}
|
}
|
||||||
return _dishProducts;
|
return _dishProducts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ForeignKey("DishId")]
|
[ForeignKey("DishId")]
|
||||||
public virtual List<DishProduct> Products { get; set; } = new();
|
public virtual List<DishProduct> Products { get; set; } = new();
|
||||||
public static Dish? Create(CanteenDatabase context, DishBindingModel model)
|
|
||||||
|
public static Dish Create(CanteenDatabase context, DishBindingModel model)
|
||||||
{
|
{
|
||||||
return new Dish()
|
return new Dish
|
||||||
{
|
{
|
||||||
Id = model.Id,
|
Id = model.Id,
|
||||||
DishName = model.DishName,
|
DishName = model.DishName,
|
||||||
Cost = model.Cost,
|
Price = model.Price,
|
||||||
Products = model.DishProducts.Select(x => new DishProduct
|
ManagerId = model.ManagerId,
|
||||||
|
Products = model.OrderDishes.Select(x => new DishProduct
|
||||||
{
|
{
|
||||||
Product = context.Products.First(y => y.Id == x.Key)
|
Product = context.Products.First(y => y.Id == x.Key),
|
||||||
|
CountProduct = x.Value.Item2
|
||||||
}).ToList()
|
}).ToList()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -53,40 +134,44 @@ namespace CanteenDatabaseImplement.Models
|
|||||||
public void Update(DishBindingModel model)
|
public void Update(DishBindingModel model)
|
||||||
{
|
{
|
||||||
DishName = model.DishName;
|
DishName = model.DishName;
|
||||||
Cost = model.Cost;
|
Price = model.Price;
|
||||||
|
ManagerId = model.ManagerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DishViewModel GetViewModel => new()
|
public DishViewModel GetViewModel => new()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
DishName = DishName,
|
DishName = DishName,
|
||||||
Cost = Cost,
|
Price = Price,
|
||||||
DishProducts = DishProduct,
|
ManagerId = ManagerId,
|
||||||
ManagerId = ManagerId
|
OrderDishes = OrderDishes
|
||||||
};
|
};
|
||||||
|
|
||||||
public Dictionary<int, (IProductModel, int)> DishProducts => throw new NotImplementedException();
|
public void UpdateDishProduct(CanteenDatabase context, DishBindingModel model)
|
||||||
|
|
||||||
public void UpdateProduct(CanteenDatabase context, DishBindingModel model)
|
|
||||||
{
|
{
|
||||||
var dishProducts = context.DishProducts.Where(record => record.ProductId == model.Id).ToList();
|
var dishProduct = context.OrderDish.Where(rec => rec.DishId == model.Id).ToList();
|
||||||
if (dishProducts != null && dishProducts.Count > 0)
|
if (dishProduct != null && (dishProduct.Count > 0))
|
||||||
{
|
{
|
||||||
context.DishProducts.RemoveRange(dishProducts.Where(record => !model.DishProducts.ContainsKey(record.ProductId)));
|
context.OrderDish.RemoveRange(dishProduct.Where(rec => !model.OrderDishes.ContainsKey(rec.DishesId)));
|
||||||
|
context.SaveChanges();
|
||||||
|
foreach (var updateProduct in dishProduct)
|
||||||
|
{
|
||||||
|
updateProduct.CountProduct = model.OrderDishes[updateProduct.DishesId].Item2;
|
||||||
|
model.OrderDishes.Remove(updateProduct.DishesId);
|
||||||
|
}
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
|
var dish = context.Dishes.First(x => x.Id == Id);
|
||||||
var product = context.Products.First(x => x.Id == Id);
|
foreach (var dp in model.OrderDishes)
|
||||||
foreach (var pc in model.DishProducts)
|
|
||||||
{
|
{
|
||||||
context.DishProducts.Add(new DishProduct
|
context.OrderDish.Add(new DishProduct
|
||||||
{
|
{
|
||||||
Product = product,
|
Dish = dish,
|
||||||
Dish = context.Dishes.First(x => x.Id == pc.Key),
|
Product = context.Products.First(x => x.Id == dp.Key),
|
||||||
|
CountProduct = dp.Value.Item2
|
||||||
});
|
});
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
_dishProducts = null;
|
_dishProducts = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ namespace CanteenDatabaseImplement.Models
|
|||||||
[Required]
|
[Required]
|
||||||
public int DishId { get; set; }
|
public int DishId { get; set; }
|
||||||
[Required]
|
[Required]
|
||||||
public int ProductId { get; set; }
|
public int DishesId { get; set; }
|
||||||
[Required]
|
[Required]
|
||||||
public int CountProduct { get; set; }
|
public int CountProduct { get; set; }
|
||||||
public virtual Dish Dish { get; set; } = new();
|
public virtual Dish Dish { get; set; } = new();
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using CanteenDataModels.Enums;
|
using CanteenContracts.BindingModels;
|
||||||
|
using CanteenContracts.View;
|
||||||
|
using CanteenDataModels.Enums;
|
||||||
using CanteenDataModels.Models;
|
using CanteenDataModels.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -13,21 +15,89 @@ namespace CanteenDatabaseImplement.Models
|
|||||||
public class Order : IOrderModel
|
public class Order : IOrderModel
|
||||||
{
|
{
|
||||||
public int Id { get; private set; }
|
public int Id { get; private set; }
|
||||||
[Required]
|
|
||||||
public int VisitorId { get; private set; }
|
public int VisitorId { get; private set; }
|
||||||
[Required]
|
|
||||||
public string Description { get; private set; } = string.Empty;
|
public string Description { get; private set; } = string.Empty;
|
||||||
[Required]
|
public double? Sum { get; private set; }
|
||||||
public double Sum { get; private set; }
|
|
||||||
[Required]
|
private Dictionary<int, (IDishModel, int)>? _orderDishes = null;
|
||||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
|
||||||
|
[NotMapped]
|
||||||
|
public Dictionary<int, (IDishModel, int)> OrderDishes
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_orderDishes == null)
|
||||||
|
{
|
||||||
|
_orderDishes = Dishes
|
||||||
|
.ToDictionary(recOD => recOD.DishId, recOD => (recOD.Dish as IDishModel, recOD.Count));
|
||||||
|
}
|
||||||
|
return _orderDishes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[ForeignKey("OrderId")]
|
[ForeignKey("OrderId")]
|
||||||
public virtual List<OrderLunch> OrderLunches { get; set; } = new();
|
public virtual List<OrderDish> Dishes { get; set; } = new();
|
||||||
[ForeignKey("OrderId")]
|
|
||||||
public virtual List<OrderCook> OrderCooks { get; set; } = new();
|
public static Order Create(OrderBindingModel model)
|
||||||
[ForeignKey("OrderId")]
|
{
|
||||||
public virtual List<OrderDish> OrderDishes { get; set; } = new();
|
if (model == null)
|
||||||
[ForeignKey("OrderId")]
|
{
|
||||||
public virtual List<OrderTableware> Orderablewares { get; set; } = new();
|
return null;
|
||||||
|
}
|
||||||
|
return new Order()
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
VisitorId = model.VisitorId,
|
||||||
|
Description = model.Description,
|
||||||
|
Sum = model.Sum
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(OrderBindingModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
VisitorId = model.VisitorId;
|
||||||
|
Description = model.Description;
|
||||||
|
Sum = model.Sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderViewModel GetViewModel => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
VisitorId = VisitorId,
|
||||||
|
Description = Description,
|
||||||
|
Sum = Sum,
|
||||||
|
OrderDishes = OrderDishes
|
||||||
|
};
|
||||||
|
public void UpdateOrderDish(CanteenDatabase context, DishBindingModel model)
|
||||||
|
{
|
||||||
|
var orderDish = context.OrderDish.Where(rec => rec.DishId == model.Id).ToList();
|
||||||
|
if (orderDish != null && (orderDish.Count > 0))
|
||||||
|
{
|
||||||
|
context.OrderDish.RemoveRange(orderDish.Where(rec => !model.OrderDishes.ContainsKey(rec.DishesId)));
|
||||||
|
context.SaveChanges();
|
||||||
|
foreach (var updateDish in orderDish)
|
||||||
|
{
|
||||||
|
updateDish.CountProduct = model.OrderDishes[updateDish.DishesId].Item2;
|
||||||
|
model.OrderDishes.Remove(updateDish.DishesId);
|
||||||
|
}
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
var dish = context.Dishes.First(x => x.Id == Id);
|
||||||
|
foreach (var dp in model.OrderDishes)
|
||||||
|
{
|
||||||
|
context.OrderDish.Add(new DishProduct
|
||||||
|
{
|
||||||
|
Dish = dish,
|
||||||
|
Product = context.Products.First(x => x.Id == dp.Key),
|
||||||
|
CountProduct = dp.Value.Item2
|
||||||
|
});
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
_orderDishes = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
22
Canteen/CanteenDatabaseImplement/Models/OrderDish.cs
Normal file
22
Canteen/CanteenDatabaseImplement/Models/OrderDish.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace CanteenDatabaseImplement.Models
|
||||||
|
{
|
||||||
|
public class OrderDish
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
[Required]
|
||||||
|
public int OrderId { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public int DishId { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public int Count { get; set; }
|
||||||
|
|
||||||
|
public virtual Order Order { get; set; } = new();
|
||||||
|
|
||||||
|
public virtual Dish Dish { get; set; } = new();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -6,16 +6,99 @@ using System.ComponentModel.DataAnnotations.Schema;
|
|||||||
|
|
||||||
namespace CanteenDatabaseImplement.Models
|
namespace CanteenDatabaseImplement.Models
|
||||||
{
|
{
|
||||||
|
//public class Product : IProductModel
|
||||||
|
//{
|
||||||
|
// public int Id { get; private set; }
|
||||||
|
// [Required]
|
||||||
|
// public string ProductName { get; private set; } = string.Empty;
|
||||||
|
// [Required]
|
||||||
|
// public double Cost { get; private set; }
|
||||||
|
// [Required]
|
||||||
|
// public int ManagerId { get; private set; }
|
||||||
|
// private Dictionary<int, ICookModel>? _productCooks = null;
|
||||||
|
// [NotMapped]
|
||||||
|
// public Dictionary<int, ICookModel> ProductCooks
|
||||||
|
// {
|
||||||
|
// get
|
||||||
|
// {
|
||||||
|
// if (_productCooks == null)
|
||||||
|
// {
|
||||||
|
// _productCooks = Cooks.ToDictionary(record => record.CookId, record => record.Cook as ICookModel);
|
||||||
|
// }
|
||||||
|
// return _productCooks;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// [ForeignKey("ProductId")]
|
||||||
|
// public virtual List<ProductCook> Cooks { get; set; } = new();
|
||||||
|
// [ForeignKey("ProductId")]
|
||||||
|
// public virtual List<DishProduct> Dishes { get; set; } = new();
|
||||||
|
// public static Product? Create(CanteenDatabase context, ProductBindingModel model)
|
||||||
|
// {
|
||||||
|
// return new Product()
|
||||||
|
// {
|
||||||
|
// Id = model.Id,
|
||||||
|
// ProductName = model.ProductName,
|
||||||
|
// Cost = model.Cost,
|
||||||
|
// Cooks = model.ProductCooks.Select(x => new ProductCook
|
||||||
|
// {
|
||||||
|
// Cook = context.Cooks.First(y => y.Id == x.Key)
|
||||||
|
// }).ToList()
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void Update(ProductBindingModel model)
|
||||||
|
// {
|
||||||
|
// ProductName = model.ProductName;
|
||||||
|
// Cost = model.Cost;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public ProductViewModel GetViewModel => new()
|
||||||
|
// {
|
||||||
|
// Id = Id,
|
||||||
|
// ProductName = ProductName,
|
||||||
|
// Cost = Cost,
|
||||||
|
// ProductCooks = ProductCooks,
|
||||||
|
// ManagerId = ManagerId
|
||||||
|
// };
|
||||||
|
|
||||||
|
// public void UpdateCooks(CanteenDatabase context, ProductBindingModel model)
|
||||||
|
// {
|
||||||
|
// var productCooks = context.ProductCooks.Where(record => record.ProductId == model.Id).ToList();
|
||||||
|
// if (productCooks != null && productCooks.Count > 0)
|
||||||
|
// {
|
||||||
|
// context.ProductCooks.RemoveRange(productCooks.Where(record => !model.ProductCooks.ContainsKey(record.CookId)));
|
||||||
|
// context.SaveChanges();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// var product = context.Products.First(x => x.Id == Id);
|
||||||
|
// foreach (var pc in model.ProductCooks)
|
||||||
|
// {
|
||||||
|
// context.ProductCooks.Add(new ProductCook
|
||||||
|
// {
|
||||||
|
// Product = product,
|
||||||
|
// Cook = context.Cooks.First(x => x.Id == pc.Key),
|
||||||
|
// });
|
||||||
|
// context.SaveChanges();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// _productCooks = null;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
public class Product : IProductModel
|
public class Product : IProductModel
|
||||||
{
|
{
|
||||||
public int Id { get; private set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public string ProductName { get; private set; } = string.Empty;
|
public string ProductName { get; set; } = string.Empty;
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public double Cost { get; private set; }
|
public double Price { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public int ManagerId { get; private set; }
|
public int ManagerId { get; set; }
|
||||||
|
|
||||||
private Dictionary<int, ICookModel>? _productCooks = null;
|
private Dictionary<int, ICookModel>? _productCooks = null;
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
public Dictionary<int, ICookModel> ProductCooks
|
public Dictionary<int, ICookModel> ProductCooks
|
||||||
{
|
{
|
||||||
@ -23,25 +106,27 @@ namespace CanteenDatabaseImplement.Models
|
|||||||
{
|
{
|
||||||
if (_productCooks == null)
|
if (_productCooks == null)
|
||||||
{
|
{
|
||||||
_productCooks = Cooks.ToDictionary(record => record.CookId, record => record.Cook as ICookModel);
|
_productCooks = Cooks
|
||||||
|
.ToDictionary(recPC => recPC.CookId, recPC => (recPC.Cook as ICookModel));
|
||||||
}
|
}
|
||||||
return _productCooks;
|
return _productCooks;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ForeignKey("ProductId")]
|
[ForeignKey("ProductId")]
|
||||||
public virtual List<ProductCook> Cooks { get; set; } = new();
|
public virtual List<ProductCook> Cooks { get; set; } = new();
|
||||||
[ForeignKey("ProductId")]
|
|
||||||
public virtual List<DishProduct> Dishes { get; set; } = new();
|
public static Product Create(CanteenDatabase context, ProductBindingModel model)
|
||||||
public static Product? Create(CanteenDatabase context, ProductBindingModel model)
|
|
||||||
{
|
{
|
||||||
return new Product()
|
return new Product
|
||||||
{
|
{
|
||||||
Id = model.Id,
|
Id = model.Id,
|
||||||
ProductName = model.ProductName,
|
ProductName = model.ProductName,
|
||||||
Cost = model.Cost,
|
Price = model.Price,
|
||||||
|
ManagerId = model.ManagerId,
|
||||||
Cooks = model.ProductCooks.Select(x => new ProductCook
|
Cooks = model.ProductCooks.Select(x => new ProductCook
|
||||||
{
|
{
|
||||||
Cook = context.Cooks.First(y => y.Id == x.Key)
|
Cook = context.Cooks.First(y => y.Id == x.Key),
|
||||||
}).ToList()
|
}).ToList()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -49,38 +134,39 @@ namespace CanteenDatabaseImplement.Models
|
|||||||
public void Update(ProductBindingModel model)
|
public void Update(ProductBindingModel model)
|
||||||
{
|
{
|
||||||
ProductName = model.ProductName;
|
ProductName = model.ProductName;
|
||||||
Cost = model.Cost;
|
Price = model.Price;
|
||||||
|
ManagerId = model.ManagerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProductViewModel GetViewModel => new()
|
public ProductViewModel GetViewModel => new()
|
||||||
{
|
{
|
||||||
Id = Id,
|
Id = Id,
|
||||||
ProductName = ProductName,
|
ProductName = ProductName,
|
||||||
Cost = Cost,
|
Price = Price,
|
||||||
ProductCooks = ProductCooks,
|
ManagerId = ManagerId,
|
||||||
ManagerId = ManagerId
|
ProductCooks = ProductCooks
|
||||||
};
|
};
|
||||||
|
|
||||||
public void UpdateCooks(CanteenDatabase context, ProductBindingModel model)
|
public void UpdateProductCooks(CanteenDatabase context, ProductBindingModel model)
|
||||||
{
|
{
|
||||||
var productCooks = context.ProductCooks.Where(record => record.ProductId == model.Id).ToList();
|
var productCookers = context.ProductCook.Where(rec => rec.ProductId == model.Id).ToList();
|
||||||
if (productCooks != null && productCooks.Count > 0)
|
if (productCookers != null && (productCookers.Count() > 0))
|
||||||
{
|
{
|
||||||
context.ProductCooks.RemoveRange(productCooks.Where(record => !model.ProductCooks.ContainsKey(record.CookId)));
|
context.ProductCook.RemoveRange(productCookers.Where(rec => !model.ProductCooks.ContainsKey(rec.CookId)));
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
var product = context.Products.First(x => x.Id == Id);
|
var product = context.Products.First(x => x.Id == Id);
|
||||||
|
|
||||||
foreach (var pc in model.ProductCooks)
|
foreach (var pc in model.ProductCooks)
|
||||||
{
|
{
|
||||||
context.ProductCooks.Add(new ProductCook
|
context.ProductCook.Add(new ProductCook
|
||||||
{
|
{
|
||||||
Product = product,
|
Product = product,
|
||||||
Cook = context.Cooks.First(x => x.Id == pc.Key),
|
Cook = context.Cooks.First(x => x.Id == pc.Key)
|
||||||
});
|
});
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
_productCooks = null;
|
_productCooks = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user