diff --git a/SoftwareInstallation/Program.cs b/SoftwareInstallation/Program.cs index c75fcba..e73efc2 100644 --- a/SoftwareInstallation/Program.cs +++ b/SoftwareInstallation/Program.cs @@ -2,7 +2,7 @@ using SoftwareInstallation.Forms; using SoftwareInstallationContracts.BusinessLogicsContracts; using SoftwareInstallationBusinessLogic.BusinessLogic; using SoftwareInstallationContracts.StoragesContracts; -using SoftwareInstallationFileImplement.Implements; +using SoftwareInstallationDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/SoftwareInstallation/SoftwareInstallation.sln b/SoftwareInstallation/SoftwareInstallation.sln index 4878f75..1598da6 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.8.34525.116 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationView", "SoftwareInstallationView.csproj", "{A6865B23-8593-40C9-B39E-E4837E348722}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationView", "SoftwareInstallationView.csproj", "{A6865B23-8593-40C9-B39E-E4837E348722}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationBusinessLogic", "..\SoftwareInstallationBusinessLogic\SoftwareInstallationBusinessLogic.csproj", "{2A8D4417-01AF-46D5-A1C1-B569BD9C4B24}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationBusinessLogic", "..\SoftwareInstallationBusinessLogic\SoftwareInstallationBusinessLogic.csproj", "{2A8D4417-01AF-46D5-A1C1-B569BD9C4B24}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationContracts", "..\SoftwareInstallationContracts\SoftwareInstallationContracts.csproj", "{35798E8A-1293-4503-870F-CD7DF457D75A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationContracts", "..\SoftwareInstallationContracts\SoftwareInstallationContracts.csproj", "{35798E8A-1293-4503-870F-CD7DF457D75A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationDataModels", "..\SoftwareInstallationDataModels\SoftwareInstallationDataModels.csproj", "{3BADBB66-3146-4A9D-A1E0-7B4B64B9ED4A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationDataModels", "..\SoftwareInstallationDataModels\SoftwareInstallationDataModels.csproj", "{3BADBB66-3146-4A9D-A1E0-7B4B64B9ED4A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationListImplement", "..\SoftwareInstallationListImplement\SoftwareInstallationListImplement.csproj", "{9A82C09E-9721-4067-A449-D7984CBAB121}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationListImplement", "..\SoftwareInstallationListImplement\SoftwareInstallationListImplement.csproj", "{9A82C09E-9721-4067-A449-D7984CBAB121}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationFileImplement", "..\SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj", "{11B51E5D-2FA6-4DC4-845B-8F051EC833D6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationFileImplement", "..\SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj", "{11B51E5D-2FA6-4DC4-845B-8F051EC833D6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationDatabaseImplement", "..\SoftwareInstallationDatabaseImplement\SoftwareInstallationDatabaseImplement.csproj", "{B95F9224-1A4D-4EA2-8228-57F5F5E96D59}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {11B51E5D-2FA6-4DC4-845B-8F051EC833D6}.Debug|Any CPU.Build.0 = Debug|Any CPU {11B51E5D-2FA6-4DC4-845B-8F051EC833D6}.Release|Any CPU.ActiveCfg = Release|Any CPU {11B51E5D-2FA6-4DC4-845B-8F051EC833D6}.Release|Any CPU.Build.0 = Release|Any CPU + {B95F9224-1A4D-4EA2-8228-57F5F5E96D59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B95F9224-1A4D-4EA2-8228-57F5F5E96D59}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B95F9224-1A4D-4EA2-8228-57F5F5E96D59}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B95F9224-1A4D-4EA2-8228-57F5F5E96D59}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SoftwareInstallation/SoftwareInstallationView.csproj b/SoftwareInstallation/SoftwareInstallationView.csproj index f3e2076..bdbe3a7 100644 --- a/SoftwareInstallation/SoftwareInstallationView.csproj +++ b/SoftwareInstallation/SoftwareInstallationView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -18,6 +22,7 @@ + diff --git a/SoftwareInstallationDatabaseImplement/Implements/ComponentStorage.cs b/SoftwareInstallationDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..a4a1a54 --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,91 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new SoftwareInstallationDataBase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + 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 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 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; + } + 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; + } + } + + +} diff --git a/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs b/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..3cee65c --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,99 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new SoftwareInstallationDataBase(); + return context.Orders + .Select(x => AccessPackageStorage(x.GetViewModel)) + .ToList(); + } + 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 => AccessPackageStorage(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new SoftwareInstallationDataBase(); + return AccessPackageStorage(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 SoftwareInstallationDataBase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessPackageStorage(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 AccessPackageStorage(order.GetViewModel); + } + 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 AccessPackageStorage(element.GetViewModel); + } + return null; + } + + public static OrderViewModel AccessPackageStorage(OrderViewModel model) + { + if (model == null) + return null; + using var context = new SoftwareInstallationDataBase(); + foreach (var Package in context.Packages) + { + if (Package.Id == model.PackageId) + { + model.PackageName = Package.PackageName; + break; + } + } + return model; + } + } +} diff --git a/SoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs b/SoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs new file mode 100644 index 0000000..c26a8b4 --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs @@ -0,0 +1,110 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDatabaseImplement.Implements +{ + public class PackageStorage : IPackageStorage + { + 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 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 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 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; + } + } + 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; + } + } +} + diff --git a/SoftwareInstallationDatabaseImplement/Migrations/20240308202100_InitialCreate.Designer.cs b/SoftwareInstallationDatabaseImplement/Migrations/20240308202100_InitialCreate.Designer.cs new file mode 100644 index 0000000..ff57b77 --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Migrations/20240308202100_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 SoftwareInstallationDatabaseImplement; + +#nullable disable + +namespace SoftwareInstallationDatabaseImplement.Migrations +{ + [DbContext(typeof(SoftwareInstallationDataBase))] + [Migration("20240308202100_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("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("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("PackageId") + .HasColumnType("int"); + + 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("Price") + .HasColumnType("float"); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + 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", null) + .WithMany("Orders") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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/SoftwareInstallationDatabaseImplement/Migrations/20240308202100_InitialCreate.cs b/SoftwareInstallationDatabaseImplement/Migrations/20240308202100_InitialCreate.cs new file mode 100644 index 0000000..9269a8b --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Migrations/20240308202100_InitialCreate.cs @@ -0,0 +1,125 @@ +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"), + 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), + PackageId = table.Column(type: "int", nullable: false) + }, + 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/SoftwareInstallationDatabaseImplement/Migrations/TravelCompanyDataBaseModelSnapshot.cs b/SoftwareInstallationDatabaseImplement/Migrations/TravelCompanyDataBaseModelSnapshot.cs new file mode 100644 index 0000000..16d6f3d --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Migrations/TravelCompanyDataBaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +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", "8.0.2") + .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("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("PackageId") + .HasColumnType("int"); + + 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("Price") + .HasColumnType("float"); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + 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", null) + .WithMany("Orders") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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/SoftwareInstallationDatabaseImplement/Models/Component.cs b/SoftwareInstallationDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..433f0d4 --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 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 + }; + } +} + diff --git a/SoftwareInstallationDatabaseImplement/Models/Order.cs b/SoftwareInstallationDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..f15544f --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Models/Order.cs @@ -0,0 +1,69 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Enums; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDatabaseImplement.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 PackageId { 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, + PackageId = model.PackageId, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + PackageId = PackageId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + + } +} diff --git a/SoftwareInstallationDatabaseImplement/Models/Package.cs b/SoftwareInstallationDatabaseImplement/Models/Package.cs new file mode 100644 index 0000000..768457d --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Models/Package.cs @@ -0,0 +1,103 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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; + } + } +} + diff --git a/SoftwareInstallationDatabaseImplement/Models/PackageComponent.cs b/SoftwareInstallationDatabaseImplement/Models/PackageComponent.cs new file mode 100644 index 0000000..1a77b44 --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/Models/PackageComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDatabaseImplement.Models +{ + public class PackageComponent + {//мы создаем этот доп класс, он содержит информацию сущностей Component и Package и создаем доп элементы которые будут в таблице(многие-ко-многим) + 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/SoftwareInstallationDatabaseImplement/SoftwareInstallationDataBase.cs b/SoftwareInstallationDatabaseImplement/SoftwareInstallationDataBase.cs new file mode 100644 index 0000000..6591ef3 --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/SoftwareInstallationDataBase.cs @@ -0,0 +1,29 @@ +using SoftwareInstallationDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDatabaseImplement +{//здесь определяются сущности и подключение к серверу + public class SoftwareInstallationDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder +optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-952S2NGN;Initial Catalog=SoftwareInstallationDatabase;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/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabaseImplement.csproj b/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabaseImplement.csproj new file mode 100644 index 0000000..e8506ba --- /dev/null +++ b/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabaseImplement.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + +