From d572253caf01dc2614c3e299e06ace9b72a3c3c0 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Thu, 23 Mar 2023 23:44:29 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 28 +-- .../Implements/ShopStorage.cs | 178 ++++++++++++++++++ 2 files changed, 183 insertions(+), 23 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs index 8dfa776..d9366e5 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -55,38 +55,19 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements using var context = new BlacksmithWorkshopDatabase(); return context.Orders + .Include(x => x.Manufacture) .Where(x => x.Id == model.Id) .Select(x => x.GetViewModel) .ToList(); } - private static OrderViewModel GetViewModel(Order order) - { - var viewModel = order.GetViewModel; - using var context = new BlacksmithWorkshopDatabase(); - var element = context.Manufactures - .FirstOrDefault(x => x.Id == order.ManufactureId); - viewModel.ManufactureName = element.ManufactureName; - - return viewModel; - } - public List GetFullList() { using var context = new BlacksmithWorkshopDatabase(); return context.Orders - .Select(x => new OrderViewModel - { - Id = x.Id, - ManufactureId = x.ManufactureId, - Count = x.Count, - Sum = x.Sum, - Status = x.Status, - DateCreate = x.DateCreate, - DateImplement = x.DateImplement, - ManufactureName = x.Manufacture.ManufactureName - }) + .Include(x => x.Manufacture) + .Select(x => x.GetViewModel) .ToList(); } @@ -103,7 +84,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; + return context.Orders.Include(x => x.Manufacture) + .FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..5724d6d --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,178 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; +using BlacksmithWorkshopDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + internal class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new ManufactureShopDatabase(); + return context.Shops + .Include(x => x.Manufactures) + .ThenInclude(x => x.Manufacture) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + + using var context = new ManufactureShopDatabase(); + + return context.Shops + .Include(x => x.Manufactures) + .ThenInclude(x => x.Manufacture) + .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 ManufactureShopDatabase(); + + return context.Shops + .Include(x => x.Manufactures) + .ThenInclude(x => x.Manufacture) + .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 ManufactureShopDatabase(); + 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 ManufactureShopDatabase(); + 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.UpdateIceCreams(context, model); + transaction.Commit(); + + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + + throw; + } + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ManufactureShopDatabase(); + + var element = context.Shops + .Include(x => x.Manufactures) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public bool SellManufactures(IManufactureModel model, int count) + { + using var context = new ManufactureShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var shops = context.ShopManufactures + .Include(x => x.Shop) + .ToList() + .Where(rec => rec.ManufactureId == model.Id); + + if (shops == null) + { + return false; + } + + foreach (var shop in shops) + { + if (shop.Count < count) + { + count -= shop.Count; + shop.Count = 0; + } + else + { + shop.Count = shop.Count - count; + count -= count; + } + + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + + return true; + } + } + + transaction.Rollback(); + + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +}