From 5c96df79857c85c3b4413a660123ab40561b6f6a Mon Sep 17 00:00:00 2001 From: shadowik Date: Thu, 6 Apr 2023 02:15:40 +0400 Subject: [PATCH] Fix --- .../PizzeriaDataModels/Models/IShopModel.cs | 1 + .../PizzeriaDatabaseImplement/Models/Shop.cs | 118 ++++++++++++++++++ .../Models/ShopPizzas.cs | 27 ++++ Pizzeria/PizzeriaListImplement/Models/Shop.cs | 9 +- 4 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Models/Shop.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Models/ShopPizzas.cs diff --git a/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs b/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs index 893ee87..68c761e 100644 --- a/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs +++ b/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs @@ -14,5 +14,6 @@ namespace PizzeriaDataModels.Models string Adress { get; } DateTime OpeningDate { get; } Dictionary ShopPizzas { get; } + public int PizzaMaxCount { get; } } } diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Shop.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..a76f330 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Shop.cs @@ -0,0 +1,118 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } = String.Empty; + + public string Adress { get; set; } = String.Empty; + + public DateTime OpeningDate { get; set; } + + private Dictionary? _shopPizzas = null; + public Dictionary ShopPizzas + { + get + { + if (_shopPizzas == null) { + if (_shopPizzas == null) + { + _shopPizzas = Pizzas + .ToDictionary(recSP => recSP.PizzaId, recSP => (recSP.Pizza as IPizzaModel, recSP.Count)); + } + return _shopPizzas; + } + return _shopPizzas; + } + } + + [ForeignKey("ShopId")] + public List Pizzas { get; set; } = new(); + + public static Shop Create(PizzeriaDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + Pizzas = model.ShopPizzas.Select(x => new ShopPizzas + { + Pizza = context.Pizzas.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + Piz = model.ReinforcedMaxCount + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + ReinforcedMaxCount = model.ReinforcedMaxCount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopReinforcedes = ShopReinforcedes, + ReinforcedMaxCount = ReinforcedMaxCount + }; + + public void UpdateReinforcedes(PrecastConcretePlantDatabase context, ShopBindingModel model) + { + var ShopReinforcedes = context.ShopReinforcedes.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopReinforcedes != null && ShopReinforcedes.Count > 0) + { + // удалили те, которых нет в модели + context.ShopReinforcedes.RemoveRange(ShopReinforcedes.Where(rec => !model.ShopReinforcedes.ContainsKey(rec.ReinforcedId))); + context.SaveChanges(); + ShopReinforcedes = context.ShopReinforcedes.Where(rec => rec.ShopId == model.Id).ToList(); + // обновили количество у существующих записей + foreach (var updateReinforced in ShopReinforcedes) + { + updateReinforced.Count = model.ShopReinforcedes[updateReinforced.ReinforcedId].Item2; + model.ShopReinforcedes.Remove(updateReinforced.ReinforcedId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ar in model.ShopReinforcedes) + { + context.ShopReinforcedes.Add(new ShopReinforced + { + Shop = shop, + Reinforced = context.Reinforceds.First(x => x.Id == ar.Key), + Count = ar.Value.Item2 + }); + context.SaveChanges(); + } + _shopReinforcedes = null; + } + + public void ReinforcedDictionatyUpdate(PrecastConcretePlantDatabase context) + { + UpdateReinforcedes(context, new ShopBindingModel + { + Id = Id, + ShopReinforcedes = ShopReinforcedes, + }); + } + + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/ShopPizzas.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/ShopPizzas.cs new file mode 100644 index 0000000..88f5740 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/ShopPizzas.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaDatabaseImplement.Models +{ + public class ShopPizzas + { + public int Id { get; set; } + + [Required] + public int PizzaId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Pizza Pizza { get; set; } = new(); + } +} diff --git a/Pizzeria/PizzeriaListImplement/Models/Shop.cs b/Pizzeria/PizzeriaListImplement/Models/Shop.cs index a145a53..27da212 100644 --- a/Pizzeria/PizzeriaListImplement/Models/Shop.cs +++ b/Pizzeria/PizzeriaListImplement/Models/Shop.cs @@ -16,6 +16,7 @@ namespace PizzeriaListImplement.Models public string Adress { get; private set; } = string.Empty; public DateTime OpeningDate { get; private set; } public Dictionary ShopPizzas { get; private set; } = new(); + public int PizzaMaxCount { get; private set; } public static Shop? Create(ShopBindingModel? model) { if (model == null) @@ -27,7 +28,8 @@ namespace PizzeriaListImplement.Models Id = model.Id, ShopName = model.ShopName, Adress = model.Adress, - OpeningDate = model.OpeningDate + OpeningDate = model.OpeningDate, + PizzaMaxCount = model.PizzaMaxCount, }; } public void Update(ShopBindingModel? model) @@ -39,6 +41,7 @@ namespace PizzeriaListImplement.Models ShopName = model.ShopName; Adress = model.Adress; OpeningDate = model.OpeningDate; + PizzaMaxCount = model.PizzaMaxCount; } public ShopViewModel GetViewModel => new() { @@ -46,8 +49,10 @@ namespace PizzeriaListImplement.Models ShopName = ShopName, Adress = Adress, OpeningDate = OpeningDate, - ShopPizzas = ShopPizzas + ShopPizzas = ShopPizzas, + PizzaMaxCount = PizzaMaxCount, }; + } }