From c1f92ed0c6c25d7f237ebc4085298f4ea7028076 Mon Sep 17 00:00:00 2001 From: dasha Date: Mon, 27 Feb 2023 22:16:28 +0400 Subject: [PATCH 01/11] =?UTF-8?q?=D0=9B=D0=A03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBar.sln | 8 +- SushiBar/SushiBar/Program.cs | 2 +- SushiBar/SushiBar/SushiBarView.csproj | 9 + .../SushiBarBusinessLogic.csproj | 4 + .../SushiBarContracts.csproj | 4 + .../SushiBarDataModels.csproj | 4 + .../Implements/IngredientStorage.cs | 83 +++++++++ .../Implements/OrderStorage.cs | 92 ++++++++++ .../Implements/SushiStorage.cs | 106 +++++++++++ .../20230227162916_InitMigration.Designer.cs | 156 ++++++++++++++++ .../20230227162916_InitMigration.cs | 114 ++++++++++++ ...20230227181342_SecondMigration.Designer.cs | 171 ++++++++++++++++++ .../20230227181342_SecondMigration.cs | 39 ++++ .../SushiBarDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Ingredient.cs | 63 +++++++ .../SushiBarDatabaseImplement/Models/Order.cs | 64 +++++++ .../SushiBarDatabaseImplement/Models/Sushi.cs | 98 ++++++++++ .../Models/SushiIngredient.cs | 22 +++ .../SushiBarDatabase.cs | 25 +++ .../SushiBarDatabaseImplement.csproj | 23 +++ .../SushiBarFileImplement.csproj | 7 + .../SushiBarListImplement.csproj | 4 + 22 files changed, 1264 insertions(+), 2 deletions(-) create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/IngredientStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.Designer.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.Designer.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/Ingredient.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/Order.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/SushiIngredient.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index 6fc6147..2f61042 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarBusinessLogic", "Su EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarListImplement", "SushiBarListImplement\SushiBarListImplement.csproj", "{81A91405-3721-40EF-A47C-CEBD92C0A4D0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{A26B0D9A-31CB-4845-ABC3-D84E60BECE46}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{A26B0D9A-31CB-4845-ABC3-D84E60BECE46}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDatabaseImplement", "SushiBarDatabaseImplement\SushiBarDatabaseImplement.csproj", "{206B0A13-956D-4E9A-88CD-17F89158E0E7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {A26B0D9A-31CB-4845-ABC3-D84E60BECE46}.Debug|Any CPU.Build.0 = Debug|Any CPU {A26B0D9A-31CB-4845-ABC3-D84E60BECE46}.Release|Any CPU.ActiveCfg = Release|Any CPU {A26B0D9A-31CB-4845-ABC3-D84E60BECE46}.Release|Any CPU.Build.0 = Release|Any CPU + {206B0A13-956D-4E9A-88CD-17F89158E0E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {206B0A13-956D-4E9A-88CD-17F89158E0E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {206B0A13-956D-4E9A-88CD-17F89158E0E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {206B0A13-956D-4E9A-88CD-17F89158E0E7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBar/Program.cs b/SushiBar/SushiBar/Program.cs index 6d5e99a..2c31bed 100644 --- a/SushiBar/SushiBar/Program.cs +++ b/SushiBar/SushiBar/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using SushiBarBusinessLogic.BusinessLogics; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.StoragesContracts; -using SushiBarFileImplement.Implements; +using SushiBarDatabaseImplement.Implements; namespace SushiBarView { diff --git a/SushiBar/SushiBar/SushiBarView.csproj b/SushiBar/SushiBar/SushiBarView.csproj index 237037b..b9d1234 100644 --- a/SushiBar/SushiBar/SushiBarView.csproj +++ b/SushiBar/SushiBar/SushiBarView.csproj @@ -9,6 +9,14 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +25,7 @@ + diff --git a/SushiBar/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj b/SushiBar/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj index 8d082e6..dffb9ed 100644 --- a/SushiBar/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj +++ b/SushiBar/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SushiBar/SushiBarContracts/SushiBarContracts.csproj b/SushiBar/SushiBarContracts/SushiBarContracts.csproj index 49dc40e..3794008 100644 --- a/SushiBar/SushiBarContracts/SushiBarContracts.csproj +++ b/SushiBar/SushiBarContracts/SushiBarContracts.csproj @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SushiBar/SushiBarDataModels/SushiBarDataModels.csproj b/SushiBar/SushiBarDataModels/SushiBarDataModels.csproj index daf9ebc..0ffedbc 100644 --- a/SushiBar/SushiBarDataModels/SushiBarDataModels.csproj +++ b/SushiBar/SushiBarDataModels/SushiBarDataModels.csproj @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/IngredientStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/IngredientStorage.cs new file mode 100644 index 0000000..cbd7f35 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/IngredientStorage.cs @@ -0,0 +1,83 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class IngredientStorage : IIngredientStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Ingredients + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(IngredientSearchModel model) + { + if (string.IsNullOrEmpty(model.IngredientName)) + { + return new(); + } + using var context = new SushiBarDatabase(); + return context.Ingredients + .Where(x => x.IngredientName.Contains(model.IngredientName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public IngredientViewModel? GetElement(IngredientSearchModel model) + { + if (string.IsNullOrEmpty(model.IngredientName) && !model.Id.HasValue) + { + return null; + } + using var context = new SushiBarDatabase(); + return context.Ingredients + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.IngredientName) && x.IngredientName == model.IngredientName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public IngredientViewModel? Insert(IngredientBindingModel model) + { + var newIngredient = Ingredient.Create(model); + if (newIngredient == null) + { + return null; + } + using var context = new SushiBarDatabase(); + context.Ingredients.Add(newIngredient); + context.SaveChanges(); + return newIngredient.GetViewModel; + } + + public IngredientViewModel? Update(IngredientBindingModel model) + { + using var context = new SushiBarDatabase(); + var ingredient = context.Ingredients.FirstOrDefault(x => x.Id == model.Id); + if (ingredient == null) + { + return null; + } + ingredient.Update(model); + context.SaveChanges(); + return ingredient.GetViewModel; + } + + public IngredientViewModel? Delete(IngredientBindingModel model) + { + using var context = new SushiBarDatabase(); + var element = context.Ingredients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Ingredients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..d451de5 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,92 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new SushiBarDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new SushiBarDatabase(); + return context.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new SushiBarDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Orders + .Select(x => new OrderViewModel + { + Id = x.Id, + SushiId = x.SushiId, + Count = x.Count, + Sum = x.Sum, + Status = x.Status, + DateCreate = x.DateCreate, + DateImplement = x.DateImplement, + SushiName = x.Sushi.SushiName + }) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new SushiBarDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new SushiBarDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..8c378c7 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs @@ -0,0 +1,106 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class SushiStorage : ISushiStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.SushiList + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName)) + { + return new(); + } + using var context = new SushiBarDatabase(); + return context.SushiList + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .Where(x => x.SushiName.Contains(model.SushiName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public SushiViewModel? GetElement(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue) + { + return null; + } + using var context = new SushiBarDatabase(); + return context.SushiList + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public SushiViewModel? Insert(SushiBindingModel model) + { + using var context = new SushiBarDatabase(); + var newSushi = Sushi.Create(context, model); + if (newSushi == null) + { + return null; + } + context.SushiList.Add(newSushi); + context.SaveChanges(); + return newSushi.GetViewModel; + } + + public SushiViewModel? Update(SushiBindingModel model) + { + using var context = new SushiBarDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var sushi = context.SushiList.FirstOrDefault(rec => rec.Id == model.Id); + if (sushi == null) + { + return null; + } + sushi.Update(model); + context.SaveChanges(); + sushi.UpdateIngredients(context, model); + transaction.Commit(); + return sushi.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public SushiViewModel? Delete(SushiBindingModel model) + { + using var context = new SushiBarDatabase(); + var element = context.SushiList + .Include(x => x.Ingredients) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.SushiList.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.Designer.cs new file mode 100644 index 0000000..04659c4 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.Designer.cs @@ -0,0 +1,156 @@ +// +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("20230227162916_InitMigration")] + partial class InitMigration + { + /// + 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.ToTable("Orders"); + }); + + 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("SushiList"); + }); + + 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.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.Sushi", b => + { + b.Navigation("Ingredients"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.cs new file mode 100644 index 0000000..d1b6e7b --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.cs @@ -0,0 +1,114 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Ingredients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IngredientName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Ingredients", 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); + }); + + migrationBuilder.CreateTable( + name: "SushiList", + 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_SushiList", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "SushiIngredients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SushiId = table.Column(type: "int", nullable: false), + IngredientId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SushiIngredients", x => x.Id); + table.ForeignKey( + name: "FK_SushiIngredients_Ingredients_IngredientId", + column: x => x.IngredientId, + principalTable: "Ingredients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SushiIngredients_SushiList_SushiId", + column: x => x.SushiId, + principalTable: "SushiList", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_SushiIngredients_IngredientId", + table: "SushiIngredients", + column: "IngredientId"); + + migrationBuilder.CreateIndex( + name: "IX_SushiIngredients_SushiId", + table: "SushiIngredients", + column: "SushiId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "SushiIngredients"); + + migrationBuilder.DropTable( + name: "Ingredients"); + + migrationBuilder.DropTable( + name: "SushiList"); + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.Designer.cs new file mode 100644 index 0000000..5d10487 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.Designer.cs @@ -0,0 +1,171 @@ +// +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("20230227181342_SecondMigration")] + partial class SecondMigration + { + /// + 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.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("SushiList"); + }); + + 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.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.Sushi", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.cs new file mode 100644 index 0000000..8c815e1 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + /// + public partial class SecondMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateIndex( + name: "IX_Orders_SushiId", + table: "Orders", + column: "SushiId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_SushiList_SushiId", + table: "Orders", + column: "SushiId", + principalTable: "SushiList", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_SushiList_SushiId", + table: "Orders"); + + migrationBuilder.DropIndex( + name: "IX_Orders_SushiId", + table: "Orders"); + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs new file mode 100644 index 0000000..d3104d5 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SushiBarDatabaseImplement; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + [DbContext(typeof(SushiBarDatabase))] + partial class SushiBarDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(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.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("SushiList"); + }); + + 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.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.Sushi", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Ingredient.cs b/SushiBar/SushiBarDatabaseImplement/Models/Ingredient.cs new file mode 100644 index 0000000..37d313f --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Ingredient.cs @@ -0,0 +1,63 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace SushiBarDatabaseImplement.Models +{ + public class Ingredient : IIngredientModel + { + public int Id { get; private set; } + + [Required] + public string IngredientName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [ForeignKey("IngredientId")] + public virtual List SushiIngredients { get; set; } = new(); + + public static Ingredient? Create(IngredientBindingModel model) + { + if (model == null) + { + return null; + } + return new Ingredient() + { + Id = model.Id, + IngredientName = model.IngredientName, + Cost = model.Cost + }; + } + + public static Ingredient Create(IngredientViewModel model) + { + return new Ingredient + { + Id = model.Id, + IngredientName = model.IngredientName, + Cost = model.Cost + }; + } + + public void Update(IngredientBindingModel model) + { + if (model == null) + { + return; + } + IngredientName = model.IngredientName; + Cost = model.Cost; + } + + public IngredientViewModel GetViewModel => new() + { + Id = Id, + IngredientName = IngredientName, + Cost = Cost + }; + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..aac6f84 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs @@ -0,0 +1,64 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Enums; +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace SushiBarDatabaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int SushiId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public int Id { get; set; } + public virtual Sushi Sushi { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SushiId = model.SushiId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + SushiId = SushiId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + }; + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs new file mode 100644 index 0000000..71826cc --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs @@ -0,0 +1,98 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace SushiBarDatabaseImplement.Models +{ + public class Sushi : ISushiModel + { + public int Id { get; set; } + + [Required] + public string SushiName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _productIngredients = null; + + [NotMapped] + public Dictionary SushiIngredients + { + get + { + if (_productIngredients == null) + { + _productIngredients = Ingredients + .ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count)); + } + return _productIngredients; + } + } + + [ForeignKey("SushiId")] + public virtual List Ingredients { get; set; } = new(); + [ForeignKey("SushiId")] + public virtual List Orders { get; set; } = new(); + + public static Sushi Create(SushiBarDatabase context, SushiBindingModel model) + { + return new Sushi() + { + Id = model.Id, + SushiName = model.SushiName, + Price = model.Price, + Ingredients = model.SushiIngredients.Select(x => new SushiIngredient + { + Ingredient = context.Ingredients.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(SushiBindingModel model) + { + SushiName = model.SushiName; + Price = model.Price; + } + + public SushiViewModel GetViewModel => new() + { + Id = Id, + SushiName = SushiName, + Price = Price, + SushiIngredients = SushiIngredients + }; + + public void UpdateIngredients(SushiBarDatabase context, SushiBindingModel model) + { + var productIngredients = context.SushiIngredients.Where(rec => rec.SushiId == model.Id).ToList(); + if (productIngredients != null && productIngredients.Count > 0) + { // удалили те, которых нет в модели + context.SushiIngredients.RemoveRange(productIngredients.Where(rec => !model.SushiIngredients.ContainsKey(rec.IngredientId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateIngredient in productIngredients) + { + updateIngredient.Count = model.SushiIngredients[updateIngredient.IngredientId].Item2; + model.SushiIngredients.Remove(updateIngredient.IngredientId); + } + context.SaveChanges(); + } + var product = context.SushiList.First(x => x.Id == Id); + foreach (var pc in model.SushiIngredients) + { + context.SushiIngredients.Add(new SushiIngredient + { + Sushi = product, + Ingredient = context.Ingredients.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _productIngredients = null; + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/SushiIngredient.cs b/SushiBar/SushiBarDatabaseImplement/Models/SushiIngredient.cs new file mode 100644 index 0000000..89ad76c --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/SushiIngredient.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace SushiBarDatabaseImplement.Models +{ + public class SushiIngredient + { + public int Id { get; set; } + + [Required] + public int SushiId { get; set; } + + [Required] + public int IngredientId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Ingredient Ingredient { get; set; } = new(); + + public virtual Sushi Sushi { get; set; } = new(); + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs new file mode 100644 index 0000000..58f8313 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -0,0 +1,25 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement +{ + public class SushiBarDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-D8KMQQU\SQLEXPRESS;Initial Catalog=SushiBarDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Ingredients { set; get; } + + public virtual DbSet SushiList { set; get; } + + public virtual DbSet SushiIngredients { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj new file mode 100644 index 0000000..1211e5e --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj b/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj index 00e2c96..428ac20 100644 --- a/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj +++ b/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj @@ -6,6 +6,13 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SushiBar/SushiBarListImplement/SushiBarListImplement.csproj b/SushiBar/SushiBarListImplement/SushiBarListImplement.csproj index f2515de..6a2b0ac 100644 --- a/SushiBar/SushiBarListImplement/SushiBarListImplement.csproj +++ b/SushiBar/SushiBarListImplement/SushiBarListImplement.csproj @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + -- 2.25.1 From 46b8e09d6a52a823f2bdabaceab10fe9beb5947b Mon Sep 17 00:00:00 2001 From: dasha Date: Mon, 27 Feb 2023 22:16:28 +0400 Subject: [PATCH 02/11] =?UTF-8?q?=D0=9B=D0=A03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SushiBarDatabaseImplement/Models/Sushi.cs | 28 +++++++++---------- .../SushiBarFileImplement/Models/Sushi.cs | 10 +++---- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs index 71826cc..5e93501 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs @@ -16,19 +16,19 @@ namespace SushiBarDatabaseImplement.Models [Required] public double Price { get; set; } - private Dictionary? _productIngredients = null; + private Dictionary? _sushiIngredients = null; [NotMapped] public Dictionary SushiIngredients { get { - if (_productIngredients == null) + if (_sushiIngredients == null) { - _productIngredients = Ingredients + _sushiIngredients = Ingredients .ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count)); } - return _productIngredients; + return _sushiIngredients; } } @@ -68,31 +68,31 @@ namespace SushiBarDatabaseImplement.Models public void UpdateIngredients(SushiBarDatabase context, SushiBindingModel model) { - var productIngredients = context.SushiIngredients.Where(rec => rec.SushiId == model.Id).ToList(); - if (productIngredients != null && productIngredients.Count > 0) + var sushiIngredients = context.SushiIngredients.Where(rec => rec.SushiId == model.Id).ToList(); + if (sushiIngredients != null && sushiIngredients.Count > 0) { // удалили те, которых нет в модели - context.SushiIngredients.RemoveRange(productIngredients.Where(rec => !model.SushiIngredients.ContainsKey(rec.IngredientId))); + context.SushiIngredients.RemoveRange(sushiIngredients.Where(rec => !model.SushiIngredients.ContainsKey(rec.IngredientId))); context.SaveChanges(); // обновили количество у существующих записей - foreach (var updateIngredient in productIngredients) + foreach (var updateIngredient in sushiIngredients) { updateIngredient.Count = model.SushiIngredients[updateIngredient.IngredientId].Item2; model.SushiIngredients.Remove(updateIngredient.IngredientId); } context.SaveChanges(); } - var product = context.SushiList.First(x => x.Id == Id); - foreach (var pc in model.SushiIngredients) + var sushi = context.SushiList.First(x => x.Id == Id); + foreach (var si in model.SushiIngredients) { context.SushiIngredients.Add(new SushiIngredient { - Sushi = product, - Ingredient = context.Ingredients.First(x => x.Id == pc.Key), - Count = pc.Value.Item2 + Sushi = sushi, + Ingredient = context.Ingredients.First(x => x.Id == si.Key), + Count = si.Value.Item2 }); context.SaveChanges(); } - _productIngredients = null; + _sushiIngredients = null; } } } diff --git a/SushiBar/SushiBarFileImplement/Models/Sushi.cs b/SushiBar/SushiBarFileImplement/Models/Sushi.cs index d697e76..ec3518c 100644 --- a/SushiBar/SushiBarFileImplement/Models/Sushi.cs +++ b/SushiBar/SushiBarFileImplement/Models/Sushi.cs @@ -11,17 +11,17 @@ namespace SushiBarFileImplement.Models public string SushiName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Ingredients { get; private set; } = new(); - private Dictionary? _productIngredients = null; + private Dictionary? _sushiIngredients = null; public Dictionary SushiIngredients { get { - if (_productIngredients == null) + if (_sushiIngredients == null) { var source = DataFileSingleton.GetInstance(); - _productIngredients = Ingredients.ToDictionary(x => x.Key, y => ((source.Ingredients.FirstOrDefault(z => z.Id == y.Key) as IIngredientModel)!, y.Value)); + _sushiIngredients = Ingredients.ToDictionary(x => x.Key, y => ((source.Ingredients.FirstOrDefault(z => z.Id == y.Key) as IIngredientModel)!, y.Value)); } - return _productIngredients; + return _sushiIngredients; } } public static Sushi? Create(SushiBindingModel model) @@ -63,7 +63,7 @@ namespace SushiBarFileImplement.Models Price = model.Price; Ingredients = model.SushiIngredients.ToDictionary(x => x.Key, x => x.Value.Item2); - _productIngredients = null; + _sushiIngredients = null; } public SushiViewModel GetViewModel => new() { -- 2.25.1 From 3f49eade0f2136fe99593007f2c072128163701e Mon Sep 17 00:00:00 2001 From: dasha Date: Mon, 27 Feb 2023 22:16:28 +0400 Subject: [PATCH 03/11] =?UTF-8?q?=D0=9B=D0=A03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Migrations/SushiBarDatabaseModelSnapshot.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs index d3104d5..34ee42b 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs @@ -1,10 +1,6 @@ // -using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using SushiBarDatabaseImplement; #nullable disable -- 2.25.1 From 9450ae2f6f363cdc82887ada3c4c515faef7adcb Mon Sep 17 00:00:00 2001 From: dasha Date: Mon, 27 Feb 2023 22:16:28 +0400 Subject: [PATCH 04/11] =?UTF-8?q?=D0=9B=D0=A03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs index 8c378c7..6f85ecb 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs @@ -93,6 +93,7 @@ namespace SushiBarDatabaseImplement.Implements using var context = new SushiBarDatabase(); var element = context.SushiList .Include(x => x.Ingredients) + .Include(x => x.Orders) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { -- 2.25.1 From 1a41ad5ce320ff201b5314a425d103597c17b150 Mon Sep 17 00:00:00 2001 From: dasha Date: Tue, 28 Feb 2023 12:38:33 +0400 Subject: [PATCH 05/11] =?UTF-8?q?=D0=9C=D0=BE=D0=B6=D0=B5=D1=82=20=D0=B1?= =?UTF-8?q?=D1=8B=D1=82=D1=8C=20=D1=82=D0=B0=D0=BA=20=D0=B1=D1=83=D0=B4?= =?UTF-8?q?=D0=B5=D1=82=20=D0=BB=D1=83=D1=87=D1=88=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 18 +++++------------- .../SushiBarDatabaseImplement/Models/Order.cs | 3 ++- .../SushiBarDatabase.cs | 2 +- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs index d451de5..41fcc45 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs @@ -1,4 +1,5 @@ -using SushiBarContracts.BindingModels; +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; @@ -49,18 +50,9 @@ namespace SushiBarDatabaseImplement.Implements { using var context = new SushiBarDatabase(); return context.Orders - .Select(x => new OrderViewModel - { - Id = x.Id, - SushiId = x.SushiId, - Count = x.Count, - Sum = x.Sum, - Status = x.Status, - DateCreate = x.DateCreate, - DateImplement = x.DateImplement, - SushiName = x.Sushi.SushiName - }) - .ToList(); + .Include(x => x.Sushi) + .Select(x => x.GetViewModel) + .ToList(); } public OrderViewModel? Insert(OrderBindingModel model) diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs index aac6f84..5e3bac7 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs @@ -20,7 +20,7 @@ namespace SushiBarDatabaseImplement.Models public DateTime DateCreate { get; set; } public DateTime? DateImplement { get; set; } public int Id { get; set; } - public virtual Sushi Sushi { get; set; } + public Sushi Sushi { get; set; } public static Order? Create(OrderBindingModel? model) { @@ -59,6 +59,7 @@ namespace SushiBarDatabaseImplement.Models DateImplement = DateImplement, Id = Id, Status = Status, + SushiName = Sushi.SushiName, }; } } diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs index 58f8313..84da6c9 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -9,7 +9,7 @@ namespace SushiBarDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-D8KMQQU\SQLEXPRESS;Initial Catalog=SushiBarDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-JC256C6\SQLEXPRESS;Initial Catalog=SushiBarDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } -- 2.25.1 From e962963589ed03fd32b5d668da9751c5cdf96842 Mon Sep 17 00:00:00 2001 From: dasha Date: Wed, 1 Mar 2023 20:14:15 +0400 Subject: [PATCH 06/11] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 21 +++++++++++++------ .../SushiBarDatabaseImplement/Models/Order.cs | 1 - .../SushiBarDatabase.cs | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs index 41fcc45..61dac62 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs @@ -12,7 +12,9 @@ namespace SushiBarDatabaseImplement.Implements public OrderViewModel? Delete(OrderBindingModel model) { using var context = new SushiBarDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders + .Include(x => x.Sushi) + .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Orders.Remove(element); @@ -30,6 +32,7 @@ namespace SushiBarDatabaseImplement.Implements } using var context = new SushiBarDatabase(); return context.Orders + .Include(x => x.Sushi) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; } @@ -42,9 +45,10 @@ namespace SushiBarDatabaseImplement.Implements } using var context = new SushiBarDatabase(); return context.Orders - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Sushi) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { @@ -65,13 +69,18 @@ namespace SushiBarDatabaseImplement.Implements using var context = new SushiBarDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; + return context.Orders + .Include(x => x.Sushi) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { using var context = new SushiBarDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders + .Include(x => x.Sushi) + .FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null; diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs index 5e3bac7..9fc4709 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs @@ -21,7 +21,6 @@ namespace SushiBarDatabaseImplement.Models public DateTime? DateImplement { get; set; } public int Id { get; set; } public Sushi Sushi { get; set; } - public static Order? Create(OrderBindingModel? model) { if (model == null) diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs index 84da6c9..58f8313 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -9,7 +9,7 @@ namespace SushiBarDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-JC256C6\SQLEXPRESS;Initial Catalog=SushiBarDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-D8KMQQU\SQLEXPRESS;Initial Catalog=SushiBarDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } -- 2.25.1 From 522bff8245429c3654fcd570ee73ee1ff52159f8 Mon Sep 17 00:00:00 2001 From: dasha Date: Tue, 7 Mar 2023 17:20:16 +0400 Subject: [PATCH 07/11] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6?= =?UTF-8?q?=D1=83=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D1=84=D0=B8=D0=BA?= =?UTF-8?q?=D1=81=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5.=20?= =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2,=20=D1=81=D0=B2?= =?UTF-8?q?=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D1=81=20=D0=BC?= =?UTF-8?q?=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 138 ++++++++++++++++++ .../Implements/SushiStorage.cs | 25 +++- .../SushiBarDatabaseImplement/Models/Shop.cs | 105 +++++++++++++ .../Models/ShopSushi.cs | 22 +++ .../SushiBarDatabaseImplement/Models/Sushi.cs | 2 +- .../SushiBarDatabase.cs | 6 +- 6 files changed, 289 insertions(+), 9 deletions(-) create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/Shop.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/ShopSushi.cs diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..270da97 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,138 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + internal class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Shops + .Include(x => x.ListSushi) + .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.ListSushi) + .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 null; + } + using var context = new SushiBarDatabase(); + return context.Shops + .Include(x => x.ListSushi) + .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 = Sushi.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(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateSushi(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new SushiBarDatabase(); + var element = context.Shops + .Include(x => x.ListSushiFk) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public bool IsEnoughSushi(ISushiModel model, int count) + { + throw new NotImplementedException(); + } + public bool SellSushi(ISushiModel model, int count) + { + using var context = new SushiBarDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var shop in context.Shops.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); + count -= countInCurrentShop; + } + else + { + shop.ListSushi[model.Id] = (shop.ListSushi[model.Id].Item1, countInCurrentShop - count); + count = 0; + + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs index 6f85ecb..d8ad298 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs @@ -4,6 +4,7 @@ using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; using SushiBarDatabaseImplement.Models; +using SushiBarDataModels.Models; namespace SushiBarDatabaseImplement.Implements { @@ -12,7 +13,7 @@ namespace SushiBarDatabaseImplement.Implements public List GetFullList() { using var context = new SushiBarDatabase(); - return context.SushiList + return context.ListSushi .Include(x => x.Ingredients) .ThenInclude(x => x.Ingredient) .ToList() @@ -27,7 +28,7 @@ namespace SushiBarDatabaseImplement.Implements return new(); } using var context = new SushiBarDatabase(); - return context.SushiList + return context.ListSushi .Include(x => x.Ingredients) .ThenInclude(x => x.Ingredient) .Where(x => x.SushiName.Contains(model.SushiName)) @@ -43,7 +44,7 @@ namespace SushiBarDatabaseImplement.Implements return null; } using var context = new SushiBarDatabase(); - return context.SushiList + return context.ListSushi .Include(x => x.Ingredients) .ThenInclude(x => x.Ingredient) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) || @@ -59,7 +60,7 @@ namespace SushiBarDatabaseImplement.Implements { return null; } - context.SushiList.Add(newSushi); + context.ListSushi.Add(newSushi); context.SaveChanges(); return newSushi.GetViewModel; } @@ -70,7 +71,7 @@ namespace SushiBarDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var sushi = context.SushiList.FirstOrDefault(rec => rec.Id == model.Id); + var sushi = context.ListSushi.FirstOrDefault(rec => rec.Id == model.Id); if (sushi == null) { return null; @@ -91,17 +92,27 @@ namespace SushiBarDatabaseImplement.Implements public SushiViewModel? Delete(SushiBindingModel model) { using var context = new SushiBarDatabase(); - var element = context.SushiList + var element = context.ListSushi .Include(x => x.Ingredients) .Include(x => x.Orders) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { - context.SushiList.Remove(element); + context.ListSushi.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } + + public bool HasSushi(ISushiModel model, int needCount) + { + throw new NotImplementedException(); + } + + public bool SellSushi(ISushiModel model, int count) + { + throw new NotImplementedException(); + } } } diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs b/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..1a79738 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs @@ -0,0 +1,105 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Xml.Linq; + +namespace SushiBarDatabaseImplement.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 DateOpening { get; private set; } + [Required] + public int MaxCountSushi { get; private set; } + + private Dictionary? _shopSushi = null; + + [NotMapped] + public Dictionary ListSushi + { + get + { + if (_shopSushi == null) + { + _shopSushi = ListSushiFk + .ToDictionary(recPC => recPC.SushiId, recPC => (recPC.Sushi as ISushiModel, recPC.Count)); + } + return _shopSushi; + } + } + + [ForeignKey("ShopId")] + public virtual List ListSushiFk { get; set; } = new(); + + public static Shop Create(SushiBarDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + MaxCountSushi = model.MaxCountSushi, + ListSushiFk = model.ListSushi.Select(x => new ShopSushi + { + Sushi = context.ListSushi.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + MaxCountSushi = model.MaxCountSushi; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + MaxCountSushi = MaxCountSushi, + ListSushi = ListSushi + }; + + public void UpdateSushi(SushiBarDatabase context, ShopBindingModel model) + { + var shopSushi = context.ShopSushi.Where(rec => rec.SushiId == model.Id).ToList(); + if (shopSushi != null && shopSushi.Count > 0) + { // удалили те, которых нет в модели + context.ShopSushi.RemoveRange(shopSushi.Where(rec => !model.ListSushi.ContainsKey(rec.SushiId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateSushi in shopSushi) + { + updateSushi.Count = model.ListSushi[updateSushi.SushiId].Item2; + model.ListSushi.Remove(updateSushi.SushiId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ss in model.ListSushi) + { + context.ShopSushi.Add(new ShopSushi + { + Shop = shop, + Sushi = context.ListSushi.First(x => x.Id == ss.Key), + Count = ss.Value.Item2 + }); + context.SaveChanges(); + } + _shopSushi = null; + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/ShopSushi.cs b/SushiBar/SushiBarDatabaseImplement/Models/ShopSushi.cs new file mode 100644 index 0000000..3a71514 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/ShopSushi.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace SushiBarDatabaseImplement.Models +{ + public class ShopSushi + { + public int Id { get; set; } + + [Required] + public int SushiId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Sushi Sushi { get; set; } = new(); + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs index 5e93501..009329f 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs @@ -81,7 +81,7 @@ namespace SushiBarDatabaseImplement.Models } context.SaveChanges(); } - var sushi = context.SushiList.First(x => x.Id == Id); + var sushi = context.ListSushi.First(x => x.Id == Id); foreach (var si in model.SushiIngredients) { context.SushiIngredients.Add(new SushiIngredient diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs index 58f8313..33a0555 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -16,10 +16,14 @@ namespace SushiBarDatabaseImplement public virtual DbSet Ingredients { set; get; } - public virtual DbSet SushiList { set; get; } + public virtual DbSet ListSushi { set; get; } public virtual DbSet SushiIngredients { set; get; } public virtual DbSet Orders { set; get; } + + public virtual DbSet Shops { set; get; } + + public virtual DbSet ShopSushi { set; get; } } } -- 2.25.1 From 9399a68fc989f8055f30380bdc7c10d061d667a0 Mon Sep 17 00:00:00 2001 From: dasha Date: Tue, 7 Mar 2023 19:27:57 +0400 Subject: [PATCH 08/11] =?UTF-8?q?=D0=A7=D1=82=D0=BE-=D1=82=D0=BE=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82,=20=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=B2=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 36 ++- .../20230307133039_ShopMigration.Designer.cs | 248 ++++++++++++++++++ .../20230307133039_ShopMigration.cs | 152 +++++++++++ .../SushiBarDatabaseModelSnapshot.cs | 83 +++++- .../SushiBarDatabaseImplement/Models/Shop.cs | 2 +- 5 files changed, 510 insertions(+), 11 deletions(-) create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.Designer.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.cs 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))); -- 2.25.1 From eaafd9b70359718e0e61d6789100248a42cf748a Mon Sep 17 00:00:00 2001 From: dasha Date: Fri, 10 Mar 2023 16:44:26 +0400 Subject: [PATCH 09/11] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BC=D0=B5=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/SushiStorage.cs | 14 +++++++------- .../20230227162916_InitMigration.Designer.cs | 2 +- .../Migrations/20230227162916_InitMigration.cs | 10 +++++----- .../20230227181342_SecondMigration.Designer.cs | 2 +- .../Migrations/20230227181342_SecondMigration.cs | 6 +++--- .../Migrations/SushiBarDatabaseModelSnapshot.cs | 2 +- SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs | 2 +- .../SushiBarDatabaseImplement/SushiBarDatabase.cs | 2 +- 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs index 6f85ecb..02b08f1 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs @@ -12,7 +12,7 @@ namespace SushiBarDatabaseImplement.Implements public List GetFullList() { using var context = new SushiBarDatabase(); - return context.SushiList + return context.ListSushi .Include(x => x.Ingredients) .ThenInclude(x => x.Ingredient) .ToList() @@ -27,7 +27,7 @@ namespace SushiBarDatabaseImplement.Implements return new(); } using var context = new SushiBarDatabase(); - return context.SushiList + return context.ListSushi .Include(x => x.Ingredients) .ThenInclude(x => x.Ingredient) .Where(x => x.SushiName.Contains(model.SushiName)) @@ -43,7 +43,7 @@ namespace SushiBarDatabaseImplement.Implements return null; } using var context = new SushiBarDatabase(); - return context.SushiList + return context.ListSushi .Include(x => x.Ingredients) .ThenInclude(x => x.Ingredient) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) || @@ -59,7 +59,7 @@ namespace SushiBarDatabaseImplement.Implements { return null; } - context.SushiList.Add(newSushi); + context.ListSushi.Add(newSushi); context.SaveChanges(); return newSushi.GetViewModel; } @@ -70,7 +70,7 @@ namespace SushiBarDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var sushi = context.SushiList.FirstOrDefault(rec => rec.Id == model.Id); + var sushi = context.ListSushi.FirstOrDefault(rec => rec.Id == model.Id); if (sushi == null) { return null; @@ -91,13 +91,13 @@ namespace SushiBarDatabaseImplement.Implements public SushiViewModel? Delete(SushiBindingModel model) { using var context = new SushiBarDatabase(); - var element = context.SushiList + var element = context.ListSushi .Include(x => x.Ingredients) .Include(x => x.Orders) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { - context.SushiList.Remove(element); + context.ListSushi.Remove(element); context.SaveChanges(); return element.GetViewModel; } diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.Designer.cs index 04659c4..d75219a 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.Designer.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.Designer.cs @@ -93,7 +93,7 @@ namespace SushiBarDatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("SushiList"); + b.ToTable("ListSushi"); }); modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b => diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.cs index d1b6e7b..729d675 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227162916_InitMigration.cs @@ -44,7 +44,7 @@ namespace SushiBarDatabaseImplement.Migrations }); migrationBuilder.CreateTable( - name: "SushiList", + name: "ListSushi", columns: table => new { Id = table.Column(type: "int", nullable: false) @@ -54,7 +54,7 @@ namespace SushiBarDatabaseImplement.Migrations }, constraints: table => { - table.PrimaryKey("PK_SushiList", x => x.Id); + table.PrimaryKey("PK_ListSushi", x => x.Id); }); migrationBuilder.CreateTable( @@ -77,9 +77,9 @@ namespace SushiBarDatabaseImplement.Migrations principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( - name: "FK_SushiIngredients_SushiList_SushiId", + name: "FK_SushiIngredients_ListSushi_SushiId", column: x => x.SushiId, - principalTable: "SushiList", + principalTable: "ListSushi", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); @@ -108,7 +108,7 @@ namespace SushiBarDatabaseImplement.Migrations name: "Ingredients"); migrationBuilder.DropTable( - name: "SushiList"); + name: "ListSushi"); } } } diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.Designer.cs index 5d10487..e1e6ddb 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.Designer.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.Designer.cs @@ -95,7 +95,7 @@ namespace SushiBarDatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("SushiList"); + b.ToTable("ListSushi"); }); modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b => diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.cs index 8c815e1..8d56dd0 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227181342_SecondMigration.cs @@ -16,10 +16,10 @@ namespace SushiBarDatabaseImplement.Migrations column: "SushiId"); migrationBuilder.AddForeignKey( - name: "FK_Orders_SushiList_SushiId", + name: "FK_Orders_ListSushi_SushiId", table: "Orders", column: "SushiId", - principalTable: "SushiList", + principalTable: "ListSushi", principalColumn: "Id", onDelete: ReferentialAction.Cascade); } @@ -28,7 +28,7 @@ namespace SushiBarDatabaseImplement.Migrations protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropForeignKey( - name: "FK_Orders_SushiList_SushiId", + name: "FK_Orders_ListSushi_SushiId", table: "Orders"); migrationBuilder.DropIndex( diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs index 34ee42b..a9956bd 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs @@ -88,7 +88,7 @@ namespace SushiBarDatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("SushiList"); + b.ToTable("ListSushi"); }); modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b => diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs index 5e93501..009329f 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs @@ -81,7 +81,7 @@ namespace SushiBarDatabaseImplement.Models } context.SaveChanges(); } - var sushi = context.SushiList.First(x => x.Id == Id); + var sushi = context.ListSushi.First(x => x.Id == Id); foreach (var si in model.SushiIngredients) { context.SushiIngredients.Add(new SushiIngredient diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs index 58f8313..1e0447f 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -16,7 +16,7 @@ namespace SushiBarDatabaseImplement public virtual DbSet Ingredients { set; get; } - public virtual DbSet SushiList { set; get; } + public virtual DbSet ListSushi { set; get; } public virtual DbSet SushiIngredients { set; get; } -- 2.25.1 From 0d26301504d08fc35babf2fca6787c9499ae4466 Mon Sep 17 00:00:00 2001 From: dasha Date: Tue, 14 Mar 2023 13:05:56 +0400 Subject: [PATCH 10/11] =?UTF-8?q?=D0=A1=D0=BF=D0=B0=D1=81=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... 20230314085708_ShopMigration.Designer.cs} | 2 +- ...ion.cs => 20230314085708_ShopMigration.cs} | 74 ------------------- 2 files changed, 1 insertion(+), 75 deletions(-) rename SushiBar/SushiBarDatabaseImplement/Migrations/{20230307133039_ShopMigration.Designer.cs => 20230314085708_ShopMigration.Designer.cs} (99%) rename SushiBar/SushiBarDatabaseImplement/Migrations/{20230307133039_ShopMigration.cs => 20230314085708_ShopMigration.cs} (53%) diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230314085708_ShopMigration.Designer.cs similarity index 99% rename from SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.Designer.cs rename to SushiBar/SushiBarDatabaseImplement/Migrations/20230314085708_ShopMigration.Designer.cs index c9c560d..9b63c33 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.Designer.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230314085708_ShopMigration.Designer.cs @@ -12,7 +12,7 @@ using SushiBarDatabaseImplement; namespace SushiBarDatabaseImplement.Migrations { [DbContext(typeof(SushiBarDatabase))] - [Migration("20230307133039_ShopMigration")] + [Migration("20230314085708_ShopMigration")] partial class ShopMigration { /// diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230314085708_ShopMigration.cs similarity index 53% rename from SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.cs rename to SushiBar/SushiBarDatabaseImplement/Migrations/20230314085708_ShopMigration.cs index 95dcfa2..dea767f 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20230307133039_ShopMigration.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230314085708_ShopMigration.cs @@ -11,27 +11,6 @@ namespace SushiBarDatabaseImplement.Migrations /// 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 @@ -84,69 +63,16 @@ namespace SushiBarDatabaseImplement.Migrations 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); } } } -- 2.25.1 From f235681f4d65a0f37ab7ccd5315dc227ece5c6f5 Mon Sep 17 00:00:00 2001 From: dasha Date: Sun, 19 Mar 2023 12:44:49 +0400 Subject: [PATCH 11/11] =?UTF-8?q?=D0=9B=D0=B8=D1=88=D0=BD=D0=B5=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/SushiStorage.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs index d8ad298..16c6651 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs @@ -104,15 +104,5 @@ namespace SushiBarDatabaseImplement.Implements } return null; } - - public bool HasSushi(ISushiModel model, int needCount) - { - throw new NotImplementedException(); - } - - public bool SellSushi(ISushiModel model, int count) - { - throw new NotImplementedException(); - } } } -- 2.25.1