From e370affee5e14e726cfde4940f5d7caddfe23662 Mon Sep 17 00:00:00 2001 From: Salikh Date: Fri, 22 Mar 2024 22:40:04 +0400 Subject: [PATCH 1/8] FirtstCommit --- MotorPlant/MotorPlant.sln | 6 + .../MotorPlantBusinessLogic.csproj | 1 + .../MotorPlantContracts.csproj | 4 + .../MotorPlantDataModels.csproj | 4 + .../Implements/ComponentStorage.cs | 71 +++ .../Implements/EngineStorage.cs | 89 ++++ .../Implements/OrderStorage.cs | 86 ++++ .../20240322111242_InitMigration.Designer.cs | 169 ++++++++ .../20240322111242_InitMigration.cs | 125 ++++++ .../20240322183023_NewMigration.Designer.cs | 173 ++++++++ .../Migrations/20240322183023_NewMigration.cs | 406 ++++++++++++++++++ .../MotorPlantDatabaseModelSnapshot.cs | 170 ++++++++ .../Models/Component.cs | 56 +++ .../Models/Engine.cs | 87 ++++ .../Models/EngineComponent.cs | 22 + .../Models/Order.cs | 59 +++ .../MotorPlantDatabase.cs | 23 + .../MotorPlantDatabaseImplement.csproj | 28 ++ .../MotorPlantFileImplement.csproj | 4 + .../MotorPlantListImplement.csproj | 4 + .../MotorPlantView/MotorPlantView.csproj | 6 + MotorPlant/MotorPlantView/Program.cs | 2 +- 22 files changed, 1594 insertions(+), 1 deletion(-) create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Implements/EngineStorage.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.Designer.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.Designer.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Models/Engine.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Models/EngineComponent.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabase.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj diff --git a/MotorPlant/MotorPlant.sln b/MotorPlant/MotorPlant.sln index 1bc644b..a312a2f 100644 --- a/MotorPlant/MotorPlant.sln +++ b/MotorPlant/MotorPlant.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MotorPlantBusinessLogic", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MotorPlantFileImplement", "MotorPlantFileImplement\MotorPlantFileImplement.csproj", "{183138D6-8EC0-46FD-9B1B-03DBCFEDD7F4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MotorPlantDatabaseImplement", "MotorPlantDatabaseImplement\MotorPlantDatabaseImplement.csproj", "{30979D82-B5E7-470E-8083-1207FE9ADA5C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {183138D6-8EC0-46FD-9B1B-03DBCFEDD7F4}.Debug|Any CPU.Build.0 = Debug|Any CPU {183138D6-8EC0-46FD-9B1B-03DBCFEDD7F4}.Release|Any CPU.ActiveCfg = Release|Any CPU {183138D6-8EC0-46FD-9B1B-03DBCFEDD7F4}.Release|Any CPU.Build.0 = Release|Any CPU + {30979D82-B5E7-470E-8083-1207FE9ADA5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {30979D82-B5E7-470E-8083-1207FE9ADA5C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {30979D82-B5E7-470E-8083-1207FE9ADA5C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {30979D82-B5E7-470E-8083-1207FE9ADA5C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj b/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj index 46d0222..3558ce0 100644 --- a/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj +++ b/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/MotorPlant/MotorPlantContracts/MotorPlantContracts.csproj b/MotorPlant/MotorPlantContracts/MotorPlantContracts.csproj index 3fdb632..f473326 100644 --- a/MotorPlant/MotorPlantContracts/MotorPlantContracts.csproj +++ b/MotorPlant/MotorPlantContracts/MotorPlantContracts.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/MotorPlant/MotorPlantDataModels/MotorPlantDataModels.csproj b/MotorPlant/MotorPlantDataModels/MotorPlantDataModels.csproj index 132c02c..8a526fb 100644 --- a/MotorPlant/MotorPlantDataModels/MotorPlantDataModels.csproj +++ b/MotorPlant/MotorPlantDataModels/MotorPlantDataModels.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/MotorPlant/MotorPlantDatabaseImplement/Implements/ComponentStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..619fc26 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,71 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantDatabaseImplement.Models; + +namespace MotorPlantDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new MotorPlantDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new MotorPlantDatabase(); + 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 MotorPlantDatabase(); + 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 MotorPlantDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new MotorPlantDatabase(); + 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 MotorPlantDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element == null) + { + return null; + } + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Implements/EngineStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/Implements/EngineStorage.cs new file mode 100644 index 0000000..e5a7b09 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/EngineStorage.cs @@ -0,0 +1,89 @@ +using Microsoft.EntityFrameworkCore; +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantDatabaseImplement.Models; + +namespace MotorPlantDatabaseImplement.Implements +{ + public class EngineStorage : IEngineStorage + { + public List GetFullList() + { + using var context = new MotorPlantDatabase(); + return context.Engines.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(EngineSearchModel model) + { + if (string.IsNullOrEmpty(model.EngineName)) + { + return new(); + } + using var context = new MotorPlantDatabase(); + return context.Engines.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.EngineName.Contains(model.EngineName)).ToList().Select(x => x.GetViewModel).ToList(); + + } + public EngineViewModel? GetElement(EngineSearchModel model) + { + if (string.IsNullOrEmpty(model.EngineName) && + !model.Id.HasValue) + { + return null; + } + using var context = new MotorPlantDatabase(); + return context.Engines.Include(x => x.Components).ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.EngineName) && x.EngineName == model.EngineName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public EngineViewModel? Insert(EngineBindingModel model) + { + using var context = new MotorPlantDatabase(); + var newDress = Engine.Create(context, model); + if (newDress == null) + { + return null; + } + context.Engines.Add(newDress); + context.SaveChanges(); + return newDress.GetViewModel; + } + public EngineViewModel? Update(EngineBindingModel model) + { + using var context = new MotorPlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var dress = context.Engines.FirstOrDefault(rec => + rec.Id == model.Id); + if (dress == null) + { + return null; + } + dress.Update(model); + context.SaveChanges(); + dress.UpdateComponents(context, model); + transaction.Commit(); + return dress.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public EngineViewModel? Delete(EngineBindingModel model) + { + using var context = new MotorPlantDatabase(); + var element = context.Engines + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Engines.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..0d22e5b --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,86 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantDatabaseImplement.Models; + + +namespace MotorPlantDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new MotorPlantDatabase(); + return context.Orders.ToList().Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + var element = GetElement(model); + if (element != null) + { + result.Add(element); + } + return new(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new MotorPlantDatabase(); + return context.Orders + .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; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new MotorPlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + 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; + } + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new MotorPlantDatabase(); + var element = context.Orders + .FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.Designer.cs new file mode 100644 index 0000000..54d714c --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.Designer.cs @@ -0,0 +1,169 @@ +// +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 new file mode 100644 index 0000000..943e89f --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs @@ -0,0 +1,125 @@ +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 new file mode 100644 index 0000000..c2c02be --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.Designer.cs @@ -0,0 +1,173 @@ +// +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 new file mode 100644 index 0000000..3da7323 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.cs @@ -0,0 +1,406 @@ +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 new file mode 100644 index 0000000..0703363 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs @@ -0,0 +1,170 @@ +// +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/Component.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..d2b422a --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using MotorPlantDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; + +namespace MotorPlantDatabaseImplement.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 DressComponents { 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/MotorPlant/MotorPlantDatabaseImplement/Models/Engine.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Engine.cs new file mode 100644 index 0000000..91d303a --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Engine.cs @@ -0,0 +1,87 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace MotorPlantDatabaseImplement.Models +{ + public class Engine : IEngineModel + { + public int Id { get; set; } + [Required] + public string EngineName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _engineComponents = null; + [NotMapped] + public Dictionary EngineComponents + { + get + { + if (_engineComponents == null) + { + _engineComponents = Components.ToDictionary(recDC => recDC.ComponentId, recDC => (recDC.Component as IComponentModel, recDC.Count)); + } + return _engineComponents; + } + } + [ForeignKey("DressId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("DressId")] + public virtual List Orders { get; set; } = new(); + public static Engine Create(MotorPlantDatabase context, EngineBindingModel model) + { + return new Engine() + { + Id = model.Id, + EngineName = model.EngineName, + Price = model.Price, + Components = model.EngineComponents.Select(x => new EngineComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(EngineBindingModel model) + { + EngineName = model.EngineName; + Price = model.Price; + } + public EngineViewModel GetViewModel => new() + { + Id = Id, + EngineName = EngineName, + Price = Price, + EngineComponents = EngineComponents + }; + public void UpdateComponents(MotorPlantDatabase context, EngineBindingModel model) + { + var engineComponents = context.EngineComponents.Where(rec => rec.EngineId == model.Id).ToList(); + if (engineComponents != null && engineComponents.Count > 0) + { + context.EngineComponents.RemoveRange(engineComponents.Where(rec => !model.EngineComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + foreach (var updateComponent in engineComponents) + { + updateComponent.Count = model.EngineComponents[updateComponent.ComponentId].Item2; + model.EngineComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var engine = context.Engines.First(x => x.Id == Id); + foreach (var pc in model.EngineComponents) + { + context.EngineComponents.Add(new EngineComponent + { + Engine = engine, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _engineComponents = null; + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/EngineComponent.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/EngineComponent.cs new file mode 100644 index 0000000..0dc8888 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/EngineComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace MotorPlantDatabaseImplement.Models +{ + public class EngineComponent + { + public int Id { get; set; } + + [Required] + public int EngineId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Engine Engine { get; set; } = new(); + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..dd2d2b6 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs @@ -0,0 +1,59 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Enums; +using System.ComponentModel.DataAnnotations; + +namespace MotorPlantDatabaseImplement.Models +{ + public class Order + { + 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) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + EngineId = model.EngineId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + EngineId = EngineId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabase.cs b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabase.cs new file mode 100644 index 0000000..26ecf49 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabase.cs @@ -0,0 +1,23 @@ +using MotorPlantDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace MotorPlantDatabaseImplement +{ + public class MotorPlantDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=MotorPlant_db;Username=postgres;Password=admin"); + } + base.OnConfiguring(optionsBuilder); + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + public virtual DbSet Components { get; set; } + public virtual DbSet Engines { get; set; } + public virtual DbSet EngineComponents { get; set; } + public virtual DbSet Orders { get; set; } + } +} \ No newline at end of file diff --git a/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj new file mode 100644 index 0000000..6fa33d6 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj @@ -0,0 +1,28 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + diff --git a/MotorPlant/MotorPlantFileImplement/MotorPlantFileImplement.csproj b/MotorPlant/MotorPlantFileImplement/MotorPlantFileImplement.csproj index 4da00b3..706e69d 100644 --- a/MotorPlant/MotorPlantFileImplement/MotorPlantFileImplement.csproj +++ b/MotorPlant/MotorPlantFileImplement/MotorPlantFileImplement.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/MotorPlant/MotorPlantListImplement/MotorPlantListImplement.csproj b/MotorPlant/MotorPlantListImplement/MotorPlantListImplement.csproj index 4da00b3..706e69d 100644 --- a/MotorPlant/MotorPlantListImplement/MotorPlantListImplement.csproj +++ b/MotorPlant/MotorPlantListImplement/MotorPlantListImplement.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/MotorPlant/MotorPlantView/MotorPlantView.csproj b/MotorPlant/MotorPlantView/MotorPlantView.csproj index 81e4b5d..88a413b 100644 --- a/MotorPlant/MotorPlantView/MotorPlantView.csproj +++ b/MotorPlant/MotorPlantView/MotorPlantView.csproj @@ -19,15 +19,21 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/MotorPlant/MotorPlantView/Program.cs b/MotorPlant/MotorPlantView/Program.cs index 27e400e..5d8ccca 100644 --- a/MotorPlant/MotorPlantView/Program.cs +++ b/MotorPlant/MotorPlantView/Program.cs @@ -1,7 +1,7 @@ using MotorPlantView.Forms; using MotorPlantContracts.BusinessLogicsContracts; using MotorPlantContracts.StoragesContracts; -using MotorPlantFileImplement.Implements; +using MotorPlantDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From 0a68dbfa4d624ef606f336358792586f351bd78b Mon Sep 17 00:00:00 2001 From: Salikh Date: Sat, 23 Mar 2024 09:45:21 +0400 Subject: [PATCH 2/8] secondCommit --- .../Migrations/20240322111242_InitMigration.cs | 2 +- MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs index 943e89f..dd0a6da 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs @@ -1,4 +1,4 @@ -using System; + using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs index d2b422a..223ccc7 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs @@ -14,7 +14,7 @@ namespace MotorPlantDatabaseImplement.Models [Required] public double Cost { get; set; } [ForeignKey("ComponentId")] - public virtual List DressComponents { get; set; } = new(); + public virtual List EngineComponents { get; set; } = new(); public static Component? Create(ComponentBindingModel model) { if (model == null) -- 2.25.1 From 76fcddd15d50e9c8aa4ef6323753c4699392a7a9 Mon Sep 17 00:00:00 2001 From: Salikh Date: Wed, 3 Apr 2024 16:47:50 +0400 Subject: [PATCH 3/8] commitLast --- .../Implements/EngineStorage.cs | 18 +- .../Implements/OrderStorage.cs | 59 +-- .../20240322111242_InitMigration.Designer.cs | 169 -------- .../20240322111242_InitMigration.cs | 125 ------ .../20240322183023_NewMigration.Designer.cs | 173 -------- .../Migrations/20240322183023_NewMigration.cs | 406 ------------------ .../MotorPlantDatabaseModelSnapshot.cs | 170 -------- .../Models/Engine.cs | 4 +- .../Models/Order.cs | 23 +- .../Implements/OrderStorage.cs | 10 +- .../MotorPlantListImplement/Models/Order.cs | 5 - 11 files changed, 61 insertions(+), 1101 deletions(-) delete mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.Designer.cs delete mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322111242_InitMigration.cs delete mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.Designer.cs delete mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240322183023_NewMigration.cs delete mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs 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() -- 2.25.1 From e4f5d2f5b6d2db9a9cdd9f82a5c7e33fa40a1ef8 Mon Sep 17 00:00:00 2001 From: Salikh Date: Wed, 3 Apr 2024 17:05:59 +0400 Subject: [PATCH 4/8] migrationCommit --- .../Implements/OrderStorage.cs | 3 +- .../20240403125759_InitMigration.Designer.cs | 171 ++++++++++++++++++ .../20240403125759_InitMigration.cs | 126 +++++++++++++ .../MotorPlantDatabaseModelSnapshot.cs | 168 +++++++++++++++++ 4 files changed, 467 insertions(+), 1 deletion(-) create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.Designer.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs diff --git a/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs index aeea1ee..08c17f1 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs @@ -1,4 +1,5 @@ -using MotorPlantContracts.BindingModels; +using Microsoft.EntityFrameworkCore; +using MotorPlantContracts.BindingModels; using MotorPlantContracts.SearchModels; using MotorPlantContracts.StoragesContracts; using MotorPlantContracts.ViewModels; diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.Designer.cs new file mode 100644 index 0000000..681748f --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.Designer.cs @@ -0,0 +1,171 @@ +// +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("20240403125759_InitMigration")] + partial class InitMigration + { + /// + 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("EngineId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("EngineId"); + + 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("EngineId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + 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", "Engine") + .WithMany("Orders") + .HasForeignKey("EngineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Engine"); + }); + + 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/20240403125759_InitMigration.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.cs new file mode 100644 index 0000000..53bde97 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#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: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Engines", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + EngineName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Engines", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "EngineComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + EngineId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", 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: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + EngineId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp without time zone", nullable: false), + DateImplement = table.Column(type: "timestamp without time zone", 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/MotorPlantDatabaseModelSnapshot.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs new file mode 100644 index 0000000..8e0807e --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +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("EngineId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("EngineId"); + + 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("EngineId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + 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", "Engine") + .WithMany("Orders") + .HasForeignKey("EngineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Engine"); + }); + + 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 + } + } +} -- 2.25.1 From 08d3fb0189285136ee1766c9508b7ff44aa0c269 Mon Sep 17 00:00:00 2001 From: Salikh Date: Thu, 4 Apr 2024 03:01:18 +0400 Subject: [PATCH 5/8] definitely commit --- ...ion.Designer.cs => 20240403225731_InitMigration.Designer.cs} | 2 +- ...3125759_InitMigration.cs => 20240403225731_InitMigration.cs} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240403125759_InitMigration.Designer.cs => 20240403225731_InitMigration.Designer.cs} (99%) rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240403125759_InitMigration.cs => 20240403225731_InitMigration.cs} (100%) diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.Designer.cs similarity index 99% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.Designer.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.Designer.cs index 681748f..575ec39 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.Designer.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.Designer.cs @@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace MotorPlantDatabaseImplement.Migrations { [DbContext(typeof(MotorPlantDatabase))] - [Migration("20240403125759_InitMigration")] + [Migration("20240403225731_InitMigration")] partial class InitMigration { /// diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.cs similarity index 100% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403125759_InitMigration.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.cs -- 2.25.1 From dcef17c190b741fe7f5e4b88a7328cc24f59853a Mon Sep 17 00:00:00 2001 From: Salikh Date: Thu, 11 Apr 2024 17:04:57 +0400 Subject: [PATCH 6/8] first commit --- .../Implements/ShopStorage.cs | 144 ++++++++++++++++++ ...40411122818_InitHardMigration.Designer.cs} | 81 +++++++++- ...cs => 20240411122818_InitHardMigration.cs} | 61 +++++++- .../MotorPlantDatabaseModelSnapshot.cs | 77 ++++++++++ .../Models/Order.cs | 2 - .../Models/Shop.cs | 105 +++++++++++++ .../Models/ShopEngine.cs | 22 +++ .../MotorPlantDatabase.cs | 6 +- .../MotorPlantDatabaseImplement.csproj | 4 - MotorPlant/MotorPlantView/FormCreateOrder.cs | 5 +- MotorPlant/MotorPlantView/FormMain.cs | 2 +- 11 files changed, 495 insertions(+), 14 deletions(-) create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Implements/ShopStorage.cs rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240403225731_InitMigration.Designer.cs => 20240411122818_InitHardMigration.Designer.cs} (68%) rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240403225731_InitMigration.cs => 20240411122818_InitHardMigration.cs} (66%) create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Models/Shop.cs create mode 100644 MotorPlant/MotorPlantDatabaseImplement/Models/ShopEngine.cs diff --git a/MotorPlant/MotorPlantDatabaseImplement/Implements/ShopStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..5772557 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,144 @@ +using Microsoft.EntityFrameworkCore; +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantDatabaseImplement.Models; +using MotorPlantDataModels.Models; + +namespace MotorPlantDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new MotorPlantDatabase(); + var element = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new MotorPlantDatabase(); + return context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine).FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new MotorPlantDatabase(); + return context.Shops + .Include(x => x.Engines) + .ThenInclude(x => x.Engine) + .Select(x => x.GetViewModel) + .Where(x => x.ShopName.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new MotorPlantDatabase(); + return context.Shops + .Include(x => x.Engines) + .ThenInclude(x => x.Engine) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new MotorPlantDatabase(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.ShopName == newShop.ShopName)) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + catch + { + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new MotorPlantDatabase(); + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + try + { + if (context.Shops.Any(x => (x.ShopName == model.ShopName && x.Id != model.Id))) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + shop.Update(model); + shop.UpdateDresses(context, model); + context.SaveChanges(); + return shop.GetViewModel; + } + catch + { + throw; + } + } + public bool SellEngines(IEngineModel model, int count) + { + if (model == null) + return false; + using var context = new MotorPlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + List lst = new List(); + foreach (var el in context.ShopEngines.Where(x => x.EngineId == model.Id)) + { + int dif = count; + if (el.Count < dif) + dif = el.Count; + el.Count -= dif; + count -= dif; + if (el.Count == 0) + { + lst.Add(el); + } + if (count == 0) + break; + } + if (count > 0) + { + transaction.Rollback(); + return false; + } + foreach (var el in lst) + { + context.ShopEngines.Remove(el); + } + context.SaveChanges(); + transaction.Commit(); + return true; + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.Designer.cs similarity index 68% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.Designer.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.Designer.cs index 575ec39..8b2c5f9 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.Designer.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.Designer.cs @@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace MotorPlantDatabaseImplement.Migrations { [DbContext(typeof(MotorPlantDatabase))] - [Migration("20240403225731_InitMigration")] - partial class InitMigration + [Migration("20240411122818_InitHardMigration")] + partial class InitHardMigration { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -124,6 +124,59 @@ namespace MotorPlantDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateOpen") + .HasColumnType("timestamp without time zone"); + + b.Property("MaxCount") + .HasColumnType("integer"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("EngineId") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("EngineId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopEngines"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => { b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component") @@ -154,6 +207,25 @@ namespace MotorPlantDatabaseImplement.Migrations b.Navigation("Engine"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b => + { + b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") + .WithMany() + .HasForeignKey("EngineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MotorPlantDatabaseImplement.Models.Shop", "Shop") + .WithMany("Engines") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Engine"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b => { b.Navigation("EngineComponents"); @@ -165,6 +237,11 @@ namespace MotorPlantDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b => + { + b.Navigation("Engines"); + }); #pragma warning restore 612, 618 } } diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.cs similarity index 66% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.cs index 53bde97..46dc8e1 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.cs @@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace MotorPlantDatabaseImplement.Migrations { /// - public partial class InitMigration : Migration + public partial class InitHardMigration : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -40,6 +40,22 @@ namespace MotorPlantDatabaseImplement.Migrations table.PrimaryKey("PK_Engines", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopName = table.Column(type: "text", nullable: false), + Adress = table.Column(type: "text", nullable: false), + DateOpen = table.Column(type: "timestamp without time zone", nullable: false), + MaxCount = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "EngineComponents", columns: table => new @@ -91,6 +107,33 @@ namespace MotorPlantDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopEngines", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + EngineId = table.Column(type: "integer", nullable: false), + ShopId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopEngines", x => x.Id); + table.ForeignKey( + name: "FK_ShopEngines_Engines_EngineId", + column: x => x.EngineId, + principalTable: "Engines", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopEngines_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_EngineComponents_ComponentId", table: "EngineComponents", @@ -105,6 +148,16 @@ namespace MotorPlantDatabaseImplement.Migrations name: "IX_Orders_EngineId", table: "Orders", column: "EngineId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopEngines_EngineId", + table: "ShopEngines", + column: "EngineId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopEngines_ShopId", + table: "ShopEngines", + column: "ShopId"); } /// @@ -116,11 +169,17 @@ namespace MotorPlantDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Orders"); + migrationBuilder.DropTable( + name: "ShopEngines"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Engines"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs index 8e0807e..51fdedd 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace MotorPlantDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateOpen") + .HasColumnType("timestamp without time zone"); + + b.Property("MaxCount") + .HasColumnType("integer"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("EngineId") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("EngineId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopEngines"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => { b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component") @@ -151,6 +204,25 @@ namespace MotorPlantDatabaseImplement.Migrations b.Navigation("Engine"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b => + { + b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") + .WithMany() + .HasForeignKey("EngineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MotorPlantDatabaseImplement.Models.Shop", "Shop") + .WithMany("Engines") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Engine"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b => { b.Navigation("EngineComponents"); @@ -162,6 +234,11 @@ namespace MotorPlantDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b => + { + b.Navigation("Engines"); + }); #pragma warning restore 612, 618 } } diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs index 00cb15b..91450c6 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs @@ -43,7 +43,6 @@ namespace MotorPlantDatabaseImplement.Models Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, - DateImplement = model.DateImplement }; } @@ -66,7 +65,6 @@ namespace MotorPlantDatabaseImplement.Models Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - EngineName = Engine.EngineName }; } } \ No newline at end of file diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/Shop.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..1aa014d --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Shop.cs @@ -0,0 +1,105 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + [Required] + public string ShopName { get; private set; } + [Required] + public string Adress { get; private set; } + [Required] + public DateTime DateOpen { get; private set; } + [Required] + public int MaxCount { get; private set; } + private Dictionary? _shopEngines = null; + [NotMapped] + public Dictionary? ShopEngines + { + get + { + if (_shopEngines == null) + { + _shopEngines = Engines.ToDictionary(x => x.EngineId, x => (x.Engine as IEngineModel, x.Count)); + } + return _shopEngines; + } + } + [ForeignKey("ShopId")] + public virtual List Engines { get; set; } = new(); + public static Shop? Create(MotorPlantDatabase context, ShopBindingModel model) + { + if (model == null) + return null; + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Adress = model.Adress, + DateOpen = model.DateOpen, + MaxCount = model.MaxCount, + Engines = model.ShopEngines.Select(x => new ShopEngine + { + Engine = context.Engines.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + return; + ShopName = model.ShopName; + Adress = model.Adress; + DateOpen = model.DateOpen; + MaxCount = model.MaxCount; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + DateOpen = DateOpen, + MaxCount = MaxCount, + ShopEngines = ShopEngines + }; + + public void UpdateDresses(MotorPlantDatabase context, ShopBindingModel model) + { + var shopEngines = context.ShopEngines.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopEngines != null && shopEngines.Count > 0) + { + context.ShopEngines.RemoveRange(shopEngines.Where(rec => !model.ShopEngines.ContainsKey(rec.EngineId))); + context.SaveChanges(); + foreach (var uEngine in shopEngines) + { + uEngine.Count = model.ShopEngines[uEngine.EngineId].Item2; + model.ShopEngines.Remove(uEngine.EngineId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var pc in model.ShopEngines) + { + context.ShopEngines.Add(new ShopEngine + { + Shop = shop, + Engine = context.Engines.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _shopEngines = null; + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/ShopEngine.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/ShopEngine.cs new file mode 100644 index 0000000..c6ed0c0 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/ShopEngine.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantDatabaseImplement.Models +{ + public class ShopEngine + { + public int Id { get; set; } + [Required] + public int EngineId { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Engine Engine { get; set; } = new(); + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabase.cs b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabase.cs index 26ecf49..73a1df5 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabase.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabase.cs @@ -9,7 +9,7 @@ namespace MotorPlantDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=MotorPlant_db;Username=postgres;Password=admin"); + optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=MotorPlantHard_db;Username=postgres;Password=admin"); } base.OnConfiguring(optionsBuilder); AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); @@ -19,5 +19,7 @@ namespace MotorPlantDatabaseImplement public virtual DbSet Engines { get; set; } public virtual DbSet EngineComponents { get; set; } public virtual DbSet Orders { get; set; } - } + public virtual DbSet Shops { get; set; } + public virtual DbSet ShopEngines { get; set; } + } } \ No newline at end of file diff --git a/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj index 6fa33d6..fa3b43b 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj +++ b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj @@ -21,8 +21,4 @@ - - - - diff --git a/MotorPlant/MotorPlantView/FormCreateOrder.cs b/MotorPlant/MotorPlantView/FormCreateOrder.cs index d4fd6e3..63df012 100644 --- a/MotorPlant/MotorPlantView/FormCreateOrder.cs +++ b/MotorPlant/MotorPlantView/FormCreateOrder.cs @@ -84,7 +84,7 @@ namespace MotorPlantView.Forms var operationResult = _logicO.CreateOrder(new OrderBindingModel { EngineId = Convert.ToInt32(comboBoxEngine.SelectedValue), - Count = Convert.ToInt32(textBoxCount.Text), + Count = Convert.ToInt32(textBoxCount.Text), Sum = Convert.ToDouble(textBoxSum.Text) }); if (!operationResult) @@ -97,7 +97,8 @@ namespace MotorPlantView.Forms } catch (Exception ex) { - _logger.LogError(ex, "Ошибка создания заказа"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError(ex, "Ошибка создания заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } diff --git a/MotorPlant/MotorPlantView/FormMain.cs b/MotorPlant/MotorPlantView/FormMain.cs index e3468ed..01227b8 100644 --- a/MotorPlant/MotorPlantView/FormMain.cs +++ b/MotorPlant/MotorPlantView/FormMain.cs @@ -30,7 +30,7 @@ namespace MotorPlantView.Forms { dataGridView.DataSource = list; dataGridView.Columns["EngineId"].Visible = false; - dataGridView.Columns["EngineName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["EngineName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; } _logger.LogInformation("Загрузка заказов"); -- 2.25.1 From 1cb89e24d726a3dce461a29a931a74412de9a57d Mon Sep 17 00:00:00 2001 From: Salikh Date: Thu, 11 Apr 2024 18:22:36 +0400 Subject: [PATCH 7/8] second commit --- .../Implements/OrderStorage.cs | 123 +++++++++--------- ...r.cs => 20240411140319_NewMig.Designer.cs} | 8 +- ...dMigration.cs => 20240411140319_NewMig.cs} | 2 +- .../MotorPlantDatabaseModelSnapshot.cs | 4 +- .../Models/Order.cs | 2 - 5 files changed, 69 insertions(+), 70 deletions(-) rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240411122818_InitHardMigration.Designer.cs => 20240411140319_NewMig.Designer.cs} (98%) rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240411122818_InitHardMigration.cs => 20240411140319_NewMig.cs} (99%) diff --git a/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs index 08c17f1..ae20ebb 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs @@ -13,8 +13,10 @@ namespace MotorPlantDatabaseImplement.Implements public List GetFullList() { using var context = new MotorPlantDatabase(); - return context.Orders.Include(x => x.Engine).Select(x => x.GetViewModel).ToList(); - } + return context.Orders + .Select(x => AccessEngineStorage(x.GetViewModel)) + .ToList(); + } public List GetFilteredList(OrderSearchModel model) { if (!model.Id.HasValue) @@ -22,67 +24,70 @@ namespace MotorPlantDatabaseImplement.Implements 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(); - } + return context.Orders.Where(x => x.Id == model.Id).Select(x => AccessEngineStorage(x.GetViewModel)).ToList(); + } public OrderViewModel? GetElement(OrderSearchModel model) { - if (!model.Id.HasValue) - { - return null; - } - using var context = new MotorPlantDatabase(); - return context.Orders.Include(x => x.Engine) - .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; - } + if (!model.Id.HasValue) + { + return null; + } + using var context = new MotorPlantDatabase(); + return AccessEngineStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + } public OrderViewModel? Insert(OrderBindingModel model) { - var newOrder = Order.Create(model); - if (newOrder == null) - { - return null; - } - using var context = new MotorPlantDatabase(); - context.Orders.Add(newOrder); - context.SaveChanges(); - return context.Orders - .Include(x => x.Engine) - .FirstOrDefault(x => x.Id == newOrder.Id) - ?.GetViewModel; - } + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new MotorPlantDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessEngineStorage(newOrder.GetViewModel); + } public OrderViewModel? Update(OrderBindingModel model) { - using var context = new MotorPlantDatabase(); - 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.Engine) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } - public OrderViewModel? Delete(OrderBindingModel model) - { - using var context = new MotorPlantDatabase(); - 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 deletedElement; - } - return null; - } - } + using var context = new MotorPlantDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessEngineStorage(order.GetViewModel); + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new MotorPlantDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessEngineStorage(element.GetViewModel); + } + return null; + } + + public static OrderViewModel AccessEngineStorage(OrderViewModel model) + { + if (model == null) + return null; + using var context = new MotorPlantDatabase(); + foreach (var Engine in context.Engines) + { + if (Engine.Id == model.EngineId) + { + model.EngineName = Engine.EngineName; + break; + } + } + return model; + } + } } diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.Designer.cs similarity index 98% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.Designer.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.Designer.cs index 8b2c5f9..1151db6 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.Designer.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.Designer.cs @@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace MotorPlantDatabaseImplement.Migrations { [DbContext(typeof(MotorPlantDatabase))] - [Migration("20240411122818_InitHardMigration")] - partial class InitHardMigration + [Migration("20240411140319_NewMig")] + partial class NewMig { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -198,13 +198,11 @@ namespace MotorPlantDatabaseImplement.Migrations modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => { - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") + b.HasOne("MotorPlantDatabaseImplement.Models.Engine", null) .WithMany("Orders") .HasForeignKey("EngineId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - - b.Navigation("Engine"); }); modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b => diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.cs similarity index 99% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.cs index 46dc8e1..5a17410 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.cs @@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace MotorPlantDatabaseImplement.Migrations { /// - public partial class InitHardMigration : Migration + public partial class NewMig : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs index 51fdedd..e59cde0 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs @@ -195,13 +195,11 @@ namespace MotorPlantDatabaseImplement.Migrations modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => { - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") + b.HasOne("MotorPlantDatabaseImplement.Models.Engine", null) .WithMany("Orders") .HasForeignKey("EngineId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - - b.Navigation("Engine"); }); modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b => diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs index 91450c6..7355218 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs @@ -27,8 +27,6 @@ namespace MotorPlantDatabaseImplement.Models public DateTime? DateImplement { get; private set; } - public virtual Engine Engine { get; set; } - public static Order? Create(OrderBindingModel? model) { if (model == null) -- 2.25.1 From 6456ec0dbf39160e3f512bd26137a4ba6469b7bd Mon Sep 17 00:00:00 2001 From: Salikh Date: Fri, 3 May 2024 15:44:28 +0400 Subject: [PATCH 8/8] commit --- .../Implements/ShopStorage .cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/MotorPlant/MotorPlantFileImplement/Implements/ShopStorage .cs b/MotorPlant/MotorPlantFileImplement/Implements/ShopStorage .cs index aefb9ca..29ae922 100644 --- a/MotorPlant/MotorPlantFileImplement/Implements/ShopStorage .cs +++ b/MotorPlant/MotorPlantFileImplement/Implements/ShopStorage .cs @@ -74,16 +74,13 @@ namespace MotorPlantFileImplement.Implements } return null; } - public bool CheckAvailability(int engineId, int count) - { - int store = _source.Shops.Select(x => x.ShopEngines.Select(y => (y.Value.Item1.Id == engineId ? y.Value.Item2 : 0)).Sum()).Sum(); - return store >= count; - } + public bool SellEngines(IEngineModel model, int count) { - var dres = _source.Engines.FirstOrDefault(x => x.Id == model.Id); + var eng = _source.Engines.FirstOrDefault(x => x.Id == model.Id); + int store = _source.Shops.SelectMany(x => x.ShopEngines).Sum(y => y.Key == model.Id ? y.Value.Item2 : 0); - if (dres == null || !CheckAvailability(model.Id, count)) + if (eng == null || store < count) { return false; } @@ -92,7 +89,7 @@ namespace MotorPlantFileImplement.Implements { var shop = _source.Shops[i]; var engines = shop.ShopEngines; - foreach (var engine in engines.Where(x => x.Value.Item1.Id == dres.Id)) + foreach (var engine in engines.Where(x => x.Value.Item1.Id == eng.Id)) { var selling = Math.Min(engine.Value.Item2, count); engines[engine.Value.Item1.Id] = (engine.Value.Item1, engine.Value.Item2 - selling); -- 2.25.1