From f1ff090bc19537e4dcbc657f4d2d0cc76984368c Mon Sep 17 00:00:00 2001 From: parap Date: Sun, 23 Apr 2023 20:30:20 +0400 Subject: [PATCH] lab3 start --- .../Implements/ShopStorage.cs | 152 ++++++++++++++++++ .../PizzeriaDatabase.cs | 2 + .../PizzeriaDatabaseImplement/models/Shop.cs | 105 ++++++++++++ .../models/ShopPizza.cs | 17 ++ 4 files changed, 276 insertions(+) create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Implements/ShopStorage.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/models/Shop.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/models/ShopPizza.cs diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..0eac63c --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,152 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.models; +using Microsoft.EntityFrameworkCore; +using PizzeriaDataModels; + +namespace PizzeriaDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Shops + .Include(x => x.Pizzas) + .ThenInclude(x => x.Pizza) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Shops + .Include(x => x.Pizzas) + .ThenInclude(x => x.Pizza) + .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 PizzeriaDatabase(); + return context.Shops + .Include(x => x.Pizzas) + .ThenInclude(x => x.Pizza) + .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 PizzeriaDatabase(); + var newShop = Shop.Create(context, model); + + if (context.Shops.Any(x => x.ShopName == newShop.ShopName)) + { + throw new Exception("Магазин с таким названием уже существует"); + } + + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new PizzeriaDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => + rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdatePizzas(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new PizzeriaDatabase(); + var element = context.Shops + .Include(x => x.Pizzas) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public bool TrySellPizza(IPizzaModel pizza, int count) + { + using var context = new PizzeriaDatabase(); + using var transaction = context.Database.BeginTransaction(); + + foreach (var shopPizzas in context.ShopPizzas.Where(x => x.PizzaId == pizza.Id)) + { + var min = Math.Min(count, shopPizzas.Count); + shopPizzas.Count -= min; + count -= min; + + if (count <= 0) + { + break; + } + } + + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + + else + { + transaction.Rollback(); + } + + if (count > 0) + { + return false; + } + + return true; + } + + public bool TryRestoreShops(IPizzaModel pizza, int count) + { + throw new NotImplementedException(); + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs index d7d0ce0..49b0346 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -15,7 +15,9 @@ namespace PizzeriaDatabaseImplement } public virtual DbSet Components { set; get; } public virtual DbSet Pizzas { set; get; } + public virtual DbSet Shops { set; get; } public virtual DbSet PizzaComponents { set; get; } + public virtual DbSet ShopPizzas { set; get; } public virtual DbSet Orders { set; get; } } } \ No newline at end of file diff --git a/Pizzeria/PizzeriaDatabaseImplement/models/Shop.cs b/Pizzeria/PizzeriaDatabaseImplement/models/Shop.cs new file mode 100644 index 0000000..e67e78d --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/models/Shop.cs @@ -0,0 +1,105 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; + +namespace PizzeriaDatabaseImplement.models +{ + public class Shop : IShopModel + { + [Required] + public int Id { get; set; } + [Required] + public string ShopName { get; set; } = string.Empty; + [Required] + public string Addres { get; set; } = string.Empty; + [Required] + public DateTime OpenTime { get; set; } + [Required] + public int Capacity { get; set; } + private Dictionary? _shopPizzas = null; + [NotMapped] + public Dictionary ShopPizzas + { + get + { + if (_shopPizzas == null) + { + _shopPizzas = Pizzas.ToDictionary(x => x.PizzaId, x => (x.Pizza as IPizzaModel, x.Count)); + } + return _shopPizzas; + } + } + [ForeignKey("ShopId")] + public virtual List Pizzas { get; set; } = new(); + public static Shop Create(PizzeriaDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Addres = model.Addres, + OpenTime = model.OpenTime, + Capacity = model.Capacity, + Pizzas = model.ShopPizzas.Select(x => new + ShopPizza + { + Pizza = context.Pizzas.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ShopBindingModel model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Addres = model.Addres; + OpenTime = model.OpenTime; + Capacity = model.Capacity; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Addres = Addres, + OpenTime = OpenTime, + Capacity = Capacity, + ShopPizzas = ShopPizzas + }; + public void UpdatePizzas(PizzeriaDatabase context, ShopBindingModel model) + { + var shopPizzas = context.ShopPizzas.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopPizzas != null && shopPizzas.Count > 0) + { // удалили те, которых нет в модели + context.ShopPizzas.RemoveRange(shopPizzas.Where(rec + => !model.ShopPizzas.ContainsKey(rec.PizzaId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updatePizza in shopPizzas) + { + updatePizza.Count = + model.ShopPizzas[updatePizza.PizzaId].Item2; + model.ShopPizzas.Remove(updatePizza.PizzaId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var pc in model.ShopPizzas) + { + context.ShopPizzas.Add(new ShopPizza + { + Shop = shop, + Pizza = context.Pizzas.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _shopPizzas = null; + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/models/ShopPizza.cs b/Pizzeria/PizzeriaDatabaseImplement/models/ShopPizza.cs new file mode 100644 index 0000000..34104b3 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/models/ShopPizza.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace PizzeriaDatabaseImplement.models +{ + public class ShopPizza + { + 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(); + } +}