From cda6b9d63ac16d77a80ae5d6c6ba3f7b8edabb3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=A5=D0=B0=D1=80=D0=BB?= =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B2?= Date: Sun, 2 Apr 2023 19:04:11 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=B9=20?= =?UTF-8?q?=D0=B8=20=D0=91=D0=94.=20=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84?= =?UTF-8?q?=D0=B5=D0=B9=D1=81=D0=BE=D0=B2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ComponentStorage.cs | 89 +++++++++ .../Implements/DishStorage.cs | 110 +++++++++++ .../Implements/OrderStorage.cs | 85 +++++++++ .../20230402145825_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20230402145825_InitialCreate.cs | 125 +++++++++++++ ...AbstractFoodOrdersDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Dish.cs | 4 +- .../Models/Order.cs | 2 + FoodOrders/FoodOrders/FoodOrders.csproj | 1 + 9 files changed, 753 insertions(+), 2 deletions(-) create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/DishStorage.cs create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230402145825_InitialCreate.Designer.cs create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230402145825_InitialCreate.cs create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/AbstractFoodOrdersDatabaseModelSnapshot.cs diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/ComponentStorage.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..da92791 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,89 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.StoragesContracts; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new AbstractFoodOrdersDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new AbstractFoodOrdersDatabase(); + 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 AbstractFoodOrdersDatabase(); + 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 AbstractFoodOrdersDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new AbstractFoodOrdersDatabase(); + 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 AbstractFoodOrdersDatabase(); + 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/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/DishStorage.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/DishStorage.cs new file mode 100644 index 0000000..905b155 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/DishStorage.cs @@ -0,0 +1,110 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.StoragesContracts; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersDatabaseImplement.Implements +{ + public class DishStorage : IDishStorage + { + public List GetFullList() + { + using var context = new AbstractFoodOrdersDatabase(); + return context.Dishes + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(DishSearchModel model) + { + if (string.IsNullOrEmpty(model.DishName)) + { + return new(); + } + using var context = new AbstractFoodOrdersDatabase(); + return context.Dishes + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.DishName.Contains(model.DishName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public DishViewModel? GetElement(DishSearchModel model) + { + if (string.IsNullOrEmpty(model.DishName) && + !model.Id.HasValue) + { + return null; + } + using var context = new AbstractFoodOrdersDatabase(); + return context.Dishes + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && + x.DishName == model.DishName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public DishViewModel? Insert(DishBindingModel model) + { + using var context = new AbstractFoodOrdersDatabase(); + var newProduct = Dish.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Dishes.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + public DishViewModel? Update(DishBindingModel model) + { + using var context = new AbstractFoodOrdersDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Dishes.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public DishViewModel? Delete(DishBindingModel model) + { + using var context = new AbstractFoodOrdersDatabase(); + var element = context.Dishes + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Dishes.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..0e5b831 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,85 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.StoragesContracts; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new AbstractFoodOrdersDatabase(); + return context.Orders.Include(x => x.Dish).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 AbstractFoodOrdersDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Include(x => x.Dish) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new AbstractFoodOrdersDatabase(); + return context.Orders.Include(x => x.Dish).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new AbstractFoodOrdersDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders.Include(x => x.Dish).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new AbstractFoodOrdersDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders.Include(x => x.Dish).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new AbstractFoodOrdersDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230402145825_InitialCreate.Designer.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230402145825_InitialCreate.Designer.cs new file mode 100644 index 0000000..a091425 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230402145825_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using AbstractFoodOrdersDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AbstractFoodOrdersDatabaseImplement.Migrations +{ + [DbContext(typeof(AbstractFoodOrdersDatabase))] + [Migration("20230402145825_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.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("AbstractFoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DishName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Dishes"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DishId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("DishId"); + + b.ToTable("DishComponents"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.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("DishId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DishId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Component", "Component") + .WithMany("DishComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Components") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Order", b => + { + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Orders") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Component", b => + { + b.Navigation("DishComponents"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230402145825_InitialCreate.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230402145825_InitialCreate.cs new file mode 100644 index 0000000..1a51d11 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230402145825_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AbstractFoodOrdersDatabaseImplement.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: "Dishes", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DishName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Dishes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DishComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DishId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DishComponents", x => x.Id); + table.ForeignKey( + name: "FK_DishComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_DishComponents_Dishes_DishId", + column: x => x.DishId, + principalTable: "Dishes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DishId = 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_Dishes_DishId", + column: x => x.DishId, + principalTable: "Dishes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_DishComponents_ComponentId", + table: "DishComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_DishComponents_DishId", + table: "DishComponents", + column: "DishId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_DishId", + table: "Orders", + column: "DishId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DishComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Dishes"); + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/AbstractFoodOrdersDatabaseModelSnapshot.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/AbstractFoodOrdersDatabaseModelSnapshot.cs new file mode 100644 index 0000000..3098f2b --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/AbstractFoodOrdersDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using AbstractFoodOrdersDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AbstractFoodOrdersDatabaseImplement.Migrations +{ + [DbContext(typeof(AbstractFoodOrdersDatabase))] + partial class AbstractFoodOrdersDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.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("AbstractFoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DishName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Dishes"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DishId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("DishId"); + + b.ToTable("DishComponents"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.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("DishId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DishId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Component", "Component") + .WithMany("DishComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Components") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Order", b => + { + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Orders") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Component", b => + { + b.Navigation("DishComponents"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Dish.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Dish.cs index 8242596..7862f7c 100644 --- a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Dish.cs +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Dish.cs @@ -33,9 +33,9 @@ namespace AbstractFoodOrdersDatabaseImplement.Models return _dishComponents; } } - [ForeignKey("ProductId")] + [ForeignKey("DishId")] public virtual List Components { get; set; } = new(); - [ForeignKey("ProductId")] + [ForeignKey("DishId")] public virtual List Orders { get; set; } = new(); public static Dish Create(AbstractFoodOrdersDatabase context, DishBindingModel model) diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs index 8aa66c3..f605720 100644 --- a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs @@ -3,6 +3,7 @@ using AbstractFoodOrdersContracts.ViewModels; using AbstractFoodOrdersDataModels.Enums; using AbstractFoodOrdersDataModels.Models; using System; +using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; @@ -25,6 +26,7 @@ namespace AbstractFoodOrdersDatabaseImplement.Models [Required] public DateTime DateCreate { get; private set; } public DateTime? DateImplement { get; private set; } + public virtual Dish Dish { get; set; } public static Order? Create(OrderBindingModel? model) { if (model == null) diff --git a/FoodOrders/FoodOrders/FoodOrders.csproj b/FoodOrders/FoodOrders/FoodOrders.csproj index 9db7e33..80aba3f 100644 --- a/FoodOrders/FoodOrders/FoodOrders.csproj +++ b/FoodOrders/FoodOrders/FoodOrders.csproj @@ -20,6 +20,7 @@ +