From 4609383c15dc7d6438332582f717d3eb2ea4d518 Mon Sep 17 00:00:00 2001 From: Sasylika4598 Date: Sat, 11 May 2024 11:57:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SecuritySystem/SecuritySystem.sln | 8 +- .../SecuritySystemBusinessLogic.csproj | 10 + .../SecuritySystemContracts.csproj | 13 ++ .../SecuritySystemDataModels.csproj | 13 ++ .../Implements/ComponentStorage.cs | 85 +++++++++ .../Implements/OrderStorage.cs | 80 ++++++++ .../Implements/SecureStorage.cs | 106 +++++++++++ .../20240507141648_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240507141648_InitialCreate.cs | 125 +++++++++++++ .../SecuritySystemDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 58 ++++++ .../Models/Order.cs | 67 +++++++ .../Models/Secure.cs | 97 ++++++++++ .../Models/SecureComponent.cs | 18 ++ .../SecuritySystemDatabase.cs | 28 +++ .../SecuritySystemDatabaseImplement.csproj | 27 +++ .../SecuritySystemFileImplement.csproj | 13 ++ .../SecuritySystemListImplement.csproj | 13 ++ SecuritySystem/SecuritySystemView/Program.cs | 4 +- .../SecuritySystemView.csproj | 23 ++- 20 files changed, 1116 insertions(+), 11 deletions(-) create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Implements/OrderStorage.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Implements/SecureStorage.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Migrations/20240507141648_InitialCreate.Designer.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Migrations/20240507141648_InitialCreate.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Migrations/SecuritySystemDatabaseModelSnapshot.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Models/Component.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Models/Order.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Models/Secure.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/Models/SecureComponent.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabase.cs create mode 100644 SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabaseImplement.csproj diff --git a/SecuritySystem/SecuritySystem.sln b/SecuritySystem/SecuritySystem.sln index c9e87cc..93d5121 100644 --- a/SecuritySystem/SecuritySystem.sln +++ b/SecuritySystem/SecuritySystem.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecuritySystemBusinessLogic EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecuritySystemListImplement", "SecuritySystemListImplement\SecuritySystemListImplement.csproj", "{6A104362-3398-4D4E-A3D0-2F4B32858569}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemFileImplement", "SecuritySystemFileImplement\SecuritySystemFileImplement.csproj", "{0ACD169C-97A8-452E-A17A-19E2464F5286}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SecuritySystemFileImplement", "SecuritySystemFileImplement\SecuritySystemFileImplement.csproj", "{0ACD169C-97A8-452E-A17A-19E2464F5286}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemDatabaseImplement", "SecuritySystemDatabaseImplement\SecuritySystemDatabaseImplement.csproj", "{4AE7461C-39CC-463B-B5A1-A47ED3B9E925}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {0ACD169C-97A8-452E-A17A-19E2464F5286}.Debug|Any CPU.Build.0 = Debug|Any CPU {0ACD169C-97A8-452E-A17A-19E2464F5286}.Release|Any CPU.ActiveCfg = Release|Any CPU {0ACD169C-97A8-452E-A17A-19E2464F5286}.Release|Any CPU.Build.0 = Release|Any CPU + {4AE7461C-39CC-463B-B5A1-A47ED3B9E925}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4AE7461C-39CC-463B-B5A1-A47ED3B9E925}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4AE7461C-39CC-463B-B5A1-A47ED3B9E925}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4AE7461C-39CC-463B-B5A1-A47ED3B9E925}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SecuritySystem/SecuritySystemBusinessLogic/SecuritySystemBusinessLogic.csproj b/SecuritySystem/SecuritySystemBusinessLogic/SecuritySystemBusinessLogic.csproj index d936e08..0cdc6b8 100644 --- a/SecuritySystem/SecuritySystemBusinessLogic/SecuritySystemBusinessLogic.csproj +++ b/SecuritySystem/SecuritySystemBusinessLogic/SecuritySystemBusinessLogic.csproj @@ -7,6 +7,16 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SecuritySystem/SecuritySystemContracts/SecuritySystemContracts.csproj b/SecuritySystem/SecuritySystemContracts/SecuritySystemContracts.csproj index 755c027..413deaa 100644 --- a/SecuritySystem/SecuritySystemContracts/SecuritySystemContracts.csproj +++ b/SecuritySystem/SecuritySystemContracts/SecuritySystemContracts.csproj @@ -6,6 +6,19 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SecuritySystem/SecuritySystemDataModels/SecuritySystemDataModels.csproj b/SecuritySystem/SecuritySystemDataModels/SecuritySystemDataModels.csproj index 132c02c..b2efa94 100644 --- a/SecuritySystem/SecuritySystemDataModels/SecuritySystemDataModels.csproj +++ b/SecuritySystem/SecuritySystemDataModels/SecuritySystemDataModels.csproj @@ -6,4 +6,17 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ComponentStorage.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..38675b9 --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,85 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDatabaseImplement.Models; + +namespace SecuritySystemDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new SecuritySystemDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new SecuritySystemDatabase(); + 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 SecuritySystemDatabase(); + 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 SecuritySystemDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new SecuritySystemDatabase(); + 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 SecuritySystemDatabase(); + 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/SecuritySystem/SecuritySystemDatabaseImplement/Implements/OrderStorage.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..00f3380 --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,80 @@ +using Microsoft.EntityFrameworkCore; +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDatabaseImplement.Models; + +namespace SecuritySystemDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new SecuritySystemDatabase(); + return context.Orders.Include(x => x.Secure).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new SecuritySystemDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Include(x => x.Secure) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new SecuritySystemDatabase(); + return context.Orders.Include(x => x.Secure).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new SecuritySystemDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders.Include(x => x.Secure).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new SecuritySystemDatabase(); + 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.Secure).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new SecuritySystemDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Implements/SecureStorage.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/SecureStorage.cs new file mode 100644 index 0000000..fba3c62 --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/SecureStorage.cs @@ -0,0 +1,106 @@ +using Microsoft.EntityFrameworkCore; +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDatabaseImplement.Models; + +namespace SecuritySystemDatabaseImplement.Implements +{ + public class SecureStorage : ISecureStorage + { + public List GetFullList() + { + using var context = new SecuritySystemDatabase(); + return context.Secures + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(SecureSearchModel model) + { + if (string.IsNullOrEmpty(model.SecureName)) + { + return new(); + } + using var context = new SecuritySystemDatabase(); + return context.Secures + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.SecureName.Contains(model.SecureName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public SecureViewModel? GetElement(SecureSearchModel model) + { + if (string.IsNullOrEmpty(model.SecureName) && + !model.Id.HasValue) + { + return null; + } + using var context = new SecuritySystemDatabase(); + return context.Secures + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SecureName) && + x.SecureName == model.SecureName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public SecureViewModel? Insert(SecureBindingModel model) + { + using var context = new SecuritySystemDatabase(); + var newSecure = Secure.Create(context, model); + if (newSecure == null) + { + return null; + } + context.Secures.Add(newSecure); + context.SaveChanges(); + return newSecure.GetViewModel; + } + public SecureViewModel? Update(SecureBindingModel model) + { + using var context = new SecuritySystemDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var secure = context.Secures.FirstOrDefault(rec => + rec.Id == model.Id); + if (secure == null) + { + return null; + } + secure.Update(model); + context.SaveChanges(); + secure.UpdateComponents(context, model); + transaction.Commit(); + return secure.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public SecureViewModel? Delete(SecureBindingModel model) + { + using var context = new SecuritySystemDatabase(); + var element = context.Secures + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Secures.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } + +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Migrations/20240507141648_InitialCreate.Designer.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Migrations/20240507141648_InitialCreate.Designer.cs new file mode 100644 index 0000000..0f1310e --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Migrations/20240507141648_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SecuritySystemDatabaseImplement; + +#nullable disable + +namespace SecuritySystemDatabaseImplement.Migrations +{ + [DbContext(typeof(SecuritySystemDatabase))] + [Migration("20240507141648_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.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("SecuritySystemDatabaseImplement.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("SecureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("SecureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.Secure", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SecureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Secures"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.SecureComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("SecureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("SecureId"); + + b.ToTable("SecureComponents"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.Order", b => + { + b.HasOne("SecuritySystemDatabaseImplement.Models.Secure", "Secure") + .WithMany("Orders") + .HasForeignKey("SecureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Secure"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.SecureComponent", b => + { + b.HasOne("SecuritySystemDatabaseImplement.Models.Component", "Component") + .WithMany("SecureComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SecuritySystemDatabaseImplement.Models.Secure", "Secure") + .WithMany("Components") + .HasForeignKey("SecureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Secure"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.Component", b => + { + b.Navigation("SecureComponents"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.Secure", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Migrations/20240507141648_InitialCreate.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Migrations/20240507141648_InitialCreate.cs new file mode 100644 index 0000000..3d9d255 --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Migrations/20240507141648_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SecuritySystemDatabaseImplement.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: "Secures", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SecureName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Secures", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SecureId = 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: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Secures_SecureId", + column: x => x.SecureId, + principalTable: "Secures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "SecureComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SecureId = 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_SecureComponents", x => x.Id); + table.ForeignKey( + name: "FK_SecureComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SecureComponents_Secures_SecureId", + column: x => x.SecureId, + principalTable: "Secures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_SecureId", + table: "Orders", + column: "SecureId"); + + migrationBuilder.CreateIndex( + name: "IX_SecureComponents_ComponentId", + table: "SecureComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_SecureComponents_SecureId", + table: "SecureComponents", + column: "SecureId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "SecureComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Secures"); + } + } +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Migrations/SecuritySystemDatabaseModelSnapshot.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Migrations/SecuritySystemDatabaseModelSnapshot.cs new file mode 100644 index 0000000..c7e1217 --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Migrations/SecuritySystemDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SecuritySystemDatabaseImplement; + +#nullable disable + +namespace SecuritySystemDatabaseImplement.Migrations +{ + [DbContext(typeof(SecuritySystemDatabase))] + partial class SecuritySystemDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.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("SecuritySystemDatabaseImplement.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("SecureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("SecureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.Secure", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SecureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Secures"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.SecureComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("SecureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("SecureId"); + + b.ToTable("SecureComponents"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.Order", b => + { + b.HasOne("SecuritySystemDatabaseImplement.Models.Secure", "Secure") + .WithMany("Orders") + .HasForeignKey("SecureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Secure"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.SecureComponent", b => + { + b.HasOne("SecuritySystemDatabaseImplement.Models.Component", "Component") + .WithMany("SecureComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SecuritySystemDatabaseImplement.Models.Secure", "Secure") + .WithMany("Components") + .HasForeignKey("SecureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Secure"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.Component", b => + { + b.Navigation("SecureComponents"); + }); + + modelBuilder.Entity("SecuritySystemDatabaseImplement.Models.Secure", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Models/Component.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..040f84f --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Component.cs @@ -0,0 +1,58 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace SecuritySystemDatabaseImplement.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 SecureComponents { 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/SecuritySystem/SecuritySystemDatabaseImplement/Models/Order.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..326ce18 --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Order.cs @@ -0,0 +1,67 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDataModels.Enums; +using SecuritySystemDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace SecuritySystemDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int SecureId { get; private set; } + + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + public virtual Secure Secure { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + SecureId = model.SecureId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + SecureId = SecureId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + SecureName = Secure.SecureName + }; + } +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Models/Secure.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Secure.cs new file mode 100644 index 0000000..1a971c5 --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Secure.cs @@ -0,0 +1,97 @@ +using SecuritySystemDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.ViewModels; + +namespace SecuritySystemDatabaseImplement.Models +{ + public class Secure : ISecureModel + { + public int Id { get; set; } + [Required] + public string SecureName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _productComponents = + null; + [NotMapped] + public Dictionary SecureComponents + { + get + { + if (_productComponents == null) + { + _productComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _productComponents; + } + } + [ForeignKey("SecureId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("SecureId")] + public virtual List Orders { get; set; } = new(); + public static Secure Create(SecuritySystemDatabase context, SecureBindingModel model) + { + return new Secure() + { + Id = model.Id, + SecureName = model.SecureName, + Price = model.Price, + Components = model.SecureComponents.Select(x => new + SecureComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(SecureBindingModel model) + { + SecureName = model.SecureName; + Price = model.Price; + } + public SecureViewModel GetViewModel => new() + { + Id = Id, + SecureName = SecureName, + Price = Price, + SecureComponents = SecureComponents + }; + public void UpdateComponents(SecuritySystemDatabase context, + SecureBindingModel model) + { + var productComponents = context.SecureComponents.Where(rec => + rec.SecureId == model.Id).ToList(); + if (productComponents != null && productComponents.Count > 0) + { // удалили те, которых нет в модели + context.SecureComponents.RemoveRange(productComponents.Where(rec + => !model.SecureComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in productComponents) + { + updateComponent.Count = + model.SecureComponents[updateComponent.ComponentId].Item2; + model.SecureComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var product = context.Secures.First(x => x.Id == Id); + foreach (var pc in model.SecureComponents) + { + context.SecureComponents.Add(new SecureComponent + { + Secure = product, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _productComponents = null; + } + } + +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Models/SecureComponent.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Models/SecureComponent.cs new file mode 100644 index 0000000..048435a --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Models/SecureComponent.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace SecuritySystemDatabaseImplement.Models +{ + public class SecureComponent + { + public int Id { get; set; } + [Required] + public int SecureId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Secure Secure { get; set; } = new(); + } + +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabase.cs b/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabase.cs new file mode 100644 index 0000000..875e04e --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabase.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore; +using SecuritySystemDatabaseImplement.Models; +using System.ComponentModel; +using Component = SecuritySystemDatabaseImplement.Models.Component; + +namespace SecuritySystemDatabaseImplement +{ + public class SecuritySystemDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder + optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS01; Initial Catalog=SecuritySystemDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + + } + base.OnConfiguring(optionsBuilder); + } + + + public virtual DbSet Components { set; get; } + public virtual DbSet Secures { set; get; } + public virtual DbSet SecureComponents { set; get; } + public virtual DbSet Orders { set; get; } + } + +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabaseImplement.csproj b/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabaseImplement.csproj new file mode 100644 index 0000000..54624d1 --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabaseImplement.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj b/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj index 7037095..452b64d 100644 --- a/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj +++ b/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj @@ -6,6 +6,19 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SecuritySystem/SecuritySystemListImplement/SecuritySystemListImplement.csproj b/SecuritySystem/SecuritySystemListImplement/SecuritySystemListImplement.csproj index 7d1c090..54624d1 100644 --- a/SecuritySystem/SecuritySystemListImplement/SecuritySystemListImplement.csproj +++ b/SecuritySystem/SecuritySystemListImplement/SecuritySystemListImplement.csproj @@ -6,6 +6,19 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SecuritySystem/SecuritySystemView/Program.cs b/SecuritySystem/SecuritySystemView/Program.cs index 6757ecc..98126e7 100644 --- a/SecuritySystem/SecuritySystemView/Program.cs +++ b/SecuritySystem/SecuritySystemView/Program.cs @@ -4,7 +4,8 @@ using NLog.Extensions.Logging; using SecuritySystemBusinessLogic.BusinessLogics; using SecuritySystemContracts.BusinessLogicsContracts; using SecuritySystemContracts.StoragesContracts; -using SecuritySystemFileImplement.Implements; +using SecuritySystemDatabaseImplement.Implements; +using System.Configuration; namespace SecuritySystemView { @@ -28,6 +29,7 @@ namespace SecuritySystemView } private static void ConfigureServices(ServiceCollection services) { + services.AddLogging(option => { option.SetMinimumLevel(LogLevel.Information); diff --git a/SecuritySystem/SecuritySystemView/SecuritySystemView.csproj b/SecuritySystem/SecuritySystemView/SecuritySystemView.csproj index 3937b06..9234b48 100644 --- a/SecuritySystem/SecuritySystemView/SecuritySystemView.csproj +++ b/SecuritySystem/SecuritySystemView/SecuritySystemView.csproj @@ -1,36 +1,41 @@  - WinExe - net6.0-windows + net8.0-windows enable true enable - - Always - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + - - + - + - \ No newline at end of file