From 83fbe7c77412483cae6922de16f4763c8546bc17 Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Sat, 16 Mar 2024 13:44:46 +0400 Subject: [PATCH 1/4] All done --- SoftwareInstallation/SoftwareInstallation.sln | 8 +- .../SoftwareInstallation/Program.cs | 2 +- .../SoftwareInstallationView.csproj | 5 + .../BusinessLogics/OrderLogic.cs | 2 - .../Implements/ComponentStorage.cs | 86 +++++++++ .../Implements/OrderStorage.cs | 113 ++++++++++++ .../Implements/PackageStorage.cs | 110 +++++++++++ .../20240304174905_InitMigration.Designer.cs | 171 ++++++++++++++++++ .../20240304174905_InitMigration.cs | 125 +++++++++++++ ...ftwareInstallationDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 61 +++++++ .../Models/Order.cs | 76 ++++++++ .../Models/Package.cs | 106 +++++++++++ .../Models/PackageComponent.cs | 22 +++ .../SoftwareInstallationDatabase.cs | 27 +++ ...ftwareInstallationDatabaseImplement.csproj | 24 +++ 16 files changed, 1102 insertions(+), 4 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240304174905_InitMigration.Designer.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240304174905_InitMigration.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/SoftwareInstallation.sln b/SoftwareInstallation/SoftwareInstallation.sln index 3ba76bb..be76632 100644 --- a/SoftwareInstallation/SoftwareInstallation.sln +++ b/SoftwareInstallation/SoftwareInstallation.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationListImp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationBusinessLogic", "SoftwareInstallationBusinessLogic\SoftwareInstallationBusinessLogic.csproj", "{7F4E4DE0-F13B-4F3B-B86C-2C7017AD6CF3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationFileImplement", "SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj", "{B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationFileImplement", "SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj", "{B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationDatabaseImplement", "SoftwareInstallationDatabaseImplement\SoftwareInstallationDatabaseImplement.csproj", "{51FEB63A-6830-42F8-8836-5388800BF0E8}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}.Debug|Any CPU.Build.0 = Debug|Any CPU {B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}.Release|Any CPU.ActiveCfg = Release|Any CPU {B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}.Release|Any CPU.Build.0 = Release|Any CPU + {51FEB63A-6830-42F8-8836-5388800BF0E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {51FEB63A-6830-42F8-8836-5388800BF0E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {51FEB63A-6830-42F8-8836-5388800BF0E8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {51FEB63A-6830-42F8-8836-5388800BF0E8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SoftwareInstallation/SoftwareInstallation/Program.cs b/SoftwareInstallation/SoftwareInstallation/Program.cs index 3c94573..50665d7 100644 --- a/SoftwareInstallation/SoftwareInstallation/Program.cs +++ b/SoftwareInstallation/SoftwareInstallation/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using SoftwareInstallationBusinessLogic.BusinessLogics; using SoftwareInstallationContracts.BusinessLogicContracts; using SoftwareInstallationContracts.StoragesContracts; -using SoftwareInstallationFileImplement.Implements; +using SoftwareInstallationDatabaseImplement.Implements; using SoftwareInstallationView; namespace SoftwareInstallation diff --git a/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj b/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj index e8917fb..4aa8640 100644 --- a/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj +++ b/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -28,6 +32,7 @@ + diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs index cd11bd1..b92300b 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs @@ -63,9 +63,7 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics model.Status = newStatus; if (model.Status == OrderStatus.Выдан) - { model.DateImplement = DateTime.Now; - } model.Sum = viewModel.Sum; model.Count = viewModel.Count; diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/ComponentStorage.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..7dabaea --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,86 @@ +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/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..4219c63 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,113 @@ +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 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).Select(x => GetPackageName(x)).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; + } + + private static OrderViewModel GetPackageName(OrderViewModel viewModel) + { + using var context = new SoftwareInstallationDatabase(); + + var package = context.Packages.FirstOrDefault(x => x.Id == viewModel.PackageId); + + if (package == null) + { + return viewModel; + } + + viewModel.PackageName = package.PackageName; + return viewModel; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs new file mode 100644 index 0000000..7d915cf --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs @@ -0,0 +1,110 @@ +using Microsoft.EntityFrameworkCore; +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 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/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240304174905_InitMigration.Designer.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240304174905_InitMigration.Designer.cs new file mode 100644 index 0000000..84d46e6 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240304174905_InitMigration.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 SoftwareInstallationDatabaseImplement; + +#nullable disable + +namespace SoftwareInstallationDatabaseImplement.Migrations +{ + [DbContext(typeof(SoftwareInstallationDatabase))] + [Migration("20240304174905_InitMigration")] + partial class InitMigration + { + /// + 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("PackageId") + .HasColumnType("int"); + + 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/20240304174905_InitMigration.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240304174905_InitMigration.cs new file mode 100644 index 0000000..c943f54 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240304174905_InitMigration.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SoftwareInstallationDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : 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), + 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..e076682 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/SoftwareInstallationDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +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("PackageId") + .HasColumnType("int"); + + 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..577412a --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +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; +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; + +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/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..c8b7df7 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs @@ -0,0 +1,76 @@ +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; } + + public int PackageId { 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 Package Package { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Order() + { + Id = model.Id, + PackageId = model.PackageId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + PackageId = PackageId, + 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..936b624 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Package.cs @@ -0,0 +1,106 @@ +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; +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; + +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/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/PackageComponent.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/PackageComponent.cs new file mode 100644 index 0000000..a439e0b --- /dev/null +++ b/SoftwareInstallation/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 + { + 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..a0e502b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabase.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using SoftwareInstallationDatabaseImplement.Models; +using System; +using System.Collections.Generic; +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=DESKTOP-SPMUS7R;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/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabaseImplement.csproj b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabaseImplement.csproj new file mode 100644 index 0000000..2f8cfae --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + -- 2.25.1 From a03f0c7e695415c62896038a155280270464d86d Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Fri, 12 Apr 2024 21:44:23 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8=20PR=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 5 +++-- .../SoftwareInstallationDatabaseImplement/Models/Order.cs | 1 + .../Implements/OrderStorage.cs | 8 +++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs index 4219c63..61cef47 100644 --- a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace SoftwareInstallationDatabaseImplement.Implements { @@ -51,14 +52,14 @@ namespace SoftwareInstallationDatabaseImplement.Implements using var context = new SoftwareInstallationDatabase(); - return context.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Package).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).Select(x => GetPackageName(x)).ToList(); + return context.Orders.Include(x => x.Package).Select(x => x.GetViewModel).ToList(); } public OrderViewModel? Insert(OrderBindingModel model) diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs index c8b7df7..0303f0d 100644 --- a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs @@ -66,6 +66,7 @@ namespace SoftwareInstallationDatabaseImplement.Models { Id = Id, PackageId = PackageId, + PackageName = Package.PackageName, Count = Count, Sum = Sum, Status = Status, diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs index b555ea6..39ef51f 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs @@ -92,12 +92,10 @@ namespace SoftwareInstallationFileImplement.Implements } private OrderViewModel GetPackageName(OrderViewModel viewModel) { - foreach (var package in source.Packages) + var package = source.Packages.FirstOrDefault(x => x.Id == viewModel.PackageId); + if (package != null) { - if (package.Id == viewModel.PackageId) - { - viewModel.PackageName = package.PackageName; - } + viewModel.PackageName = package.PackageName; } return viewModel; } -- 2.25.1 From be0690f470765c9e1ec63002bebe90cad02e4611 Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Fri, 12 Apr 2024 21:54:54 +0400 Subject: [PATCH 3/4] last fix --- .../Implements/OrderStorage.cs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs index 61cef47..c9fe224 100644 --- a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs @@ -95,20 +95,5 @@ namespace SoftwareInstallationDatabaseImplement.Implements return order.GetViewModel; } - - private static OrderViewModel GetPackageName(OrderViewModel viewModel) - { - using var context = new SoftwareInstallationDatabase(); - - var package = context.Packages.FirstOrDefault(x => x.Id == viewModel.PackageId); - - if (package == null) - { - return viewModel; - } - - viewModel.PackageName = package.PackageName; - return viewModel; - } } } -- 2.25.1 From 1d42d66ba86a0071e9aaf084aebccbb7b993cceb Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Thu, 2 May 2024 22:55:41 +0400 Subject: [PATCH 4/4] All done --- .../SoftwareInstallation/FormShop.cs | 5 +- .../BusinessLogics/OrderLogic.cs | 5 +- .../Implements/OrderStorage.cs | 9 +- .../Implements/ShopStorage.cs | 178 +++++++++++++ .../20240429151038_AddShopModel.Designer.cs | 248 ++++++++++++++++++ .../Migrations/20240429151038_AddShopModel.cs | 78 ++++++ ...ftwareInstallationDatabaseModelSnapshot.cs | 77 ++++++ .../Models/Order.cs | 7 +- .../Models/Shop.cs | 110 ++++++++ .../Models/ShopPackage.cs | 27 ++ .../SoftwareInstallationDatabase.cs | 6 +- 11 files changed, 736 insertions(+), 14 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/ShopStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240429151038_AddShopModel.Designer.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240429151038_AddShopModel.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Shop.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/ShopPackage.cs diff --git a/SoftwareInstallation/SoftwareInstallation/FormShop.cs b/SoftwareInstallation/SoftwareInstallation/FormShop.cs index 71b4502..c625bfc 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormShop.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormShop.cs @@ -57,8 +57,9 @@ namespace SoftwareInstallationView ShopName = nameTextBox.Text, ShopAddress = addressTextBox.Text, OpeningDate = openingDatePicker.Value.Date, - PackageMaxCount = (int)numericUpDown.Value - }; + PackageMaxCount = (int)numericUpDown.Value, + ShopPackages = _listStores + }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) { diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs index 5cf085f..17ea9c9 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs @@ -67,6 +67,7 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics model.Status = newStatus; if (model.Status == OrderStatus.Выдан) + { model.DateImplement = DateTime.Now; var package = _packageStorage.GetElement(new() { Id = viewModel.PackageId }); @@ -81,8 +82,8 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics throw new Exception($"AddPackage operation failed. Store is full."); } } - else - { + + else { model.DateImplement = viewModel.DateImplement; } diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs index c9fe224..23671a7 100644 --- a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs @@ -40,7 +40,7 @@ namespace SoftwareInstallationDatabaseImplement.Implements using var context = new SoftwareInstallationDatabase(); - return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Orders.Include(x => x.Package).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) @@ -64,15 +64,14 @@ namespace SoftwareInstallationDatabaseImplement.Implements public OrderViewModel? Insert(OrderBindingModel model) { - var newOrder = Order.Create(model); + using var context = new SoftwareInstallationDatabase(); + var newOrder = Order.Create(context, model); if (newOrder == null) { return null; } - using var context = new SoftwareInstallationDatabase(); - context.Orders.Add(newOrder); context.SaveChanges(); @@ -83,7 +82,7 @@ namespace SoftwareInstallationDatabaseImplement.Implements { using var context = new SoftwareInstallationDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders.Include(x => x.Package).FirstOrDefault(x => x.Id == model.Id); if (order == null) { diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..1eb47fb --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,178 @@ +using Microsoft.EntityFrameworkCore; +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDatabaseImplement.Models; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new SoftwareInstallationDatabase(); + + var element = context.Shops + .Include(x => x.Packages) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + + using var context = new SoftwareInstallationDatabase(); + + return context.Shops + .Include(x => x.Packages) + .ThenInclude(x => x.Package) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + + using var context = new SoftwareInstallationDatabase(); + + return context.Shops + .Include(x => x.Packages) + .ThenInclude(x => x.Package) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new SoftwareInstallationDatabase(); + return context.Shops + .Include(x => x.Packages) + .ThenInclude(x => x.Package) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new SoftwareInstallationDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + + public bool SellPackage(IPackageModel model, int quantity) + { + using var context = new SoftwareInstallationDatabase(); + + if (context.Shops + .Include(x => x.Packages) + .ThenInclude(x => x.Package) + .ToList() + .Where(x => x.ShopPackages.ContainsKey(model.Id)).Select(x => x.PackageMaxCount).Sum() < quantity) + { + return false; + } + + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var Shop in context.Shops + .Include(x => x.Packages) + .ThenInclude(x => x.Package) + .ToList() + .Where(x => x.ShopPackages.ContainsKey(model.Id))) + { + int countInCurrentShop = Shop.ShopPackages[model.Id].Item2; + if (countInCurrentShop <= quantity) + { + var elem = context.ShopPackages + .Where(x => x.PackageId == model.Id) + .FirstOrDefault(x => x.ShopId == Shop.Id); + context.ShopPackages.Remove(elem); + Shop.ShopPackages.Remove(model.Id); + quantity -= countInCurrentShop; + } + else + { + Shop.ShopPackages[model.Id] = (Shop.ShopPackages[model.Id].Item1, countInCurrentShop - quantity); + quantity = 0; + Shop.UpdatePackage(context, new() + { + Id = Shop.Id, + ShopPackages = Shop.ShopPackages, + }); + } + if (quantity == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new SoftwareInstallationDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (Shop == null) + { + return null; + } + Shop.Update(model); + Shop.UpdatePackage(context, model); + context.SaveChanges(); + transaction.Commit(); + return Shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240429151038_AddShopModel.Designer.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240429151038_AddShopModel.Designer.cs new file mode 100644 index 0000000..e1d8c56 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240429151038_AddShopModel.Designer.cs @@ -0,0 +1,248 @@ +// +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("20240429151038_AddShopModel")] + partial class AddShopModel + { + /// + 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("PackageId") + .HasColumnType("int"); + + 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.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("PackageMaxCount") + .HasColumnType("int"); + + b.Property("ShopAddress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.ShopPackage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PackageId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PackageId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPackages"); + }); + + 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.ShopPackage", b => + { + b.HasOne("SoftwareInstallationDatabaseImplement.Models.Package", "Package") + .WithMany() + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SoftwareInstallationDatabaseImplement.Models.Shop", "Shop") + .WithMany("Packages") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Package"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.Component", b => + { + b.Navigation("PackageComponents"); + }); + + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.Package", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.Shop", b => + { + b.Navigation("Packages"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240429151038_AddShopModel.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240429151038_AddShopModel.cs new file mode 100644 index 0000000..fbbbed6 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240429151038_AddShopModel.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SoftwareInstallationDatabaseImplement.Migrations +{ + /// + public partial class AddShopModel : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + ShopAddress = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + PackageMaxCount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopPackages", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PackageId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopPackages", x => x.Id); + table.ForeignKey( + name: "FK_ShopPackages_Packages_PackageId", + column: x => x.PackageId, + principalTable: "Packages", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPackages_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopPackages_PackageId", + table: "ShopPackages", + column: "PackageId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPackages_ShopId", + table: "ShopPackages", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopPackages"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/SoftwareInstallationDatabaseModelSnapshot.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/SoftwareInstallationDatabaseModelSnapshot.cs index e076682..2b06282 100644 --- a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/SoftwareInstallationDatabaseModelSnapshot.cs +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/SoftwareInstallationDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace SoftwareInstallationDatabaseImplement.Migrations b.ToTable("PackageComponents"); }); + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("PackageMaxCount") + .HasColumnType("int"); + + b.Property("ShopAddress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.ShopPackage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PackageId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PackageId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPackages"); + }); + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.Order", b => { b.HasOne("SoftwareInstallationDatabaseImplement.Models.Package", "Package") @@ -151,6 +204,25 @@ namespace SoftwareInstallationDatabaseImplement.Migrations b.Navigation("Package"); }); + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.ShopPackage", b => + { + b.HasOne("SoftwareInstallationDatabaseImplement.Models.Package", "Package") + .WithMany() + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SoftwareInstallationDatabaseImplement.Models.Shop", "Shop") + .WithMany("Packages") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Package"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.Component", b => { b.Navigation("PackageComponents"); @@ -162,6 +234,11 @@ namespace SoftwareInstallationDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Models.Shop", b => + { + b.Navigation("Packages"); + }); #pragma warning restore 612, 618 } } diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs index 0303f0d..f33aec7 100644 --- a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Order.cs @@ -33,7 +33,7 @@ namespace SoftwareInstallationDatabaseImplement.Models public virtual Package Package { get; set; } - public static Order? Create(OrderBindingModel? model) + public static Order? Create(SoftwareInstallationDatabase context, OrderBindingModel? model) { if (model == null) { @@ -48,8 +48,9 @@ namespace SoftwareInstallationDatabaseImplement.Models Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, - DateImplement = model.DateImplement - }; + DateImplement = model.DateImplement, + Package = context.Packages.First(x => x.Id == model.PackageId) + }; } public void Update(OrderBindingModel model) diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Shop.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..8881058 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/Shop.cs @@ -0,0 +1,110 @@ +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; +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; + +namespace SoftwareInstallationDatabaseImplement.Models +{ + public class Shop : IShopModel + { + [Required] + public string ShopName { get; private set; } = string.Empty; + [Required] + public string ShopAddress { get; private set; } = string.Empty; + [Required] + public DateTime OpeningDate { get; private set; } + [Required] + public int PackageMaxCount { get; private set; } + + public int Id { get; private set; } + + + private Dictionary _ShopPackages = null; + [NotMapped] + public Dictionary ShopPackages + { + get + { + if (_ShopPackages == null) + { + _ShopPackages = Packages + .ToDictionary(recPC => recPC.PackageId, recPC => (recPC.Package as IPackageModel, recPC.Count)); + } + return _ShopPackages; + } + } + + [ForeignKey("ShopId")] + public virtual List Packages { get; set; } = new(); + + public static Shop Create(SoftwareInstallationDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + ShopAddress = model.ShopAddress, + OpeningDate = model.OpeningDate, + PackageMaxCount = model.PackageMaxCount, + Packages = model.ShopPackages.Select(x => new ShopPackage + { + Package = context.Packages.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + ShopAddress = model.ShopAddress; + OpeningDate = model.OpeningDate; + PackageMaxCount = model.PackageMaxCount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + ShopAddress = ShopAddress, + OpeningDate = OpeningDate, + PackageMaxCount = PackageMaxCount, + ShopPackages = ShopPackages + }; + + public void UpdatePackage(SoftwareInstallationDatabase context, ShopBindingModel model) + { + var ShopPackages = context.ShopPackages.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopPackages != null && ShopPackages.Count > 0) + { // удалили те, которых нет в модели + context.ShopPackages.RemoveRange(ShopPackages.Where(rec => !model.ShopPackages.ContainsKey(rec.PackageId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updatePackage in ShopPackages) + { + updatePackage.Count = model.ShopPackages[updatePackage.PackageId].Item2; + model.ShopPackages.Remove(updatePackage.PackageId); + } + context.SaveChanges(); + } + var Shop = context.Shops.First(x => x.Id == Id); + foreach (var sp in model.ShopPackages) + { + context.ShopPackages.Add(new ShopPackage + { + Shop = Shop, + Package = context.Packages.First(x => x.Id == sp.Key), + Count = sp.Value.Item2 + }); + context.SaveChanges(); + } + _ShopPackages = null; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/ShopPackage.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/ShopPackage.cs new file mode 100644 index 0000000..b0e026f --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Models/ShopPackage.cs @@ -0,0 +1,27 @@ +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 ShopPackage + { + public int Id { get; set; } + + [Required] + public int PackageId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Package Package { get; set; } = new(); + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabase.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabase.cs index a0e502b..022875f 100644 --- a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabase.cs +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabase.cs @@ -15,7 +15,7 @@ namespace SoftwareInstallationDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-SPMUS7R;Initial Catalog=SoftwareInstallationDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-SPMUS7R;Initial Catalog=SoftwareInstallationHardDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -23,5 +23,7 @@ namespace SoftwareInstallationDatabaseImplement public virtual DbSet Packages { set; get; } public virtual DbSet PackageComponents { set; get; } public virtual DbSet Orders { set; get; } - } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopPackages { set; get; } + } } -- 2.25.1