commitLast

This commit is contained in:
Salikh 2024-04-03 16:47:50 +04:00
parent 0a68dbfa4d
commit 76fcddd15d
11 changed files with 61 additions and 1101 deletions

View File

@ -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
{

View File

@ -12,17 +12,20 @@ namespace MotorPlantDatabaseImplement.Implements
public List<OrderViewModel> 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<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
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;
}

View File

@ -1,169 +0,0 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("EngineName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Engines");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("EngineId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("EngineId");
b.ToTable("EngineComponents");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("EngineId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("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
}
}
}

View File

@ -1,125 +0,0 @@
 using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MotorPlantDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComponentName = table.Column<string>(type: "varchar", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Engines",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
EngineName = table.Column<string>(type: "varchar", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Engines", x => x.Id);
});
migrationBuilder.CreateTable(
name: "EngineComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
EngineId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(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<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
EngineId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false),
Sum = table.Column<double>(type: "float", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
DateCreate = table.Column<DateTime>(type: "timestamp", nullable: false),
DateImplement = table.Column<DateTime>(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");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "EngineComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Engines");
}
}
}

View File

@ -1,173 +0,0 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Cost")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("EngineName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Engines");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("DressId")
.HasColumnType("integer");
b.Property<int>("EngineId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("DressId");
b.ToTable("EngineComponents");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<DateTime>("DateCreate")
.HasColumnType("timestamp without time zone");
b.Property<DateTime?>("DateImplement")
.HasColumnType("timestamp without time zone");
b.Property<int?>("DressId")
.HasColumnType("integer");
b.Property<int>("EngineId")
.HasColumnType("integer");
b.Property<int>("Status")
.HasColumnType("integer");
b.Property<double>("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
}
}
}

View File

@ -1,406 +0,0 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace MotorPlantDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class NewMigration : Migration
{
/// <inheritdoc />
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<double>(
name: "Sum",
table: "Orders",
type: "double precision",
nullable: false,
oldClrType: typeof(double),
oldType: "float");
migrationBuilder.AlterColumn<int>(
name: "Status",
table: "Orders",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.AlterColumn<int>(
name: "EngineId",
table: "Orders",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.AlterColumn<DateTime>(
name: "DateImplement",
table: "Orders",
type: "timestamp without time zone",
nullable: true,
oldClrType: typeof(DateTime),
oldType: "datetime2",
oldNullable: true);
migrationBuilder.AlterColumn<DateTime>(
name: "DateCreate",
table: "Orders",
type: "timestamp without time zone",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "datetime2");
migrationBuilder.AlterColumn<int>(
name: "Count",
table: "Orders",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "Orders",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "int")
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
migrationBuilder.AddColumn<int>(
name: "DressId",
table: "Orders",
type: "integer",
nullable: true);
migrationBuilder.AlterColumn<double>(
name: "Price",
table: "Engines",
type: "double precision",
nullable: false,
oldClrType: typeof(double),
oldType: "float");
migrationBuilder.AlterColumn<string>(
name: "EngineName",
table: "Engines",
type: "text",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)");
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "Engines",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "int")
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
migrationBuilder.AlterColumn<int>(
name: "EngineId",
table: "EngineComponents",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.AlterColumn<int>(
name: "Count",
table: "EngineComponents",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.AlterColumn<int>(
name: "ComponentId",
table: "EngineComponents",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "EngineComponents",
type: "integer",
nullable: false,
oldClrType: typeof(int),
oldType: "int")
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
migrationBuilder.AddColumn<int>(
name: "DressId",
table: "EngineComponents",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AlterColumn<double>(
name: "Cost",
table: "Components",
type: "double precision",
nullable: false,
oldClrType: typeof(double),
oldType: "float");
migrationBuilder.AlterColumn<string>(
name: "ComponentName",
table: "Components",
type: "text",
nullable: false,
oldClrType: typeof(string),
oldType: "varchar");
migrationBuilder.AlterColumn<int>(
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");
}
/// <inheritdoc />
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<double>(
name: "Sum",
table: "Orders",
type: "float",
nullable: false,
oldClrType: typeof(double),
oldType: "double precision");
migrationBuilder.AlterColumn<int>(
name: "Status",
table: "Orders",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "integer");
migrationBuilder.AlterColumn<int>(
name: "EngineId",
table: "Orders",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "integer");
migrationBuilder.AlterColumn<DateTime>(
name: "DateImplement",
table: "Orders",
type: "datetime2",
nullable: true,
oldClrType: typeof(DateTime),
oldType: "timestamp without time zone",
oldNullable: true);
migrationBuilder.AlterColumn<DateTime>(
name: "DateCreate",
table: "Orders",
type: "datetime2",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp without time zone");
migrationBuilder.AlterColumn<int>(
name: "Count",
table: "Orders",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "integer");
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "Orders",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "integer")
.OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
migrationBuilder.AlterColumn<double>(
name: "Price",
table: "Engines",
type: "float",
nullable: false,
oldClrType: typeof(double),
oldType: "double precision");
migrationBuilder.AlterColumn<string>(
name: "EngineName",
table: "Engines",
type: "nvarchar(max)",
nullable: false,
oldClrType: typeof(string),
oldType: "text");
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "Engines",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "integer")
.OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
migrationBuilder.AlterColumn<int>(
name: "EngineId",
table: "EngineComponents",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "integer");
migrationBuilder.AlterColumn<int>(
name: "Count",
table: "EngineComponents",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "integer");
migrationBuilder.AlterColumn<int>(
name: "ComponentId",
table: "EngineComponents",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "integer");
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "EngineComponents",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "integer")
.OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
migrationBuilder.AlterColumn<double>(
name: "Cost",
table: "Components",
type: "float",
nullable: false,
oldClrType: typeof(double),
oldType: "double precision");
migrationBuilder.AlterColumn<string>(
name: "ComponentName",
table: "Components",
type: "varchar",
nullable: false,
oldClrType: typeof(string),
oldType: "text");
migrationBuilder.AlterColumn<int>(
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);
}
}
}

View File

@ -1,170 +0,0 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Cost")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("EngineName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Engines");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("DressId")
.HasColumnType("integer");
b.Property<int>("EngineId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("DressId");
b.ToTable("EngineComponents");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<DateTime>("DateCreate")
.HasColumnType("timestamp without time zone");
b.Property<DateTime?>("DateImplement")
.HasColumnType("timestamp without time zone");
b.Property<int?>("DressId")
.HasColumnType("integer");
b.Property<int>("EngineId")
.HasColumnType("integer");
b.Property<int>("Status")
.HasColumnType("integer");
b.Property<double>("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
}
}
}

View File

@ -26,9 +26,9 @@ namespace MotorPlantDatabaseImplement.Models
return _engineComponents;
}
}
[ForeignKey("DressId")]
[ForeignKey("EngineId")]
public virtual List<EngineComponent> Components { get; set; } = new();
[ForeignKey("DressId")]
[ForeignKey("EngineId")]
public virtual List<Order> Orders { get; set; } = new();
public static Engine Create(MotorPlantDatabase context, EngineBindingModel model)
{

View File

@ -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
};
}
}
}

View File

@ -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;
}
}

View File

@ -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()