From d8c6b81849ec9689f6df4d7e5db8864f488ff815 Mon Sep 17 00:00:00 2001 From: shadowik Date: Thu, 9 Mar 2023 03:09:42 +0300 Subject: [PATCH 1/3] Initial --- Pizzeria/Pizzeria.sln | 8 +- Pizzeria/Pizzeria/PizzeriaView.csproj | 5 + Pizzeria/Pizzeria/Program.cs | 2 +- .../Implements/ComponentStorage.cs | 79 ++++++++ .../Implements/OrderStorage.cs | 79 ++++++++ .../Implements/PizzaStorage.cs | 94 ++++++++++ .../20230309000843_InitialCreate.Designer.cs | 174 ++++++++++++++++++ .../20230309000843_InitialCreate.cs | 126 +++++++++++++ .../PizzeriaDatabaseModelSnapshot.cs | 171 +++++++++++++++++ .../Models/Component.cs | 58 ++++++ .../PizzeriaDatabaseImplement/Models/Order.cs | 73 ++++++++ .../PizzeriaDatabaseImplement/Models/Pizza.cs | 93 ++++++++++ .../Models/PizzaComponent.cs | 22 +++ .../PizzeriaDatabase.cs | 22 +++ .../PizzeriaDatabaseImplement.csproj | 27 +++ .../PizzeriaFileImplement.csproj | 9 + 16 files changed, 1040 insertions(+), 2 deletions(-) create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.Designer.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 47cfee0..67ca23c 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "Pi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{17269B24-6A77-4C45-B3DD-AA444F22D9B8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{B3A0C7DA-97ED-428C-9E2E-6FF6569F81A9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{B3A0C7DA-97ED-428C-9E2E-6FF6569F81A9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{ACCCBB67-15A1-4568-86C2-BFF5428FA073}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {B3A0C7DA-97ED-428C-9E2E-6FF6569F81A9}.Debug|Any CPU.Build.0 = Debug|Any CPU {B3A0C7DA-97ED-428C-9E2E-6FF6569F81A9}.Release|Any CPU.ActiveCfg = Release|Any CPU {B3A0C7DA-97ED-428C-9E2E-6FF6569F81A9}.Release|Any CPU.Build.0 = Release|Any CPU + {ACCCBB67-15A1-4568-86C2-BFF5428FA073}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ACCCBB67-15A1-4568-86C2-BFF5428FA073}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ACCCBB67-15A1-4568-86C2-BFF5428FA073}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ACCCBB67-15A1-4568-86C2-BFF5428FA073}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/Pizzeria/PizzeriaView.csproj b/Pizzeria/Pizzeria/PizzeriaView.csproj index 317eadb..d3bbecf 100644 --- a/Pizzeria/Pizzeria/PizzeriaView.csproj +++ b/Pizzeria/Pizzeria/PizzeriaView.csproj @@ -9,11 +9,16 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/Pizzeria/Pizzeria/Program.cs b/Pizzeria/Pizzeria/Program.cs index 572cdef..cc6affd 100644 --- a/Pizzeria/Pizzeria/Program.cs +++ b/Pizzeria/Pizzeria/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using PizzeriaBusinessLogic.BusinessLogics; using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.StoragesContracts; -using PizzeriaFileImplement.Implements; +using PizzeriaDatabaseImplement.Implements; using PizzeriaView; using System; using System.Drawing; diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..b40943d --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,79 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.Models; + +namespace PizzeriaDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new PizzeriaDatabase(); + return context.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new PizzeriaDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new PizzeriaDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new PizzeriaDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..7ac6154 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,79 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace PizzeriaDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Orders.Include(x => x.Pizza).Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Orders.Include(x => x.Pizza).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Orders.Include(x => x.Pizza).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new PizzeriaDatabase(); + if (model == null) + return null; + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new PizzeriaDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new PizzeriaDatabase(); + var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (order != null) + { + context.Orders.Remove(order); + context.SaveChanges(); + return order.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs new file mode 100644 index 0000000..4073726 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs @@ -0,0 +1,94 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace PizzeriaDatabaseImplement.Implements +{ + public class PizzaStorage : IPizzaStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Pizzas.Include(x => x.Components).ThenInclude(x => x.Component).ToList() + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Pizzas.Include(x => x.Components).ThenInclude(x => x.Component) + .Where(x => x.PizzaName.Contains(model.PizzaName)).ToList().Select(x => x.GetViewModel).ToList(); + } + + public PizzaViewModel? GetElement(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName) && !model.Id.HasValue) + { + return null; + } + using var context = new PizzeriaDatabase(); + return context.Pizzas.Include(x => x.Components).ThenInclude(x => x.Component) + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.PizzaName) && x.PizzaName == model.PizzaName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public PizzaViewModel? Insert(PizzaBindingModel model) + { + using var context = new PizzeriaDatabase(); + var newPizza = Pizza.Create(context, model); + if (newPizza == null) + { + return null; + } + context.Pizzas.Add(newPizza); + context.SaveChanges(); + return newPizza.GetViewModel; + } + + public PizzaViewModel? Update(PizzaBindingModel model) + { + using var context = new PizzeriaDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Pizza = context.Pizzas.FirstOrDefault(rec => rec.Id == model.Id); + if (Pizza == null) + { + return null; + } + Pizza.Update(model); + context.SaveChanges(); + Pizza.UpdateComponents(context, model); + transaction.Commit(); + return Pizza.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public PizzaViewModel? Delete(PizzaBindingModel model) + { + using var context = new PizzeriaDatabase(); + var element = context.Pizzas.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Pizzas.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.Designer.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.Designer.cs new file mode 100644 index 0000000..fbe12a8 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.Designer.cs @@ -0,0 +1,174 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + [Migration("20230309000843_InitialCreate")] + partial class InitialCreate + { + /// + 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("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.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("PizzaId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Product") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.cs new file mode 100644 index 0000000..ef650ef --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Pizzas", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PizzaName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pizzas", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PizzaId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Pizzas_PizzaId", + column: x => x.PizzaId, + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PizzaComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ProductId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + PizzaId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_PizzaComponents", x => x.Id); + table.ForeignKey( + name: "FK_PizzaComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PizzaComponents_Pizzas_PizzaId", + column: x => x.PizzaId, + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PizzaId", + table: "Orders", + column: "PizzaId"); + + migrationBuilder.CreateIndex( + name: "IX_PizzaComponents_ComponentId", + table: "PizzaComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PizzaComponents_PizzaId", + table: "PizzaComponents", + column: "PizzaId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PizzaComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Pizzas"); + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs new file mode 100644 index 0000000..3a1a691 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs @@ -0,0 +1,171 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + partial class PizzeriaDatabaseModelSnapshot : 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("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.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("PizzaId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Product") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..8f01acb --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs @@ -0,0 +1,58 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace PizzeriaDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string ComponentName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ComponentId")] + public virtual List PizzaComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..4cf7d5b --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs @@ -0,0 +1,73 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Enums; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaDatabaseImplement.Models +{ + public class Order + { + public int Id { get; private set; } + + [Required] + public int PizzaId { get; private set; } + + public virtual Pizza Pizza { get; set; } = new(); + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order Create(PizzeriaDatabase context, OrderBindingModel model) + { + return new Order() + { + Id = model.Id, + PizzaId = model.PizzaId, + Pizza = context.Pizzas.First(x => x.Id == model.PizzaId), + 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() + { + Id = Id, + PizzaId = PizzaId, + PizzaName = Pizza.PizzaName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs new file mode 100644 index 0000000..b79d8e1 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs @@ -0,0 +1,93 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace PizzeriaDatabaseImplement.Models +{ + public class Pizza : IPizzaModel + { + public int Id { get; set; } + [Required] + public string PizzaName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + + private Dictionary? _pizzaComponents = null; + [NotMapped] + public Dictionary PizzaComponents + { + get + { + if (_pizzaComponents == null) + { + _pizzaComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _pizzaComponents; + } + } + [ForeignKey("PizzaId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("PizzaId")] + public virtual List Orders { get; set; } = new(); + + public static Pizza + Create(PizzeriaDatabase context, PizzaBindingModel model) + { + return new Pizza() + { + Id = model.Id, + PizzaName = model.PizzaName, + Price = model.Price, + Components = model.PizzaComponents.Select(x => new PizzaComponent { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PizzaBindingModel model) + { + PizzaName = model.PizzaName; + Price = model.Price; + } + public PizzaViewModel GetViewModel => new() + { + Id = Id, + PizzaName = PizzaName, + Price = Price, + PizzaComponents = PizzaComponents + }; + public void UpdateComponents(PizzeriaDatabase context, PizzaBindingModel model) + { + var pizzaComponents = context.PizzaComponents.Where(rec => rec.ProductId == model.Id).ToList(); + + if (pizzaComponents != null && pizzaComponents.Count > 0) + { // удалили те, которых нет в модели + context.PizzaComponents.RemoveRange(pizzaComponents.Where(rec => !model.PizzaComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in pizzaComponents) + { + updateComponent.Count = model.PizzaComponents[updateComponent.ComponentId].Item2; + model.PizzaComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + + var product = context.Pizzas.First(x => x.Id == Id); + foreach (var pc in model.PizzaComponents) { + context.PizzaComponents.Add(new PizzaComponent + { + Product = product, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _pizzaComponents = null; + } + + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs new file mode 100644 index 0000000..35fb5aa --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; + +namespace PizzeriaDatabaseImplement.Models +{ + public class PizzaComponent + { + public int Id { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Pizza Product { get; set; } = new(); + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs new file mode 100644 index 0000000..65959f0 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -0,0 +1,22 @@ +using PizzeriaDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; + +namespace PizzeriaDatabaseImplement +{ + public class PizzeriaDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseSqlServer(@"Server=shadowik;Initial Catalog=PizzeriaDatabase;Integrated Security=True;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Pizzas { set; get; } + public virtual DbSet PizzaComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj new file mode 100644 index 0000000..2b00238 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + diff --git a/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj b/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj index b612a23..22ae113 100644 --- a/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj +++ b/Pizzeria/PizzeriaFileImplement/PizzeriaFileImplement.csproj @@ -6,6 +6,15 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + -- 2.25.1 From 8cde12c752d9077f71ac253330f0080116153d3e Mon Sep 17 00:00:00 2001 From: shadowik Date: Thu, 9 Mar 2023 07:30:15 +0300 Subject: [PATCH 2/3] Final --- ...e.Designer.cs => 20230308222141_InitMigration.Designer.cs} | 4 ++-- ...00843_InitialCreate.cs => 20230308222141_InitMigration.cs} | 2 +- Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs | 4 +--- Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs | 2 +- .../PizzeriaDatabaseImplement.csproj | 4 ---- 5 files changed, 5 insertions(+), 11 deletions(-) rename Pizzeria/PizzeriaDatabaseImplement/Migrations/{20230309000843_InitialCreate.Designer.cs => 20230308222141_InitMigration.Designer.cs} (98%) rename Pizzeria/PizzeriaDatabaseImplement/Migrations/{20230309000843_InitialCreate.cs => 20230308222141_InitMigration.cs} (99%) diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.Designer.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.Designer.cs similarity index 98% rename from Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.Designer.cs rename to Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.Designer.cs index fbe12a8..05251d8 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.Designer.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.Designer.cs @@ -12,8 +12,8 @@ using PizzeriaDatabaseImplement; namespace PizzeriaDatabaseImplement.Migrations { [DbContext(typeof(PizzeriaDatabase))] - [Migration("20230309000843_InitialCreate")] - partial class InitialCreate + [Migration("20230308222141_InitMigration")] + partial class InitMigration { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.cs similarity index 99% rename from Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.cs rename to Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.cs index ef650ef..27c2c27 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230309000843_InitialCreate.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace PizzeriaDatabaseImplement.Migrations { /// - public partial class InitialCreate : Migration + public partial class InitMigration : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs index b79d8e1..a32f628 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs @@ -63,11 +63,9 @@ namespace PizzeriaDatabaseImplement.Models { var pizzaComponents = context.PizzaComponents.Where(rec => rec.ProductId == model.Id).ToList(); - if (pizzaComponents != null && pizzaComponents.Count > 0) - { // удалили те, которых нет в модели + if (pizzaComponents != null && pizzaComponents.Count > 0) { context.PizzaComponents.RemoveRange(pizzaComponents.Where(rec => !model.PizzaComponents.ContainsKey(rec.ComponentId))); context.SaveChanges(); - // обновили количество у существующих записей foreach (var updateComponent in pizzaComponents) { updateComponent.Count = model.PizzaComponents[updateComponent.ComponentId].Item2; diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs index 65959f0..c071b87 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -10,7 +10,7 @@ namespace PizzeriaDatabaseImplement { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlServer(@"Server=shadowik;Initial Catalog=PizzeriaDatabase;Integrated Security=True;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=SHADOWIK\SHADOWIK;Initial Catalog=PizzeriaDatabase;Integrated Security=True;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj index 2b00238..738b64b 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj @@ -20,8 +20,4 @@ - - - - -- 2.25.1 From fc252a5ea404777505ebb043b2c64aea6d3c3192 Mon Sep 17 00:00:00 2001 From: shadowik Date: Sun, 26 Mar 2023 19:15:52 +0400 Subject: [PATCH 3/3] Rename products --- ...er.cs => 20230326151327_InitialCreate.Designer.cs} | 11 ++++------- ...itMigration.cs => 20230326151327_InitialCreate.cs} | 7 +++---- .../Migrations/PizzeriaDatabaseModelSnapshot.cs | 7 ++----- Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs | 6 +++--- .../Models/PizzaComponent.cs | 4 ++-- .../PizzeriaDatabaseImplement.csproj | 4 ++++ 6 files changed, 18 insertions(+), 21 deletions(-) rename Pizzeria/PizzeriaDatabaseImplement/Migrations/{20230308222141_InitMigration.Designer.cs => 20230326151327_InitialCreate.Designer.cs} (95%) rename Pizzeria/PizzeriaDatabaseImplement/Migrations/{20230308222141_InitMigration.cs => 20230326151327_InitialCreate.cs} (96%) diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.Designer.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230326151327_InitialCreate.Designer.cs similarity index 95% rename from Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.Designer.cs rename to Pizzeria/PizzeriaDatabaseImplement/Migrations/20230326151327_InitialCreate.Designer.cs index 05251d8..bc10304 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.Designer.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230326151327_InitialCreate.Designer.cs @@ -12,8 +12,8 @@ using PizzeriaDatabaseImplement; namespace PizzeriaDatabaseImplement.Migrations { [DbContext(typeof(PizzeriaDatabase))] - [Migration("20230308222141_InitMigration")] - partial class InitMigration + [Migration("20230326151327_InitialCreate")] + partial class InitialCreate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -115,9 +115,6 @@ namespace PizzeriaDatabaseImplement.Migrations b.Property("PizzaId") .HasColumnType("int"); - b.Property("ProductId") - .HasColumnType("int"); - b.HasKey("Id"); b.HasIndex("ComponentId"); @@ -146,7 +143,7 @@ namespace PizzeriaDatabaseImplement.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Product") + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") .WithMany("Components") .HasForeignKey("PizzaId") .OnDelete(DeleteBehavior.Cascade) @@ -154,7 +151,7 @@ namespace PizzeriaDatabaseImplement.Migrations b.Navigation("Component"); - b.Navigation("Product"); + b.Navigation("Pizza"); }); modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230326151327_InitialCreate.cs similarity index 96% rename from Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.cs rename to Pizzeria/PizzeriaDatabaseImplement/Migrations/20230326151327_InitialCreate.cs index 27c2c27..ad96fd6 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230308222141_InitMigration.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230326151327_InitialCreate.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace PizzeriaDatabaseImplement.Migrations { /// - public partial class InitMigration : Migration + public partial class InitialCreate : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -69,10 +69,9 @@ namespace PizzeriaDatabaseImplement.Migrations { Id = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), - ProductId = table.Column(type: "int", nullable: false), + PizzaId = table.Column(type: "int", nullable: false), ComponentId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false), - PizzaId = table.Column(type: "int", nullable: false) + Count = table.Column(type: "int", nullable: false) }, constraints: table => { diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs index 3a1a691..b5035a2 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs @@ -112,9 +112,6 @@ namespace PizzeriaDatabaseImplement.Migrations b.Property("PizzaId") .HasColumnType("int"); - b.Property("ProductId") - .HasColumnType("int"); - b.HasKey("Id"); b.HasIndex("ComponentId"); @@ -143,7 +140,7 @@ namespace PizzeriaDatabaseImplement.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Product") + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") .WithMany("Components") .HasForeignKey("PizzaId") .OnDelete(DeleteBehavior.Cascade) @@ -151,7 +148,7 @@ namespace PizzeriaDatabaseImplement.Migrations b.Navigation("Component"); - b.Navigation("Product"); + b.Navigation("Pizza"); }); modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs index a32f628..0bb2059 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs @@ -61,7 +61,7 @@ namespace PizzeriaDatabaseImplement.Models }; public void UpdateComponents(PizzeriaDatabase context, PizzaBindingModel model) { - var pizzaComponents = context.PizzaComponents.Where(rec => rec.ProductId == model.Id).ToList(); + var pizzaComponents = context.PizzaComponents.Where(rec => rec.PizzaId == model.Id).ToList(); if (pizzaComponents != null && pizzaComponents.Count > 0) { context.PizzaComponents.RemoveRange(pizzaComponents.Where(rec => !model.PizzaComponents.ContainsKey(rec.ComponentId))); @@ -74,11 +74,11 @@ namespace PizzeriaDatabaseImplement.Models context.SaveChanges(); } - var product = context.Pizzas.First(x => x.Id == Id); + var pizza = context.Pizzas.First(x => x.Id == Id); foreach (var pc in model.PizzaComponents) { context.PizzaComponents.Add(new PizzaComponent { - Product = product, + Pizza = pizza, Component = context.Components.First(x => x.Id == pc.Key), Count = pc.Value.Item2 }); diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs index 35fb5aa..a5a83e3 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/PizzaComponent.cs @@ -11,12 +11,12 @@ namespace PizzeriaDatabaseImplement.Models { public int Id { get; set; } [Required] - public int ProductId { get; set; } + public int PizzaId { get; set; } [Required] public int ComponentId { get; set; } [Required] public int Count { get; set; } public virtual Component Component { get; set; } = new(); - public virtual Pizza Product { get; set; } = new(); + public virtual Pizza Pizza { get; set; } = new(); } } diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj index 738b64b..2b00238 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj @@ -20,4 +20,8 @@ + + + + -- 2.25.1