From 27a19ce7f099b5022faebab1014b546214a66cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=91=D0=B0=D1=82=D1=8B?= =?UTF-8?q?=D0=BB=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Fri, 7 Apr 2023 23:35:33 +0400 Subject: [PATCH] partially implemented storage --- .../BindingModels/DishBindingModel.cs | 2 +- .../BindingModels/ProductBindingModel.cs | 2 +- .../ViewModels/DishViewModel.cs | 2 +- .../ViewModels/OrderViewModel.cs | 2 +- .../CanteenDataModels/Models/IDishModel.cs | 2 +- .../CanteenDatabase.cs | 10 +- .../CanteenDatabaseImplement/Models/Cook.cs | 48 +++++- .../CanteenDatabaseImplement/Models/Dish.cs | 147 ++++++++++++++---- .../Models/DishProduct.cs | 2 +- .../CanteenDatabaseImplement/Models/Order.cs | 98 ++++++++++-- .../Models/OrderDish.cs | 22 +++ .../Models/Product.cs | 130 +++++++++++++--- 12 files changed, 381 insertions(+), 86 deletions(-) create mode 100644 Canteen/CanteenDatabaseImplement/Models/OrderDish.cs diff --git a/Canteen/CanteenContracts/BindingModels/DishBindingModel.cs b/Canteen/CanteenContracts/BindingModels/DishBindingModel.cs index 6c910fc..17ff7a1 100644 --- a/Canteen/CanteenContracts/BindingModels/DishBindingModel.cs +++ b/Canteen/CanteenContracts/BindingModels/DishBindingModel.cs @@ -13,7 +13,7 @@ namespace CanteenContracts.BindingModels public string DishName { get; set; } = string.Empty; public double Price { get; set; } public int ManagerId { get; set; } - public Dictionary DishProducts { get; set; } = new(); + public Dictionary OrderDishes { get; set; } = new(); } } diff --git a/Canteen/CanteenContracts/BindingModels/ProductBindingModel.cs b/Canteen/CanteenContracts/BindingModels/ProductBindingModel.cs index 15d507c..a074659 100644 --- a/Canteen/CanteenContracts/BindingModels/ProductBindingModel.cs +++ b/Canteen/CanteenContracts/BindingModels/ProductBindingModel.cs @@ -13,6 +13,6 @@ namespace CanteenContracts.BindingModels public string ProductName { get; set; } = string.Empty; public double Price { get; set; } public int ManagerId { get; set; } - public Dictionary ProductCooks { get; set; } = new Dictionary(); + public Dictionary ProductCooks { get; set; } = new(); } } diff --git a/Canteen/CanteenContracts/ViewModels/DishViewModel.cs b/Canteen/CanteenContracts/ViewModels/DishViewModel.cs index 8ea7f6d..18c1e27 100644 --- a/Canteen/CanteenContracts/ViewModels/DishViewModel.cs +++ b/Canteen/CanteenContracts/ViewModels/DishViewModel.cs @@ -17,6 +17,6 @@ namespace CanteenContracts.View [DisplayName("ID менеджера")] public int ManagerId { get; set; } public int Id { get; set; } - public Dictionary DishProducts { get; set; } + public Dictionary OrderDishes { get; set; } } } diff --git a/Canteen/CanteenContracts/ViewModels/OrderViewModel.cs b/Canteen/CanteenContracts/ViewModels/OrderViewModel.cs index e29d616..e866ce3 100644 --- a/Canteen/CanteenContracts/ViewModels/OrderViewModel.cs +++ b/Canteen/CanteenContracts/ViewModels/OrderViewModel.cs @@ -17,8 +17,8 @@ namespace CanteenContracts.View public string Description { get; set; } = string.Empty; [DisplayName("Сумма")] public double? Sum { get; set; } - [DisplayName("Блюда в заказе")] public Dictionary OrderDishes { get; set; } = new Dictionary(); + [DisplayName("ID заказа")] public int Id { get; set; } } diff --git a/Canteen/CanteenDataModels/Models/IDishModel.cs b/Canteen/CanteenDataModels/Models/IDishModel.cs index ee1991f..0bb1f41 100644 --- a/Canteen/CanteenDataModels/Models/IDishModel.cs +++ b/Canteen/CanteenDataModels/Models/IDishModel.cs @@ -11,6 +11,6 @@ namespace CanteenDataModels.Models string DishName { get; } double Price { get; } int ManagerId { get; } - Dictionary DishProducts { get; } + Dictionary OrderDishes { get; } } } diff --git a/Canteen/CanteenDatabaseImplement/CanteenDatabase.cs b/Canteen/CanteenDatabaseImplement/CanteenDatabase.cs index 6548567..858125e 100644 --- a/Canteen/CanteenDatabaseImplement/CanteenDatabase.cs +++ b/Canteen/CanteenDatabaseImplement/CanteenDatabase.cs @@ -14,16 +14,16 @@ namespace CanteenDatabaseImplement base.OnConfiguring(optionsBuilder); } public virtual DbSet Cooks { set; get; } - public virtual DbSet OrderCooks { set; get; } + public virtual DbSet OrderCook { set; get; } public virtual DbSet Dishes { set; get; } public virtual DbSet Lunches { set; get; } - public virtual DbSet OrderLunches { set; get; } + public virtual DbSet OrderLunch { set; get; } public virtual DbSet Managers { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Products { set; get; } - public virtual DbSet ProductCooks { set; get; } - public virtual DbSet LunchProducts { set; get; } - public virtual DbSet DishProducts { set; get; } + public virtual DbSet ProductCook { set; get; } + public virtual DbSet LunchProduct { set; get; } + public virtual DbSet OrderDish { set; get; } public virtual DbSet Roles { set; get; } public virtual DbSet Tablewares { set; get; } public virtual DbSet Visitors { set; get; } diff --git a/Canteen/CanteenDatabaseImplement/Models/Cook.cs b/Canteen/CanteenDatabaseImplement/Models/Cook.cs index ad49cac..06c932b 100644 --- a/Canteen/CanteenDatabaseImplement/Models/Cook.cs +++ b/Canteen/CanteenDatabaseImplement/Models/Cook.cs @@ -1,4 +1,6 @@ -using CanteenDataModels.Models; +using CanteenContracts.BindingModels; +using CanteenContracts.View; +using CanteenDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -15,15 +17,45 @@ namespace CanteenDatabaseImplement.Models [Required] public int ManagerId { get; private set; } [Required] - public string Name { get; private set; } = string.Empty; - [Required] - public string Surname { get; private set; } = string.Empty; - public string? Patronymic { get; private set; } = string.Empty; + public string FIO { get; private set; } = string.Empty; [Required] public string Position { get; private set; } = string.Empty; [ForeignKey("CookId")] - public virtual List Products{ get; set; } = new(); - [ForeignKey("CookId")] - public virtual List Orders { get; set; } = new(); + public virtual List Products { 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 + }; } } diff --git a/Canteen/CanteenDatabaseImplement/Models/Dish.cs b/Canteen/CanteenDatabaseImplement/Models/Dish.cs index 67832bb..8d0f323 100644 --- a/Canteen/CanteenDatabaseImplement/Models/Dish.cs +++ b/Canteen/CanteenDatabaseImplement/Models/Dish.cs @@ -12,40 +12,121 @@ using System.Threading.Tasks; 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? _dishProduct = null; + // public Dictionary 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 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 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 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; } + public int Id { get; set; } + public string DishName { get; set; } = string.Empty; + public double Price { get; set; } + public int ManagerId { get; set; } + private Dictionary? _dishProducts = null; - public Dictionary DishProduct + [NotMapped] + public Dictionary OrderDishes { get { if (_dishProducts == null) { _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; } } + [ForeignKey("DishId")] public virtual List 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, DishName = model.DishName, - Cost = model.Cost, - Products = model.DishProducts.Select(x => new DishProduct + Price = model.Price, + 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() }; } @@ -53,40 +134,44 @@ namespace CanteenDatabaseImplement.Models public void Update(DishBindingModel model) { DishName = model.DishName; - Cost = model.Cost; + Price = model.Price; + ManagerId = model.ManagerId; } public DishViewModel GetViewModel => new() { Id = Id, DishName = DishName, - Cost = Cost, - DishProducts = DishProduct, - ManagerId = ManagerId + Price = Price, + ManagerId = ManagerId, + OrderDishes = OrderDishes }; - public Dictionary DishProducts => throw new NotImplementedException(); - - public void UpdateProduct(CanteenDatabase context, DishBindingModel model) + public void UpdateDishProduct(CanteenDatabase context, DishBindingModel model) { - var dishProducts = context.DishProducts.Where(record => record.ProductId == model.Id).ToList(); - if (dishProducts != null && dishProducts.Count > 0) + var dishProduct = context.OrderDish.Where(rec => rec.DishId == model.Id).ToList(); + 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(); } - - var product = context.Products.First(x => x.Id == Id); - foreach (var pc in model.DishProducts) + var dish = context.Dishes.First(x => x.Id == Id); + foreach (var dp in model.OrderDishes) { - context.DishProducts.Add(new DishProduct + context.OrderDish.Add(new DishProduct { - Product = product, - Dish = context.Dishes.First(x => x.Id == pc.Key), + Dish = dish, + Product = context.Products.First(x => x.Id == dp.Key), + CountProduct = dp.Value.Item2 }); context.SaveChanges(); } - _dishProducts = null; } } diff --git a/Canteen/CanteenDatabaseImplement/Models/DishProduct.cs b/Canteen/CanteenDatabaseImplement/Models/DishProduct.cs index 15e00c1..d464445 100644 --- a/Canteen/CanteenDatabaseImplement/Models/DishProduct.cs +++ b/Canteen/CanteenDatabaseImplement/Models/DishProduct.cs @@ -13,7 +13,7 @@ namespace CanteenDatabaseImplement.Models [Required] public int DishId { get; set; } [Required] - public int ProductId { get; set; } + public int DishesId { get; set; } [Required] public int CountProduct { get; set; } public virtual Dish Dish { get; set; } = new(); diff --git a/Canteen/CanteenDatabaseImplement/Models/Order.cs b/Canteen/CanteenDatabaseImplement/Models/Order.cs index a7f0af8..2e58c3c 100644 --- a/Canteen/CanteenDatabaseImplement/Models/Order.cs +++ b/Canteen/CanteenDatabaseImplement/Models/Order.cs @@ -1,4 +1,6 @@ -using CanteenDataModels.Enums; +using CanteenContracts.BindingModels; +using CanteenContracts.View; +using CanteenDataModels.Enums; using CanteenDataModels.Models; using System; using System.Collections.Generic; @@ -13,21 +15,89 @@ namespace CanteenDatabaseImplement.Models public class Order : IOrderModel { public int Id { get; private set; } - [Required] public int VisitorId { get; private set; } - [Required] public string Description { get; private set; } = string.Empty; - [Required] - public double Sum { get; private set; } - [Required] - public DateTime DateCreate { get; private set; } = DateTime.Now; + public double? Sum { get; private set; } + + private Dictionary? _orderDishes = null; + + [NotMapped] + public Dictionary OrderDishes + { + get + { + if (_orderDishes == null) + { + _orderDishes = Dishes + .ToDictionary(recOD => recOD.DishId, recOD => (recOD.Dish as IDishModel, recOD.Count)); + } + return _orderDishes; + } + } + [ForeignKey("OrderId")] - public virtual List OrderLunches { get; set; } = new(); - [ForeignKey("OrderId")] - public virtual List OrderCooks { get; set; } = new(); - [ForeignKey("OrderId")] - public virtual List OrderDishes { get; set; } = new(); - [ForeignKey("OrderId")] - public virtual List Orderablewares { get; set; } = new(); + public virtual List Dishes { get; set; } = new(); + + public static Order Create(OrderBindingModel model) + { + if (model == null) + { + 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; + } } } diff --git a/Canteen/CanteenDatabaseImplement/Models/OrderDish.cs b/Canteen/CanteenDatabaseImplement/Models/OrderDish.cs new file mode 100644 index 0000000..6f71499 --- /dev/null +++ b/Canteen/CanteenDatabaseImplement/Models/OrderDish.cs @@ -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(); + + } +} \ No newline at end of file diff --git a/Canteen/CanteenDatabaseImplement/Models/Product.cs b/Canteen/CanteenDatabaseImplement/Models/Product.cs index ce0bff2..1037885 100644 --- a/Canteen/CanteenDatabaseImplement/Models/Product.cs +++ b/Canteen/CanteenDatabaseImplement/Models/Product.cs @@ -6,16 +6,99 @@ using System.ComponentModel.DataAnnotations.Schema; 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? _productCooks = null; + // [NotMapped] + // public Dictionary ProductCooks + // { + // get + // { + // if (_productCooks == null) + // { + // _productCooks = Cooks.ToDictionary(record => record.CookId, record => record.Cook as ICookModel); + // } + // return _productCooks; + // } + // } + // [ForeignKey("ProductId")] + // public virtual List Cooks { get; set; } = new(); + // [ForeignKey("ProductId")] + // public virtual List 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 int Id { get; private set; } + public int Id { get; set; } + [Required] - public string ProductName { get; private set; } = string.Empty; + public string ProductName { get; set; } = string.Empty; + [Required] - public double Cost { get; private set; } + public double Price { get; set; } + [Required] - public int ManagerId { get; private set; } + public int ManagerId { get; set; } + private Dictionary? _productCooks = null; + [NotMapped] public Dictionary ProductCooks { @@ -23,25 +106,27 @@ namespace CanteenDatabaseImplement.Models { 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; } } + [ForeignKey("ProductId")] public virtual List Cooks { get; set; } = new(); - [ForeignKey("ProductId")] - public virtual List 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, ProductName = model.ProductName, - Cost = model.Cost, + Price = model.Price, + ManagerId = model.ManagerId, 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() }; } @@ -49,38 +134,39 @@ namespace CanteenDatabaseImplement.Models public void Update(ProductBindingModel model) { ProductName = model.ProductName; - Cost = model.Cost; + Price = model.Price; + ManagerId = model.ManagerId; } public ProductViewModel GetViewModel => new() { Id = Id, ProductName = ProductName, - Cost = Cost, - ProductCooks = ProductCooks, - ManagerId = ManagerId + Price = Price, + 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(); - if (productCooks != null && productCooks.Count > 0) + var productCookers = context.ProductCook.Where(rec => rec.ProductId == model.Id).ToList(); + 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(); } var product = context.Products.First(x => x.Id == Id); + foreach (var pc in model.ProductCooks) { - context.ProductCooks.Add(new ProductCook + context.ProductCook.Add(new ProductCook { Product = product, - Cook = context.Cooks.First(x => x.Id == pc.Key), + Cook = context.Cooks.First(x => x.Id == pc.Key) }); context.SaveChanges(); } - _productCooks = null; } }