From 0157390a30f3a4b72c59884c06a701ba9c8c74ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Thu, 23 Mar 2023 20:29:55 +0400 Subject: [PATCH] =?UTF-8?q?=D1=87=D1=91-=D1=82=D0=BE=20=D1=81=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D0=BB=20=D0=BC=D0=B1=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FoodOrdersDatabase.cs | 4 + .../Implements/ShopStorage.cs | 154 ++++++++++++++++++ .../Models/Shop.cs | 124 ++++++++++++++ .../Models/ShopDish.cs | 27 +++ 4 files changed, 309 insertions(+) create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Implements/ShopStorage.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Models/Shop.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Models/ShopDish.cs diff --git a/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabase.cs b/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabase.cs index 5bf8737..42d89ca 100644 --- a/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabase.cs +++ b/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabase.cs @@ -21,5 +21,9 @@ namespace FoodOrdersDatabaseImplement public virtual DbSet DishComponents { set; get; } public virtual DbSet Orders { set; get; } + + public virtual DbSet Shops { set; get; } + + public virtual DbSet ShopDishes { set; get; } } } \ No newline at end of file diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Implements/ShopStorage.cs b/FoodOrders/FoodOrdersDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..b2e28ad --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,154 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.SearchModels; +using FoodOrdersContracts.StoragesContracts; +using FoodOrdersContracts.ViewModels; +using FoodOrdersDatabaseImplement; +using FoodOrdersDataModels.Models; +using FoodOrdersDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace FoodOrdersDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + + + public List GetFullList() + { + using var context = new FoodOrdersDatabase(); + return context.Shops + .Include(x => x.Dishes) + .ThenInclude(x => x.Dish) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new FoodOrdersDatabase(); + return context.Shops + .Include(x => x.Dishes) + .ThenInclude(x => x.Dish) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new FoodOrdersDatabase(); + return context.Shops + .Include(x => x.Dishes) + .ThenInclude(x => x.Dish) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new FoodOrdersDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new FoodOrdersDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var updateShop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (updateShop == null) + { + return null; + } + updateShop.Update(model); + context.SaveChanges(); + updateShop.UpdateDish(context, model); + transaction.Commit(); + return updateShop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new FoodOrdersDatabase(); + var element = context.Shops + .Include(x => x.Dishes) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public bool SellDishes(IDishModel dish, int count) + { + using var context = new FoodOrdersDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + List ListShopDish = context.ShopDishes + .Include(x => x.Shop) + .Where(y => y.DishId == dish.Id) + .ToList(); + + if (ListShopDish == null) return false; + + foreach (var shopDish in ListShopDish) + { + if (count - shopDish.Count >= 0) + { + count -= shopDish.Count; + ListShopDish.Remove(shopDish); + } + else + { + shopDish.Count -= count; + count = 0; + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Models/Shop.cs b/FoodOrders/FoodOrdersDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..2b7f2f5 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Models/Shop.cs @@ -0,0 +1,124 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.ViewModels; +using FoodOrdersDatabaseImplement.Models; +using FoodOrdersDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FoodOrdersDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + [Required] + public string ShopName { get; private set; } = string.Empty; + + [Required] + public string Address { get; private set; } = string.Empty; + + [Required] + public DateTime DateOfOpening { get; private set; } = DateTime.Now; + + [Required] + public int Capacity { get; private set; } = 0; + + [NotMapped] + + public Dictionary? _shopDishes = null; + + [NotMapped] + public Dictionary ShopDishes + { + get + { + if (_shopDishes == null) + { + _shopDishes = Dishes.ToDictionary(recSD => recSD.DishId, recSD => (recSD.Dish as IDishModel, recSD.Count)); + } + return _shopDishes; + } + } + + [ForeignKey("ShopId")] + public virtual List Dishes { get; set; } = new(); + + public static Shop? Create(FoodOrdersDatabase context, ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOfOpening = model.DateOfOpening, + Dishes = model.ShopDishes.Select(x => new ShopDish + { + Dish = context.Dishes.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + Capacity = model.Capacity + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOfOpening = model.DateOfOpening; + Capacity = model.Capacity; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOfOpening = DateOfOpening, + ShopDishes = ShopDishes, + Capacity = Capacity + }; + public void UpdateDish(FoodOrdersDatabase context, ShopBindingModel model) + { + var shopDishes = context.ShopDishes.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopDishes != null && shopDishes.Count > 0) + { // удалили те в бд, которых нет в модели + context.ShopDishes.RemoveRange(shopDishes.Where(rec => !model.ShopDishes.ContainsKey(rec.DishId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateDish in shopDishes) + { + updateDish.Count = model.ShopDishes[updateDish.DishId].Item2; + model.ShopDishes.Remove(updateDish.DishId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + //добавляем в бд блюда которые есть в моделе, но ещё нет в бд + foreach (var sd in model.ShopDishes) + { + context.ShopDishes.Add(new ShopDish + { + Shop = shop, + Dish = context.Dishes.First(x => x.Id == sd.Key), + Count = sd.Value.Item2 + }); + context.SaveChanges(); + } + _shopDishes = null; + } + } +} diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Models/ShopDish.cs b/FoodOrders/FoodOrdersDatabaseImplement/Models/ShopDish.cs new file mode 100644 index 0000000..fce5abc --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Models/ShopDish.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 FoodOrdersDatabaseImplement.Models +{ + public class ShopDish + { + public int Id { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int DishId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Dish Dish { get; set; } = new(); + } +}