From d623bca9e2a2a619c5dcea789900c74de98453d3 Mon Sep 17 00:00:00 2001 From: "Nikolaeva_Y.A" Date: Tue, 11 Apr 2023 08:26:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SofrwareInstallationContracts.csproj | 6 + SoftwareInstallation/SoftwareInstallation.sln | 18 +- .../SoftwareInstallationBusinessLogic.csproj | 6 + .../Implements/ComponentStorage.cs | 92 +++++++++ .../Implements/OrderStorage.cs | 93 ++++++++++ .../Implements/PackegeStorage.cs | 106 +++++++++++ .../20230329095230_InitialCreate.Designer.cs | 175 ++++++++++++++++++ .../20230329095230_InitialCreate.cs | 126 +++++++++++++ ...ftwareInstallationDataBaseModelSnapshot.cs | 172 +++++++++++++++++ .../Models/Component.cs | 60 ++++++ .../Models/Order.cs | 82 ++++++++ .../Models/Package.cs | 101 ++++++++++ .../Models/PackageComponent.cs | 22 +++ .../SoftwareInstallationDataBase.cs | 21 +++ ...ftwareInstallationDataBaseImplement.csproj | 16 ++ .../SoftwareInstallationDataModels.csproj | 6 + .../SoftwareInstallationFileImplement.csproj | 9 + .../SoftwareInstallationListImplement.csproj | 6 + .../SoftwareInstallationView.csproj | 11 ++ 19 files changed, 1122 insertions(+), 6 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/ComponentStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/OrderStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/PackegeStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230329095230_InitialCreate.Designer.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230329095230_InitialCreate.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/SoftwareInstallationDataBaseModelSnapshot.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Component.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Order.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Package.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/PackageComponent.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBase.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBaseImplement.csproj diff --git a/SoftwareInstallation/SofrwareInstallationContracts/SofrwareInstallationContracts.csproj b/SoftwareInstallation/SofrwareInstallationContracts/SofrwareInstallationContracts.csproj index be58ab9..1e81202 100644 --- a/SoftwareInstallation/SofrwareInstallationContracts/SofrwareInstallationContracts.csproj +++ b/SoftwareInstallation/SofrwareInstallationContracts/SofrwareInstallationContracts.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SoftwareInstallation/SoftwareInstallation.sln b/SoftwareInstallation/SoftwareInstallation.sln index b9e0574..66400bf 100644 --- a/SoftwareInstallation/SoftwareInstallation.sln +++ b/SoftwareInstallation/SoftwareInstallation.sln @@ -3,17 +3,19 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.1.32210.238 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationDataModels", "SoftwareInstallationDataModels\SoftwareInstallationDataModels.csproj", "{E639D017-3A1A-4FE9-BBD0-2F66DB8324B1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationDataModels", "SoftwareInstallationDataModels\SoftwareInstallationDataModels.csproj", "{E639D017-3A1A-4FE9-BBD0-2F66DB8324B1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SofrwareInstallationContracts", "SofrwareInstallationContracts\SofrwareInstallationContracts.csproj", "{024C73F4-F2B4-4ACB-B1DB-665B5229A727}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SofrwareInstallationContracts", "SofrwareInstallationContracts\SofrwareInstallationContracts.csproj", "{024C73F4-F2B4-4ACB-B1DB-665B5229A727}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationBusinessLogic", "SoftwareInstallationBusinessLogic\SoftwareInstallationBusinessLogic.csproj", "{4DBC05D4-6FDB-4DB3-A012-81DB6B3A60BE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationBusinessLogic", "SoftwareInstallationBusinessLogic\SoftwareInstallationBusinessLogic.csproj", "{4DBC05D4-6FDB-4DB3-A012-81DB6B3A60BE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationListImplement", "SoftwareInstallationListImplement\SoftwareInstallationListImplement.csproj", "{28F7D064-2A50-43DA-A946-A5180EDB96B2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationListImplement", "SoftwareInstallationListImplement\SoftwareInstallationListImplement.csproj", "{28F7D064-2A50-43DA-A946-A5180EDB96B2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationView", "SoftwareInstallationView\SoftwareInstallationView.csproj", "{E91EF595-BDEC-4005-A9F2-BBF8ADB5AF7F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationView", "SoftwareInstallationView\SoftwareInstallationView.csproj", "{E91EF595-BDEC-4005-A9F2-BBF8ADB5AF7F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationFileImplement", "SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj", "{B9005716-5A66-4DF8-AE5F-B5A5D4C6DD71}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationFileImplement", "SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj", "{B9005716-5A66-4DF8-AE5F-B5A5D4C6DD71}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationDataBaseImplement", "SoftwareInstallationDataBaseImplement\SoftwareInstallationDataBaseImplement.csproj", "{5CA51689-5F83-432B-9081-89DC1350F681}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {B9005716-5A66-4DF8-AE5F-B5A5D4C6DD71}.Debug|Any CPU.Build.0 = Debug|Any CPU {B9005716-5A66-4DF8-AE5F-B5A5D4C6DD71}.Release|Any CPU.ActiveCfg = Release|Any CPU {B9005716-5A66-4DF8-AE5F-B5A5D4C6DD71}.Release|Any CPU.Build.0 = Release|Any CPU + {5CA51689-5F83-432B-9081-89DC1350F681}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CA51689-5F83-432B-9081-89DC1350F681}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CA51689-5F83-432B-9081-89DC1350F681}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CA51689-5F83-432B-9081-89DC1350F681}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/SoftwareInstallationBusinessLogic.csproj b/SoftwareInstallation/SoftwareInstallationBusinessLogic/SoftwareInstallationBusinessLogic.csproj index 9d13a0a..d958c84 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/SoftwareInstallationBusinessLogic.csproj +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/SoftwareInstallationBusinessLogic.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/ComponentStorage.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..86158cf --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,92 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.SearchModels; +using SofrwareInstallationContracts.StoragesContracts; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataBaseImplement.Models; + +namespace SoftwareInstallationDataBaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new SoftwareInstallationDataBase(); + + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + return null; + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + + using var context = new SoftwareInstallationDataBase(); + + return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + + using var context = new SoftwareInstallationDataBase(); + + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new SoftwareInstallationDataBase(); + + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + + if (newComponent == null) + { + return null; + } + + using var context = new SoftwareInstallationDataBase(); + + context.Components.Add(newComponent); + context.SaveChanges(); + + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new SoftwareInstallationDataBase(); + + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + + if (component == null) + { + return null; + } + + component.Update(model); + context.SaveChanges(); + + return component.GetViewModel; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..008bcc7 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,93 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.SearchModels; +using SofrwareInstallationContracts.StoragesContracts; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataBaseImplement.Models; + +namespace SoftwareInstallationDataBaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new SoftwareInstallationDataBase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new SoftwareInstallationDataBase(); + + return context.Orders.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 SoftwareInstallationDataBase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new SoftwareInstallationDataBase(); + + return context.Orders.Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + using var context = new SoftwareInstallationDataBase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new SoftwareInstallationDataBase(); + + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return order.GetViewModel; + } + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/PackegeStorage.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/PackegeStorage.cs new file mode 100644 index 0000000..ea577b9 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/PackegeStorage.cs @@ -0,0 +1,106 @@ +using Microsoft.EntityFrameworkCore; +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.SearchModels; +using SofrwareInstallationContracts.StoragesContracts; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataBaseImplement.Models; + +namespace SoftwareInstallationDataBaseImplement.Implements +{ + public class PackageStorage : IPackageStorage + { + public PackageViewModel? Delete(PackageBindingModel model) + { + using var context = new SoftwareInstallationDataBase(); + + var element = context.Packages.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Packages.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + return null; + } + + public PackageViewModel? GetElement(PackageSearchModel model) + { + if (string.IsNullOrEmpty(model.PackageName) && !model.Id.HasValue) + { + return null; + } + + using var context = new SoftwareInstallationDataBase(); + + return context.Packages.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.PackageName) && x.PackageName == model.PackageName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(PackageSearchModel model) + { + if (string.IsNullOrEmpty(model.PackageName)) + { + return new(); + } + + using var context = new SoftwareInstallationDataBase(); + + return context.Packages.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.PackageName.Contains(model.PackageName)).ToList().Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new SoftwareInstallationDataBase(); + + return context.Packages.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + } + + public PackageViewModel? Insert(PackageBindingModel model) + { + using var context = new SoftwareInstallationDataBase(); + + var newPackage = Package.Create(context, model); + + if (newPackage == null) + { + return null; + } + + context.Packages.Add(newPackage); + context.SaveChanges(); + + return newPackage.GetViewModel; + } + + public PackageViewModel? Update(PackageBindingModel model) + { + using var context = new SoftwareInstallationDataBase(); + + using var transaction = context.Database.BeginTransaction(); + + try + { + var package = context.Packages.FirstOrDefault(rec => rec.Id == model.Id); + + if (package == null) + { + return null; + } + + package.Update(model); + context.SaveChanges(); + package.UpdateComponents(context, model); + transaction.Commit(); + + return package.GetViewModel; + } + catch + { + transaction.Rollback(); + + throw; + } + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230329095230_InitialCreate.Designer.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230329095230_InitialCreate.Designer.cs new file mode 100644 index 0000000..c44ca0b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230329095230_InitialCreate.Designer.cs @@ -0,0 +1,175 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SoftwareInstallationDataBaseImplement; + +#nullable disable + +namespace SoftwareInstallationDataBaseImplement.Migrations +{ + [DbContext(typeof(SoftwareInstallationDataBase))] + [Migration("20230329095230_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.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("SoftwareInstallationDataBaseImplement.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("PackageId") + .HasColumnType("int"); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PackageId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Package", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Packages"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.PackageComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PackageId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PackageId"); + + b.ToTable("PackageComponents"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Order", b => + { + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Package", "Package") + .WithMany("Orders") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Package"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.PackageComponent", b => + { + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Component", "Component") + .WithMany("PackageComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Package", "Package") + .WithMany("Components") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Package"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Component", b => + { + b.Navigation("PackageComponents"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Package", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230329095230_InitialCreate.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230329095230_InitialCreate.cs new file mode 100644 index 0000000..7f1d78b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230329095230_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SoftwareInstallationDataBaseImplement.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: "Packages", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PackageName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Packages", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PackageId = table.Column(type: "int", nullable: false), + PackageName = table.Column(type: "nvarchar(max)", 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_Packages_PackageId", + column: x => x.PackageId, + principalTable: "Packages", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PackageComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PackageId = 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_PackageComponents", x => x.Id); + table.ForeignKey( + name: "FK_PackageComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PackageComponents_Packages_PackageId", + column: x => x.PackageId, + principalTable: "Packages", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PackageId", + table: "Orders", + column: "PackageId"); + + migrationBuilder.CreateIndex( + name: "IX_PackageComponents_ComponentId", + table: "PackageComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PackageComponents_PackageId", + table: "PackageComponents", + column: "PackageId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PackageComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Packages"); + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/SoftwareInstallationDataBaseModelSnapshot.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/SoftwareInstallationDataBaseModelSnapshot.cs new file mode 100644 index 0000000..1ffa3ea --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/SoftwareInstallationDataBaseModelSnapshot.cs @@ -0,0 +1,172 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SoftwareInstallationDataBaseImplement; + +#nullable disable + +namespace SoftwareInstallationDataBaseImplement.Migrations +{ + [DbContext(typeof(SoftwareInstallationDataBase))] + partial class SoftwareInstallationDataBaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.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("SoftwareInstallationDataBaseImplement.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("PackageId") + .HasColumnType("int"); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PackageId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Package", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Packages"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.PackageComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PackageId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PackageId"); + + b.ToTable("PackageComponents"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Order", b => + { + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Package", "Package") + .WithMany("Orders") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Package"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.PackageComponent", b => + { + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Component", "Component") + .WithMany("PackageComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Package", "Package") + .WithMany("Components") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Package"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Component", b => + { + b.Navigation("PackageComponents"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Package", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Component.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Component.cs new file mode 100644 index 0000000..b33c9c0 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Component.cs @@ -0,0 +1,60 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace SoftwareInstallationDataBaseImplement.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 PackageComponents { 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 + }; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Order.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Order.cs new file mode 100644 index 0000000..a9c7375 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Order.cs @@ -0,0 +1,82 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Enums; +using SoftwareInstallationDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace SoftwareInstallationDataBaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int PackageId { get; private set; } + + public string PackageName { get; private set; } = string.Empty; + + [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 Package Package { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Order() + { + Id = model.Id, + PackageId = model.PackageId, + PackageName = model.PackageName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + + PackageId = model.PackageId; + PackageName = model.PackageName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + PackageId = PackageId, + PackageName = PackageName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} + diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Package.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Package.cs new file mode 100644 index 0000000..a8cf74e --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Package.cs @@ -0,0 +1,101 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace SoftwareInstallationDataBaseImplement.Models +{ + public class Package : IPackageModel + { + public int Id { get; set; } + + [Required] + public string PackageName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _packageComponents = null; + + [NotMapped] + public Dictionary PackageComponents + { + get + { + if (_packageComponents == null) + { + _packageComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _packageComponents; + } + } + + [ForeignKey("PackageId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("PackageId")] + public virtual List Orders { get; set; } = new(); + + public static Package Create(SoftwareInstallationDataBase context, PackageBindingModel model) + { + return new Package() + { + Id = model.Id, + PackageName = model.PackageName, + Price = model.Price, + Components = model.PackageComponents.Select(x => new PackageComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(PackageBindingModel model) + { + PackageName = model.PackageName; + Price = model.Price; + } + + public PackageViewModel GetViewModel => new() + { + Id = Id, + PackageName = PackageName, + Price = Price, + PackageComponents = PackageComponents + }; + + public void UpdateComponents(SoftwareInstallationDataBase context, PackageBindingModel model) + { + var packageComponents = context.PackageComponents.Where(rec => rec.PackageId == model.Id).ToList(); + + if (packageComponents != null && packageComponents.Count > 0) + { // удалили те, которых нет в модели + context.PackageComponents.RemoveRange(packageComponents.Where(rec => !model.PackageComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in packageComponents) + { + updateComponent.Count = model.PackageComponents[updateComponent.ComponentId].Item2; + model.PackageComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + + var package = context.Packages.First(x => x.Id == Id); + + foreach (var pc in model.PackageComponents) + { + context.PackageComponents.Add(new PackageComponent + { + Package = package, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _packageComponents = null; + } + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/PackageComponent.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/PackageComponent.cs new file mode 100644 index 0000000..f0ac997 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/PackageComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace SoftwareInstallationDataBaseImplement.Models +{ + public class PackageComponent + { + public int Id { get; set; } + + [Required] + public int PackageId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + public virtual Package Package { get; set; } = new(); + } +} + diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBase.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBase.cs new file mode 100644 index 0000000..257d209 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBase.cs @@ -0,0 +1,21 @@ +using Microsoft.EntityFrameworkCore; +using SoftwareInstallationDataBaseImplement.Models; + +namespace SoftwareInstallationDataBaseImplement +{ + public class SoftwareInstallationDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=IS-431-12\SQLEXPRESS;Initial Catalog=SoftwareInstallationDataBaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Packages { set; get; } + public virtual DbSet PackageComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBaseImplement.csproj b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBaseImplement.csproj new file mode 100644 index 0000000..1740d64 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBaseImplement.csproj @@ -0,0 +1,16 @@ + + + + Library + net6.0-windows + enable + true + enable + + + + + + + + \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationDataModels/SoftwareInstallationDataModels.csproj b/SoftwareInstallation/SoftwareInstallationDataModels/SoftwareInstallationDataModels.csproj index b62af12..ed4a88f 100644 --- a/SoftwareInstallation/SoftwareInstallationDataModels/SoftwareInstallationDataModels.csproj +++ b/SoftwareInstallation/SoftwareInstallationDataModels/SoftwareInstallationDataModels.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/SoftwareInstallationFileImplement.csproj b/SoftwareInstallation/SoftwareInstallationFileImplement/SoftwareInstallationFileImplement.csproj index c53b4c7..23403c0 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/SoftwareInstallationFileImplement.csproj +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/SoftwareInstallationFileImplement.csproj @@ -6,6 +6,15 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/SoftwareInstallationListImplement.csproj b/SoftwareInstallation/SoftwareInstallationListImplement/SoftwareInstallationListImplement.csproj index a118b74..a166580 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/SoftwareInstallationListImplement.csproj +++ b/SoftwareInstallation/SoftwareInstallationListImplement/SoftwareInstallationListImplement.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SoftwareInstallation/SoftwareInstallationView/SoftwareInstallationView.csproj b/SoftwareInstallation/SoftwareInstallationView/SoftwareInstallationView.csproj index 9646c3b..d22d433 100644 --- a/SoftwareInstallation/SoftwareInstallationView/SoftwareInstallationView.csproj +++ b/SoftwareInstallation/SoftwareInstallationView/SoftwareInstallationView.csproj @@ -9,6 +9,16 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -38,6 +48,7 @@ + -- 2.25.1