From 1203148896cdefb98221c66642ef57c257d2e59c Mon Sep 17 00:00:00 2001 From: ValAnn Date: Wed, 27 Mar 2024 14:25:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ShopLogic.cs | 10 + .../Implements/ShopStorage.cs | 185 ++++++++++++++++ .../20240313082356_InitialCreate.cs | 206 +++++++++--------- 3 files changed, 298 insertions(+), 103 deletions(-) create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs diff --git a/SushiBar/SushiBarBusinessLogic_/BusinessLogics/ShopLogic.cs b/SushiBar/SushiBarBusinessLogic_/BusinessLogics/ShopLogic.cs index 47cfd25..f14617f 100644 --- a/SushiBar/SushiBarBusinessLogic_/BusinessLogics/ShopLogic.cs +++ b/SushiBar/SushiBarBusinessLogic_/BusinessLogics/ShopLogic.cs @@ -125,6 +125,16 @@ namespace SushiBarBusinessLogic.BusinessLogics } shop.ShopSushis.Add(model.SushiId, (sushi, model.Count)); } + + _shopStorage.Update(new ShopBindingModel() + { + Id = shop.Id, + ShopName = shop.ShopName, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + ShopSushis = shop.ShopSushis, + SushiMaxCount = shop.SushiMaxCount, + }); return true; } diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f0f1fd1 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,185 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi).ToList(). + Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new SushiBarDatabase(); + return context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi).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 new(); + } + using var context = new SushiBarDatabase(); + return context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi) + .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 SushiBarDatabase(); + 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 SushiBarDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateSushis(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new SushiBarDatabase(); + var shop = context.Shops.Include(x => x.Sushis).FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + context.Shops.Remove(shop); + context.SaveChanges(); + return shop.GetViewModel; + } + return null; + } + + public bool RestockingShops(SupplyBindingModel model) + { + using var context = new SushiBarDatabase(); + var transaction = context.Database.BeginTransaction(); + var Shops = context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi).ToList(). + Where(x => x.SushiMaxCount > x.ShopSushis.Select(x => x.Value.Item2).Sum()).ToList(); + if (model == null) + { + return false; + } + try + { + foreach (Shop shop in Shops) + { + int difference = shop.SushiMaxCount - shop.ShopSushis.Select(x => x.Value.Item2).Sum(); + int refill = Math.Min(difference, model.Count); + model.Count -= refill; + if (shop.ShopSushis.ContainsKey(model.SushiId)) + { + var datePair = shop.ShopSushis[model.SushiId]; + datePair.Item2 += refill; + shop.ShopSushis[model.SushiId] = datePair; + } + else + { + var sushi = context.Sushis.First(x => x.Id == model.SushiId); + shop.ShopSushis.Add(model.SushiId, (sushi, refill)); + } + shop.SushisDictionatyUpdate(context); + if (model.Count == 0) + { + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public bool Sale(SupplySearchModel model) + { + using var context = new SushiBarDatabase(); + var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi).ToList(). + Where(x => x.ShopSushis.ContainsKey(model.SushiId.Value)).OrderByDescending(x => x.ShopSushis[model.SushiId.Value].Item2).ToList(); + + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.ShopSushis[model.SushiId.Value].Item2; + if (residue > 0) + { + shop.ShopSushis.Remove(model.SushiId.Value); + shop.SushisDictionatyUpdate(context); + context.SaveChanges(); + model.Count = residue; + + } + else + { + if (residue == 0) + shop.ShopSushis.Remove(model.SushiId.Value); + else + { + var dataPair = shop.ShopSushis[model.SushiId.Value]; + dataPair.Item2 = -residue; + shop.ShopSushis[model.SushiId.Value] = dataPair; + } + + shop.SushisDictionatyUpdate(context); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs index 4367175..3ffad67 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs @@ -8,118 +8,118 @@ namespace SushiBarDatabaseImplement.Migrations /// public partial class InitialCreate : Migration { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Components", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ComponentName = table.Column(type: "nvarchar(max)", nullable: false), - Cost = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Components", x => x.Id); - }); + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); - migrationBuilder.CreateTable( - name: "Sushis", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - SushiName = table.Column(type: "nvarchar(max)", nullable: false), - Price = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Sushis", x => x.Id); - }); + migrationBuilder.CreateTable( + name: "Sushis", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SushiName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Sushis", x => x.Id); + }); - migrationBuilder.CreateTable( - name: "Orders", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - SushiId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false), - Sum = table.Column(type: "float", nullable: false), - Status = table.Column(type: "int", nullable: false), - DateCreate = table.Column(type: "datetime2", nullable: false), - DateImplement = table.Column(type: "datetime2", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Orders", x => x.Id); - table.ForeignKey( - name: "FK_Orders_Sushis_SushiId", - column: x => x.SushiId, - principalTable: "Sushis", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SushiId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Sushis_SushiId", + column: x => x.SushiId, + principalTable: "Sushis", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); - migrationBuilder.CreateTable( - name: "SushiComponents", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - SushiId = table.Column(type: "int", nullable: false), - ComponentId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_SushiComponents", x => x.Id); - table.ForeignKey( - name: "FK_SushiComponents_Components_ComponentId", - column: x => x.ComponentId, - principalTable: "Components", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_SushiComponents_Sushis_SushiId", - column: x => x.SushiId, - principalTable: "Sushis", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); + migrationBuilder.CreateTable( + name: "SushiComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SushiId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SushiComponents", x => x.Id); + table.ForeignKey( + name: "FK_SushiComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SushiComponents_Sushis_SushiId", + column: x => x.SushiId, + principalTable: "Sushis", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); - migrationBuilder.CreateIndex( - name: "IX_Orders_SushiId", - table: "Orders", - column: "SushiId"); + migrationBuilder.CreateIndex( + name: "IX_Orders_SushiId", + table: "Orders", + column: "SushiId"); - migrationBuilder.CreateIndex( - name: "IX_SushiComponents_ComponentId", - table: "SushiComponents", - column: "ComponentId"); + migrationBuilder.CreateIndex( + name: "IX_SushiComponents_ComponentId", + table: "SushiComponents", + column: "ComponentId"); - migrationBuilder.CreateIndex( - name: "IX_SushiComponents_SushiId", - table: "SushiComponents", - column: "SushiId"); - } + migrationBuilder.CreateIndex( + name: "IX_SushiComponents_SushiId", + table: "SushiComponents", + column: "SushiId"); + } - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Orders"); + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); - migrationBuilder.DropTable( - name: "SushiComponents"); + migrationBuilder.DropTable( + name: "SushiComponents"); - migrationBuilder.DropTable( - name: "Components"); + migrationBuilder.DropTable( + name: "Components"); - migrationBuilder.DropTable( - name: "Sushis"); + migrationBuilder.DropTable( + name: "Sushis"); + } } } -}