diff --git a/MotorPlant/MotorPlant.sln b/MotorPlant/MotorPlant.sln index b16b409..fcac901 100644 --- a/MotorPlant/MotorPlant.sln +++ b/MotorPlant/MotorPlant.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MotorPlantListImplement", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MotorPlantFileImplement", "MotorPlantFileImplement\MotorPlantFileImplement.csproj", "{7F4C8448-8678-4AA4-A922-BB10EFCEF9AF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MotorPlantDatabaseImplement", "MotorPlantDatabaseImplement\MotorPlantDatabaseImplement.csproj", "{635EFBA2-ECFB-45F0-B1BF-75D3DF9BD171}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {7F4C8448-8678-4AA4-A922-BB10EFCEF9AF}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F4C8448-8678-4AA4-A922-BB10EFCEF9AF}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F4C8448-8678-4AA4-A922-BB10EFCEF9AF}.Release|Any CPU.Build.0 = Release|Any CPU + {635EFBA2-ECFB-45F0-B1BF-75D3DF9BD171}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {635EFBA2-ECFB-45F0-B1BF-75D3DF9BD171}.Debug|Any CPU.Build.0 = Debug|Any CPU + {635EFBA2-ECFB-45F0-B1BF-75D3DF9BD171}.Release|Any CPU.ActiveCfg = Release|Any CPU + {635EFBA2-ECFB-45F0-B1BF-75D3DF9BD171}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MotorPlant/MotorPlantDatabaseImplement/Component.cs b/MotorPlant/MotorPlantDatabaseImplement/Component.cs new file mode 100644 index 0000000..2382ad7 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Component.cs @@ -0,0 +1,57 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +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/ComponentStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/ComponentStorage.cs new file mode 100644 index 0000000..96c8d28 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/ComponentStorage.cs @@ -0,0 +1,84 @@ +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) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/Engine.cs b/MotorPlant/MotorPlantDatabaseImplement/Engine.cs new file mode 100644 index 0000000..8a51aae --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Engine.cs @@ -0,0 +1,97 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +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(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.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/EngineComponent.cs b/MotorPlant/MotorPlantDatabaseImplement/EngineComponent.cs new file mode 100644 index 0000000..694a36b --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/EngineComponent.cs @@ -0,0 +1,17 @@ +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(); + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/EngineStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/EngineStorage.cs new file mode 100644 index 0000000..04eb2f8 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/EngineStorage.cs @@ -0,0 +1,104 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +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/Migrations/20240308215333_InitialCreate.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.Designer.cs new file mode 100644 index 0000000..9e32da0 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.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("20240308215333_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .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/20240308215333_InitialCreate.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.cs new file mode 100644 index 0000000..a857529 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MotorPlantDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Engines", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + EngineName = table.Column(type: "nvarchar(max)", 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"), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true), + EngineId = table.Column(type: "int", nullable: false) + }, + 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..7479b8f --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDataBaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MotorPlantDatabaseImplement; + +#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", "8.0.2") + .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/MotorPlantDataBase.cs b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDataBase.cs new file mode 100644 index 0000000..41ab4eb --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDataBase.cs @@ -0,0 +1,29 @@ +using MotorPlantDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantDatabaseImplement +{ + public class MotorPlantDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder +optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-ETBGTEE;Initial Catalog=MotorPlantDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Engines { set; get; } + public virtual DbSet EngineComponents { set; get; } + public virtual DbSet Orders { set; get; } + + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj new file mode 100644 index 0000000..d68950d --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net8.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/MotorPlant/MotorPlantDatabaseImplement/Order.cs b/MotorPlant/MotorPlantDatabaseImplement/Order.cs new file mode 100644 index 0000000..811f955 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Order.cs @@ -0,0 +1,64 @@ +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 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; } + public DateTime? DateImplement { get; private set; } + [Required] + public int EngineId { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + EngineId = model.EngineId, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + EngineId = EngineId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/OrderStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/OrderStorage.cs new file mode 100644 index 0000000..1f12ebe --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/OrderStorage.cs @@ -0,0 +1,94 @@ +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 + .Select(x => AccessEngineStorage(x.GetViewModel)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new MotorPlantDataBase(); + 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 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 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 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/MotorPlantFileImplement/OrderStorage.cs b/MotorPlant/MotorPlantFileImplement/OrderStorage.cs index 5fdd024..8aff0bf 100644 --- a/MotorPlant/MotorPlantFileImplement/OrderStorage.cs +++ b/MotorPlant/MotorPlantFileImplement/OrderStorage.cs @@ -71,14 +71,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/Order.cs b/MotorPlant/MotorPlantListImplement/Order.cs index f9bc63b..176124c 100644 --- a/MotorPlant/MotorPlantListImplement/Order.cs +++ b/MotorPlant/MotorPlantListImplement/Order.cs @@ -37,11 +37,7 @@ namespace MotorPlantListImplement.Models { return; } - EngineId = model.EngineId; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() diff --git a/MotorPlant/MotorPlantView/MotorPlantView.csproj b/MotorPlant/MotorPlantView/MotorPlantView.csproj index 0c7b07f..b63b22d 100644 --- a/MotorPlant/MotorPlantView/MotorPlantView.csproj +++ b/MotorPlant/MotorPlantView/MotorPlantView.csproj @@ -2,13 +2,17 @@ WinExe - net6.0-windows + net8.0-windows enable true enable + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -20,6 +24,7 @@ + diff --git a/MotorPlant/MotorPlantView/Program.cs b/MotorPlant/MotorPlantView/Program.cs index f808486..1d41c4e 100644 --- a/MotorPlant/MotorPlantView/Program.cs +++ b/MotorPlant/MotorPlantView/Program.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using MotorPlantBusinessLogic.BusinessLogics; -using MotorPlantFileImplement.Implements; +using MotorPlantDatabaseImplement.Implements; namespace MotorPlantView.Forms {