diff --git a/MotorPlant/MotorPlantDatabaseImplement/Implements/EngineStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/Implements/EngineStorage.cs index e5a7b09..fde0c80 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Implements/EngineStorage.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/EngineStorage.cs @@ -38,14 +38,14 @@ namespace MotorPlantDatabaseImplement.Implements public EngineViewModel? Insert(EngineBindingModel model) { using var context = new MotorPlantDatabase(); - var newDress = Engine.Create(context, model); - if (newDress == null) + var newEngine = Engine.Create(context, model); + if (newEngine == null) { return null; } - context.Engines.Add(newDress); + context.Engines.Add(newEngine); context.SaveChanges(); - return newDress.GetViewModel; + return newEngine.GetViewModel; } public EngineViewModel? Update(EngineBindingModel model) { @@ -53,17 +53,17 @@ namespace MotorPlantDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var dress = context.Engines.FirstOrDefault(rec => + var engine = context.Engines.FirstOrDefault(rec => rec.Id == model.Id); - if (dress == null) + if (engine == null) { return null; } - dress.Update(model); + engine.Update(model); context.SaveChanges(); - dress.UpdateComponents(context, model); + engine.UpdateComponents(context, model); transaction.Commit(); - return dress.GetViewModel; + return engine.GetViewModel; } catch { diff --git a/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs index 0d22e5b..aeea1ee 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs @@ -12,17 +12,20 @@ namespace MotorPlantDatabaseImplement.Implements public List GetFullList() { using var context = new MotorPlantDatabase(); - return context.Orders.ToList().Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Engine).Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(OrderSearchModel model) { - var result = new List(); - var element = GetElement(model); - if (element != null) + if (!model.Id.HasValue) { - result.Add(element); + return new(); } - return new(); + using var context = new MotorPlantDatabase(); + return context.Orders + .Include(x => x.Engine) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) { @@ -31,54 +34,52 @@ namespace MotorPlantDatabaseImplement.Implements return null; } using var context = new MotorPlantDatabase(); - return context.Orders + return context.Orders.Include(x => x.Engine) .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { - using var context = new MotorPlantDatabase(); var newOrder = Order.Create(model); if (newOrder == null) { return null; } + using var context = new MotorPlantDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; + return context.Orders + .Include(x => x.Engine) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { using var context = new MotorPlantDatabase(); - using var transaction = context.Database.BeginTransaction(); - try + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) { - var order = context.Orders.FirstOrDefault(rec => - rec.Id == model.Id); - if (order == null) - { - return null; - } - order.Update(model); - context.SaveChanges(); - transaction.Commit(); - return order.GetViewModel; - } - catch - { - transaction.Rollback(); - throw; + return null; } + order.Update(model); + context.SaveChanges(); + return context.Orders + .Include(x => x.Engine) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { using var context = new MotorPlantDatabase(); - var element = context.Orders - .FirstOrDefault(x => x.Id == model.Id); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { + var deletedElement = context.Orders + .Include(x => x.Engine) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; context.Orders.Remove(element); context.SaveChanges(); - return element.GetViewModel; + return deletedElement; } return null; } diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.Designer.cs deleted file mode 100644 index 54d714c..0000000 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.Designer.cs +++ /dev/null @@ -1,169 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using MotorPlantDatabaseImplement; - -#nullable disable - -namespace MotorPlantDatabaseImplement.Migrations -{ - [DbContext(typeof(MotorPlantDatabase))] - [Migration("20240322111242_InitMigration")] - partial class InitMigration - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.16") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("MotorPlantDatabaseImplement.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("MotorPlantDatabaseImplement.Models.Engine", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("EngineName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Engines"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("EngineId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("ComponentId"); - - b.HasIndex("EngineId"); - - b.ToTable("EngineComponents"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.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("EngineId") - .HasColumnType("int"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.HasIndex("EngineId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => - { - b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component") - .WithMany("EngineComponents") - .HasForeignKey("ComponentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") - .WithMany("Components") - .HasForeignKey("EngineId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("Engine"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => - { - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", null) - .WithMany("Orders") - .HasForeignKey("EngineId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b => - { - b.Navigation("EngineComponents"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b => - { - b.Navigation("Components"); - - b.Navigation("Orders"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs deleted file mode 100644 index dd0a6da..0000000 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs +++ /dev/null @@ -1,125 +0,0 @@ - using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MotorPlantDatabaseImplement.Migrations -{ - /// - public partial class InitMigration : 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: "varchar", nullable: false), - Cost = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Components", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Engines", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - EngineName = table.Column(type: "varchar", nullable: false), - Price = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Engines", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "EngineComponents", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - EngineId = 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_EngineComponents", x => x.Id); - table.ForeignKey( - name: "FK_EngineComponents_Components_ComponentId", - column: x => x.ComponentId, - principalTable: "Components", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_EngineComponents_Engines_EngineId", - column: x => x.EngineId, - principalTable: "Engines", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Orders", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - EngineId = 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: "timestamp", nullable: false), - DateImplement = table.Column(type: "timestamp", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Orders", x => x.Id); - table.ForeignKey( - name: "FK_Orders_Engines_EngineId", - column: x => x.EngineId, - principalTable: "Engines", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_EngineComponents_ComponentId", - table: "EngineComponents", - column: "ComponentId"); - - migrationBuilder.CreateIndex( - name: "IX_EngineComponents_EngineId", - table: "EngineComponents", - column: "EngineId"); - - migrationBuilder.CreateIndex( - name: "IX_Orders_EngineId", - table: "Orders", - column: "EngineId"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "EngineComponents"); - - migrationBuilder.DropTable( - name: "Orders"); - - migrationBuilder.DropTable( - name: "Components"); - - migrationBuilder.DropTable( - name: "Engines"); - } - } -} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.Designer.cs deleted file mode 100644 index c2c02be..0000000 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.Designer.cs +++ /dev/null @@ -1,173 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using MotorPlantDatabaseImplement; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace MotorPlantDatabaseImplement.Migrations -{ - [DbContext(typeof(MotorPlantDatabase))] - [Migration("20240322183023_NewMigration")] - partial class NewMigration - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.16") - .HasAnnotation("Relational:MaxIdentifierLength", 63); - - NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("text"); - - b.Property("Cost") - .HasColumnType("double precision"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("EngineName") - .IsRequired() - .HasColumnType("text"); - - b.Property("Price") - .HasColumnType("double precision"); - - b.HasKey("Id"); - - b.ToTable("Engines"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("integer"); - - b.Property("Count") - .HasColumnType("integer"); - - b.Property("DressId") - .HasColumnType("integer"); - - b.Property("EngineId") - .HasColumnType("integer"); - - b.HasKey("Id"); - - b.HasIndex("ComponentId"); - - b.HasIndex("DressId"); - - b.ToTable("EngineComponents"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("integer"); - - b.Property("DateCreate") - .HasColumnType("timestamp without time zone"); - - b.Property("DateImplement") - .HasColumnType("timestamp without time zone"); - - b.Property("DressId") - .HasColumnType("integer"); - - b.Property("EngineId") - .HasColumnType("integer"); - - b.Property("Status") - .HasColumnType("integer"); - - b.Property("Sum") - .HasColumnType("double precision"); - - b.HasKey("Id"); - - b.HasIndex("DressId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => - { - b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component") - .WithMany("DressComponents") - .HasForeignKey("ComponentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") - .WithMany("Components") - .HasForeignKey("DressId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("Engine"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => - { - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", null) - .WithMany("Orders") - .HasForeignKey("DressId"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b => - { - b.Navigation("DressComponents"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b => - { - b.Navigation("Components"); - - b.Navigation("Orders"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.cs deleted file mode 100644 index 3da7323..0000000 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.cs +++ /dev/null @@ -1,406 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace MotorPlantDatabaseImplement.Migrations -{ - /// - public partial class NewMigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropForeignKey( - name: "FK_EngineComponents_Engines_EngineId", - table: "EngineComponents"); - - migrationBuilder.DropForeignKey( - name: "FK_Orders_Engines_EngineId", - table: "Orders"); - - migrationBuilder.DropIndex( - name: "IX_Orders_EngineId", - table: "Orders"); - - migrationBuilder.DropIndex( - name: "IX_EngineComponents_EngineId", - table: "EngineComponents"); - - migrationBuilder.AlterColumn( - name: "Sum", - table: "Orders", - type: "double precision", - nullable: false, - oldClrType: typeof(double), - oldType: "float"); - - migrationBuilder.AlterColumn( - name: "Status", - table: "Orders", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int"); - - migrationBuilder.AlterColumn( - name: "EngineId", - table: "Orders", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int"); - - migrationBuilder.AlterColumn( - name: "DateImplement", - table: "Orders", - type: "timestamp without time zone", - nullable: true, - oldClrType: typeof(DateTime), - oldType: "datetime2", - oldNullable: true); - - migrationBuilder.AlterColumn( - name: "DateCreate", - table: "Orders", - type: "timestamp without time zone", - nullable: false, - oldClrType: typeof(DateTime), - oldType: "datetime2"); - - migrationBuilder.AlterColumn( - name: "Count", - table: "Orders", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int"); - - migrationBuilder.AlterColumn( - name: "Id", - table: "Orders", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int") - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - migrationBuilder.AddColumn( - name: "DressId", - table: "Orders", - type: "integer", - nullable: true); - - migrationBuilder.AlterColumn( - name: "Price", - table: "Engines", - type: "double precision", - nullable: false, - oldClrType: typeof(double), - oldType: "float"); - - migrationBuilder.AlterColumn( - name: "EngineName", - table: "Engines", - type: "text", - nullable: false, - oldClrType: typeof(string), - oldType: "nvarchar(max)"); - - migrationBuilder.AlterColumn( - name: "Id", - table: "Engines", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int") - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - migrationBuilder.AlterColumn( - name: "EngineId", - table: "EngineComponents", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int"); - - migrationBuilder.AlterColumn( - name: "Count", - table: "EngineComponents", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int"); - - migrationBuilder.AlterColumn( - name: "ComponentId", - table: "EngineComponents", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int"); - - migrationBuilder.AlterColumn( - name: "Id", - table: "EngineComponents", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int") - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - migrationBuilder.AddColumn( - name: "DressId", - table: "EngineComponents", - type: "integer", - nullable: false, - defaultValue: 0); - - migrationBuilder.AlterColumn( - name: "Cost", - table: "Components", - type: "double precision", - nullable: false, - oldClrType: typeof(double), - oldType: "float"); - - migrationBuilder.AlterColumn( - name: "ComponentName", - table: "Components", - type: "text", - nullable: false, - oldClrType: typeof(string), - oldType: "varchar"); - - migrationBuilder.AlterColumn( - name: "Id", - table: "Components", - type: "integer", - nullable: false, - oldClrType: typeof(int), - oldType: "int") - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - migrationBuilder.CreateIndex( - name: "IX_Orders_DressId", - table: "Orders", - column: "DressId"); - - migrationBuilder.CreateIndex( - name: "IX_EngineComponents_DressId", - table: "EngineComponents", - column: "DressId"); - - migrationBuilder.AddForeignKey( - name: "FK_EngineComponents_Engines_DressId", - table: "EngineComponents", - column: "DressId", - principalTable: "Engines", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - - migrationBuilder.AddForeignKey( - name: "FK_Orders_Engines_DressId", - table: "Orders", - column: "DressId", - principalTable: "Engines", - principalColumn: "Id"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropForeignKey( - name: "FK_EngineComponents_Engines_DressId", - table: "EngineComponents"); - - migrationBuilder.DropForeignKey( - name: "FK_Orders_Engines_DressId", - table: "Orders"); - - migrationBuilder.DropIndex( - name: "IX_Orders_DressId", - table: "Orders"); - - migrationBuilder.DropIndex( - name: "IX_EngineComponents_DressId", - table: "EngineComponents"); - - migrationBuilder.DropColumn( - name: "DressId", - table: "Orders"); - - migrationBuilder.DropColumn( - name: "DressId", - table: "EngineComponents"); - - migrationBuilder.AlterColumn( - name: "Sum", - table: "Orders", - type: "float", - nullable: false, - oldClrType: typeof(double), - oldType: "double precision"); - - migrationBuilder.AlterColumn( - name: "Status", - table: "Orders", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer"); - - migrationBuilder.AlterColumn( - name: "EngineId", - table: "Orders", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer"); - - migrationBuilder.AlterColumn( - name: "DateImplement", - table: "Orders", - type: "datetime2", - nullable: true, - oldClrType: typeof(DateTime), - oldType: "timestamp without time zone", - oldNullable: true); - - migrationBuilder.AlterColumn( - name: "DateCreate", - table: "Orders", - type: "datetime2", - nullable: false, - oldClrType: typeof(DateTime), - oldType: "timestamp without time zone"); - - migrationBuilder.AlterColumn( - name: "Count", - table: "Orders", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer"); - - migrationBuilder.AlterColumn( - name: "Id", - table: "Orders", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer") - .OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - migrationBuilder.AlterColumn( - name: "Price", - table: "Engines", - type: "float", - nullable: false, - oldClrType: typeof(double), - oldType: "double precision"); - - migrationBuilder.AlterColumn( - name: "EngineName", - table: "Engines", - type: "nvarchar(max)", - nullable: false, - oldClrType: typeof(string), - oldType: "text"); - - migrationBuilder.AlterColumn( - name: "Id", - table: "Engines", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer") - .OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - migrationBuilder.AlterColumn( - name: "EngineId", - table: "EngineComponents", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer"); - - migrationBuilder.AlterColumn( - name: "Count", - table: "EngineComponents", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer"); - - migrationBuilder.AlterColumn( - name: "ComponentId", - table: "EngineComponents", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer"); - - migrationBuilder.AlterColumn( - name: "Id", - table: "EngineComponents", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer") - .OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - migrationBuilder.AlterColumn( - name: "Cost", - table: "Components", - type: "float", - nullable: false, - oldClrType: typeof(double), - oldType: "double precision"); - - migrationBuilder.AlterColumn( - name: "ComponentName", - table: "Components", - type: "varchar", - nullable: false, - oldClrType: typeof(string), - oldType: "text"); - - migrationBuilder.AlterColumn( - name: "Id", - table: "Components", - type: "int", - nullable: false, - oldClrType: typeof(int), - oldType: "integer") - .OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - migrationBuilder.CreateIndex( - name: "IX_Orders_EngineId", - table: "Orders", - column: "EngineId"); - - migrationBuilder.CreateIndex( - name: "IX_EngineComponents_EngineId", - table: "EngineComponents", - column: "EngineId"); - - migrationBuilder.AddForeignKey( - name: "FK_EngineComponents_Engines_EngineId", - table: "EngineComponents", - column: "EngineId", - principalTable: "Engines", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - - migrationBuilder.AddForeignKey( - name: "FK_Orders_Engines_EngineId", - table: "Orders", - column: "EngineId", - principalTable: "Engines", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - } - } -} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs deleted file mode 100644 index 0703363..0000000 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs +++ /dev/null @@ -1,170 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using MotorPlantDatabaseImplement; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace MotorPlantDatabaseImplement.Migrations -{ - [DbContext(typeof(MotorPlantDatabase))] - partial class MotorPlantDatabaseModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.16") - .HasAnnotation("Relational:MaxIdentifierLength", 63); - - NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("text"); - - b.Property("Cost") - .HasColumnType("double precision"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("EngineName") - .IsRequired() - .HasColumnType("text"); - - b.Property("Price") - .HasColumnType("double precision"); - - b.HasKey("Id"); - - b.ToTable("Engines"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("integer"); - - b.Property("Count") - .HasColumnType("integer"); - - b.Property("DressId") - .HasColumnType("integer"); - - b.Property("EngineId") - .HasColumnType("integer"); - - b.HasKey("Id"); - - b.HasIndex("ComponentId"); - - b.HasIndex("DressId"); - - b.ToTable("EngineComponents"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("integer"); - - b.Property("DateCreate") - .HasColumnType("timestamp without time zone"); - - b.Property("DateImplement") - .HasColumnType("timestamp without time zone"); - - b.Property("DressId") - .HasColumnType("integer"); - - b.Property("EngineId") - .HasColumnType("integer"); - - b.Property("Status") - .HasColumnType("integer"); - - b.Property("Sum") - .HasColumnType("double precision"); - - b.HasKey("Id"); - - b.HasIndex("DressId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => - { - b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component") - .WithMany("DressComponents") - .HasForeignKey("ComponentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") - .WithMany("Components") - .HasForeignKey("DressId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("Engine"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => - { - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", null) - .WithMany("Orders") - .HasForeignKey("DressId"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b => - { - b.Navigation("DressComponents"); - }); - - modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b => - { - b.Navigation("Components"); - - b.Navigation("Orders"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/Engine.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Engine.cs index 91d303a..44db39e 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Models/Engine.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Engine.cs @@ -26,9 +26,9 @@ namespace MotorPlantDatabaseImplement.Models return _engineComponents; } } - [ForeignKey("DressId")] + [ForeignKey("EngineId")] public virtual List Components { get; set; } = new(); - [ForeignKey("DressId")] + [ForeignKey("EngineId")] public virtual List Orders { get; set; } = new(); public static Engine Create(MotorPlantDatabase context, EngineBindingModel model) { diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs index dd2d2b6..00cb15b 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs @@ -1,26 +1,35 @@ using MotorPlantContracts.BindingModels; using MotorPlantContracts.ViewModels; using MotorPlantDataModels.Enums; +using MotorPlantDataModels.Models; using System.ComponentModel.DataAnnotations; namespace MotorPlantDatabaseImplement.Models { - public class Order + public class Order : IOrderModel { public int Id { get; private set; } + [Required] public int EngineId { get; private set; } + [Required] public int Count { get; private set; } + [Required] public double Sum { get; private set; } + [Required] public OrderStatus Status { get; private set; } + [Required] public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } - public static Order? Create(OrderBindingModel model) + public virtual Engine Engine { get; set; } + + public static Order? Create(OrderBindingModel? model) { if (model == null) { @@ -33,9 +42,11 @@ namespace MotorPlantDatabaseImplement.Models Count = model.Count, Sum = model.Sum, Status = model.Status, - DateCreate = model.DateCreate + DateCreate = model.DateCreate, + DateImplement = model.DateImplement }; } + public void Update(OrderBindingModel model) { if (model == null) @@ -45,6 +56,7 @@ namespace MotorPlantDatabaseImplement.Models Status = model.Status; DateImplement = model.DateImplement; } + public OrderViewModel GetViewModel => new() { Id = Id, @@ -53,7 +65,8 @@ namespace MotorPlantDatabaseImplement.Models Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement + DateImplement = DateImplement, + EngineName = Engine.EngineName }; } -} +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantFileImplement/Implements/OrderStorage.cs b/MotorPlant/MotorPlantFileImplement/Implements/OrderStorage.cs index 3bb7e5b..d79a83b 100644 --- a/MotorPlant/MotorPlantFileImplement/Implements/OrderStorage.cs +++ b/MotorPlant/MotorPlantFileImplement/Implements/OrderStorage.cs @@ -83,14 +83,8 @@ namespace MotorPlantFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - foreach (var comp in source.Engines) - { - if (comp.Id == order.EngineId) - { - viewModel.EngineName = comp.EngineName; - break; - } - } + var engine = source.Engines.FirstOrDefault(x => x.Id == order.EngineId); + viewModel.EngineName = engine?.EngineName; return viewModel; } } diff --git a/MotorPlant/MotorPlantListImplement/Models/Order.cs b/MotorPlant/MotorPlantListImplement/Models/Order.cs index 56053d1..e8196ac 100644 --- a/MotorPlant/MotorPlantListImplement/Models/Order.cs +++ b/MotorPlant/MotorPlantListImplement/Models/Order.cs @@ -49,12 +49,7 @@ namespace MotorPlantListImplement.Models { return; } - Id = model.Id; - EngineId = model.EngineId; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new()