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..fde0c80 --- /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 newEngine = Engine.Create(context, model); + if (newEngine == null) + { + return null; + } + context.Engines.Add(newEngine); + context.SaveChanges(); + return newEngine.GetViewModel; + } + public EngineViewModel? Update(EngineBindingModel model) + { + using var context = new MotorPlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var engine = context.Engines.FirstOrDefault(rec => + rec.Id == model.Id); + if (engine == null) + { + return null; + } + engine.Update(model); + context.SaveChanges(); + engine.UpdateComponents(context, model); + transaction.Commit(); + return engine.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..08c17f1 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,88 @@ +using Microsoft.EntityFrameworkCore; +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.Include(x => x.Engine).Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + 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) + { + 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; + } + 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; + } + 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; + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.Designer.cs new file mode 100644 index 0000000..575ec39 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_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("20240403225731_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/20240403225731_InitMigration.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_InitMigration.cs new file mode 100644 index 0000000..53bde97 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240403225731_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 + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..223ccc7 --- /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 EngineComponents { 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..44db39e --- /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("EngineId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("EngineId")] + 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..00cb15b --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs @@ -0,0 +1,72 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Enums; +using MotorPlantDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace MotorPlantDatabaseImplement.Models +{ + 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 virtual Engine Engine { get; 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, + DateImplement = model.DateImplement + }; + } + + 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, + EngineName = Engine.EngineName + }; + } +} \ No newline at end of file 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 088c50c..1dd2729 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;