diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs index 270da97..76e3d53 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs @@ -3,17 +3,20 @@ using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; using SushiBarDataModels.Models; +using System.Collections.Generic; +using System.Xml.Linq; namespace SushiBarDatabaseImplement.Implements { - internal class ShopStorage : IShopStorage + public class ShopStorage : IShopStorage { public List GetFullList() { using var context = new SushiBarDatabase(); return context.Shops - .Include(x => x.ListSushi) + .Include(x => x.ListSushiFk) .ThenInclude(x => x.Sushi) .ToList() .Select(x => x.GetViewModel) @@ -27,7 +30,7 @@ namespace SushiBarDatabaseImplement.Implements } using var context = new SushiBarDatabase(); return context.Shops - .Include(x => x.ListSushi) + .Include(x => x.ListSushiFk) .ThenInclude(x => x.Sushi) .Where(x => x.ShopName.Contains(model.ShopName)) .ToList() @@ -42,7 +45,7 @@ namespace SushiBarDatabaseImplement.Implements } using var context = new SushiBarDatabase(); return context.Shops - .Include(x => x.ListSushi) + .Include(x => x.ListSushiFk) .ThenInclude(x => x.Sushi) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id)) @@ -51,7 +54,7 @@ namespace SushiBarDatabaseImplement.Implements public ShopViewModel? Insert(ShopBindingModel model) { using var context = new SushiBarDatabase(); - var newShop = Sushi.Create(context, model); + var newShop = Shop.Create(context, model); if (newShop == null) { return null; @@ -107,19 +110,34 @@ namespace SushiBarDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - foreach (var shop in context.Shops.Where(x => x.ListSushi.ContainsKey(model.Id))) + foreach (var shop in context.Shops + .Include(x => x.ListSushiFk) + .ThenInclude(x => x.Sushi) + .ToList() + .Where(x => x.ListSushi.ContainsKey(model.Id))) { int countInCurrentShop = shop.ListSushi[model.Id].Item2; if (countInCurrentShop <= count) { - shop.ListSushi[model.Id] = (shop.ListSushi[model.Id].Item1, 0); + var elem = context.ShopSushi + .Where(x => x.SushiId == model.Id) + .FirstOrDefault(x => x.ShopId == shop.Id); + context.ShopSushi.Remove(elem); + shop.ListSushi.Remove(model.Id); count -= countInCurrentShop; } else { shop.ListSushi[model.Id] = (shop.ListSushi[model.Id].Item1, countInCurrentShop - count); count = 0; - + shop.UpdateSushi(context, new() + { + Id = shop.Id, + ListSushi = shop.ListSushi, + }); + } + if (count == 0) + { context.SaveChanges(); transaction.Commit(); return true; @@ -127,7 +145,7 @@ namespace SushiBarDatabaseImplement.Implements } transaction.Rollback(); return false; - } + } catch { transaction.Rollback(); diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.Designer.cs new file mode 100644 index 0000000..c9c560d --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SushiBarDatabaseImplement; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + [DbContext(typeof(SushiBarDatabase))] + [Migration("20230307133039_ShopMigration")] + partial class ShopMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Ingredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("IngredientName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Ingredients"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("SushiId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountSushi") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("SushiId"); + + b.ToTable("ShopSushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SushiName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("ListSushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IngredientId") + .HasColumnType("int"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("IngredientId"); + + b.HasIndex("SushiId"); + + b.ToTable("SushiIngredients"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") + .WithMany("Orders") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Shop", "Shop") + .WithMany("ListSushiFk") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") + .WithMany() + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Ingredient", "Ingredient") + .WithMany("SushiIngredients") + .HasForeignKey("IngredientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") + .WithMany("Ingredients") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Ingredient"); + + b.Navigation("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Ingredient", b => + { + b.Navigation("SushiIngredients"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Navigation("ListSushiFk"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.cs new file mode 100644 index 0000000..95dcfa2 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.cs @@ -0,0 +1,152 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + /// + public partial class ShopMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_SushiList_SushiId", + table: "Orders"); + + migrationBuilder.DropForeignKey( + name: "FK_SushiIngredients_SushiList_SushiId", + table: "SushiIngredients"); + + migrationBuilder.DropPrimaryKey( + name: "PK_SushiList", + table: "SushiList"); + + migrationBuilder.RenameTable( + name: "SushiList", + newName: "ListSushi"); + + migrationBuilder.AddPrimaryKey( + name: "PK_ListSushi", + table: "ListSushi", + column: "Id"); + + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + MaxCountSushi = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopSushi", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SushiId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopSushi", x => x.Id); + table.ForeignKey( + name: "FK_ShopSushi_ListSushi_SushiId", + column: x => x.SushiId, + principalTable: "ListSushi", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopSushi_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopSushi_ShopId", + table: "ShopSushi", + column: "ShopId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopSushi_SushiId", + table: "ShopSushi", + column: "SushiId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_ListSushi_SushiId", + table: "Orders", + column: "SushiId", + principalTable: "ListSushi", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_SushiIngredients_ListSushi_SushiId", + table: "SushiIngredients", + column: "SushiId", + principalTable: "ListSushi", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_ListSushi_SushiId", + table: "Orders"); + + migrationBuilder.DropForeignKey( + name: "FK_SushiIngredients_ListSushi_SushiId", + table: "SushiIngredients"); + + migrationBuilder.DropTable( + name: "ShopSushi"); + + migrationBuilder.DropTable( + name: "Shops"); + + migrationBuilder.DropPrimaryKey( + name: "PK_ListSushi", + table: "ListSushi"); + + migrationBuilder.RenameTable( + name: "ListSushi", + newName: "SushiList"); + + migrationBuilder.AddPrimaryKey( + name: "PK_SushiList", + table: "SushiList", + column: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_SushiList_SushiId", + table: "Orders", + column: "SushiId", + principalTable: "SushiList", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_SushiIngredients_SushiList_SushiId", + table: "SushiIngredients", + column: "SushiId", + principalTable: "SushiList", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs index 34ee42b..8dc0147 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs @@ -1,6 +1,10 @@ // +using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SushiBarDatabaseImplement; #nullable disable @@ -71,6 +75,59 @@ namespace SushiBarDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountSushi") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("SushiId"); + + b.ToTable("ShopSushi"); + }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => { b.Property("Id") @@ -88,7 +145,7 @@ namespace SushiBarDatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("SushiList"); + b.ToTable("ListSushi"); }); modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b => @@ -128,6 +185,25 @@ namespace SushiBarDatabaseImplement.Migrations b.Navigation("Sushi"); }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Shop", "Shop") + .WithMany("ListSushiFk") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") + .WithMany() + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Sushi"); + }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b => { b.HasOne("SushiBarDatabaseImplement.Models.Ingredient", "Ingredient") @@ -152,6 +228,11 @@ namespace SushiBarDatabaseImplement.Migrations b.Navigation("SushiIngredients"); }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Navigation("ListSushiFk"); + }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => { b.Navigation("Ingredients"); diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs b/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs index 1a79738..f7ac029 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs @@ -75,7 +75,7 @@ namespace SushiBarDatabaseImplement.Models public void UpdateSushi(SushiBarDatabase context, ShopBindingModel model) { - var shopSushi = context.ShopSushi.Where(rec => rec.SushiId == model.Id).ToList(); + var shopSushi = context.ShopSushi.Where(rec => rec.ShopId == model.Id).ToList(); if (shopSushi != null && shopSushi.Count > 0) { // удалили те, которых нет в модели context.ShopSushi.RemoveRange(shopSushi.Where(rec => !model.ListSushi.ContainsKey(rec.SushiId)));