From b6bdd6deb44381184575ca723ada4b9b479fdc10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D1=81=D0=BB=D0=B0=D0=B2=20?= =?UTF-8?q?=D0=97=D0=B0=D1=85=D0=B0=D1=80=D0=BE=D0=B2?= Date: Sun, 21 May 2023 21:30:15 +0400 Subject: [PATCH] Lab3 --- FurnitureAssembly/FurnitureAssembly.csproj | 5 + FurnitureAssembly/FurnitureAssembly.sln | 6 + FurnitureAssembly/Program.cs | 2 +- .../FurnitureAssemblyDataBase.cs | 26 +++ .../FurnitureAssemblyDatabaseImplement.csproj | 22 +++ .../Implements/ComponentStoreage.cs | 97 ++++++++++ .../Implements/OrderStorage.cs | 98 ++++++++++ .../Implements/ProductStorage.cs | 111 +++++++++++ .../20230521172613_InitMigration.Designer.cs | 175 ++++++++++++++++++ .../20230521172613_InitMigration.cs | 126 +++++++++++++ .../FurnitureAssemblyDataBaseModelSnapshot.cs | 172 +++++++++++++++++ .../Models/Component.cs | 65 +++++++ .../Models/Order.cs | 86 +++++++++ .../Models/Product.cs | 106 +++++++++++ .../Models/ProductComponent.cs | 26 +++ 15 files changed, 1122 insertions(+), 1 deletion(-) create mode 100644 FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDataBase.cs create mode 100644 FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabaseImplement.csproj create mode 100644 FurnitureAssemblyDatabaseImplement/Implements/ComponentStoreage.cs create mode 100644 FurnitureAssemblyDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FurnitureAssemblyDatabaseImplement/Implements/ProductStorage.cs create mode 100644 FurnitureAssemblyDatabaseImplement/Migrations/20230521172613_InitMigration.Designer.cs create mode 100644 FurnitureAssemblyDatabaseImplement/Migrations/20230521172613_InitMigration.cs create mode 100644 FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDataBaseModelSnapshot.cs create mode 100644 FurnitureAssemblyDatabaseImplement/Models/Component.cs create mode 100644 FurnitureAssemblyDatabaseImplement/Models/Order.cs create mode 100644 FurnitureAssemblyDatabaseImplement/Models/Product.cs create mode 100644 FurnitureAssemblyDatabaseImplement/Models/ProductComponent.cs diff --git a/FurnitureAssembly/FurnitureAssembly.csproj b/FurnitureAssembly/FurnitureAssembly.csproj index c410d4f..e6ad81b 100644 --- a/FurnitureAssembly/FurnitureAssembly.csproj +++ b/FurnitureAssembly/FurnitureAssembly.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/FurnitureAssembly/FurnitureAssembly.sln b/FurnitureAssembly/FurnitureAssembly.sln index d6deb98..7c3ef23 100644 --- a/FurnitureAssembly/FurnitureAssembly.sln +++ b/FurnitureAssembly/FurnitureAssembly.sln @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyBusinessLo EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyFileImplement", "..\FurnitureAssemblyFileImplement\FurnitureAssemblyFileImplement.csproj", "{A55CCB0B-73D1-4887-9B25-9747F1E2DFC4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyDatabaseImplement", "..\FurnitureAssemblyDatabaseImplement\FurnitureAssemblyDatabaseImplement.csproj", "{FD269542-C15E-4C26-BC6C-FDB93C20D8B5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {A55CCB0B-73D1-4887-9B25-9747F1E2DFC4}.Debug|Any CPU.Build.0 = Debug|Any CPU {A55CCB0B-73D1-4887-9B25-9747F1E2DFC4}.Release|Any CPU.ActiveCfg = Release|Any CPU {A55CCB0B-73D1-4887-9B25-9747F1E2DFC4}.Release|Any CPU.Build.0 = Release|Any CPU + {FD269542-C15E-4C26-BC6C-FDB93C20D8B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FD269542-C15E-4C26-BC6C-FDB93C20D8B5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FD269542-C15E-4C26-BC6C-FDB93C20D8B5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FD269542-C15E-4C26-BC6C-FDB93C20D8B5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FurnitureAssembly/Program.cs b/FurnitureAssembly/Program.cs index 6013025..a516471 100644 --- a/FurnitureAssembly/Program.cs +++ b/FurnitureAssembly/Program.cs @@ -1,7 +1,7 @@ using FurnitureAssemblyBusinessLogic.BusinessLogic; using FurnitureAssemblyContracts.BusinessLogicsContracts; using FurnitureAssemblyContracts.StoragesContracts; -using FurnitureAssemblyFileImplement.Implements; +using FurnitureAssemblyDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDataBase.cs b/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDataBase.cs new file mode 100644 index 0000000..0128a91 --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDataBase.cs @@ -0,0 +1,26 @@ +using FurnitureAssemblyDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement +{ + public class FurnitureAssemblyDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-KVE3KO9;Initial Catalog=FurnitureAssemblyDataBaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Products { set; get; } + public virtual DbSet ProductComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabaseImplement.csproj b/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabaseImplement.csproj new file mode 100644 index 0000000..2da52b0 --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabaseImplement.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/FurnitureAssemblyDatabaseImplement/Implements/ComponentStoreage.cs b/FurnitureAssemblyDatabaseImplement/Implements/ComponentStoreage.cs new file mode 100644 index 0000000..7f53caf --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Implements/ComponentStoreage.cs @@ -0,0 +1,97 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new FurnitureAssemblyDataBase(); + + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + return null; + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + + using var context = new FurnitureAssemblyDataBase(); + + return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + + using var context = new FurnitureAssemblyDataBase(); + + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new FurnitureAssemblyDataBase(); + + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + + if (newComponent == null) + { + return null; + } + + using var context = new FurnitureAssemblyDataBase(); + + context.Components.Add(newComponent); + context.SaveChanges(); + + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new FurnitureAssemblyDataBase(); + + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + + if (component == null) + { + return null; + } + + component.Update(model); + context.SaveChanges(); + + return component.GetViewModel; + } + } +} diff --git a/FurnitureAssemblyDatabaseImplement/Implements/OrderStorage.cs b/FurnitureAssemblyDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..3bf8ebe --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,98 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FurnitureAssemblyDataBase(); + + 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 FurnitureAssemblyDataBase(); + + 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 FurnitureAssemblyDataBase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new FurnitureAssemblyDataBase(); + + return context.Orders.Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + using var context = new FurnitureAssemblyDataBase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new FurnitureAssemblyDataBase(); + + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return order.GetViewModel; + } + } +} diff --git a/FurnitureAssemblyDatabaseImplement/Implements/ProductStorage.cs b/FurnitureAssemblyDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..eb62995 --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,111 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new FurnitureAssemblyDataBase(); + + var element = context.Products.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Products.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + return null; + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.ProductName) && !model.Id.HasValue) + { + return null; + } + + using var context = new FurnitureAssemblyDataBase(); + + return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.ProductName)) + { + return new(); + } + + using var context = new FurnitureAssemblyDataBase(); + + return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ProductName.Contains(model.ProductName)).ToList().Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new FurnitureAssemblyDataBase(); + + return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new FurnitureAssemblyDataBase(); + + var newProduct = Product.Create(context, model); + + if (newProduct == null) + { + return null; + } + + context.Products.Add(newProduct); + context.SaveChanges(); + + return newProduct.GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new FurnitureAssemblyDataBase(); + + using var transaction = context.Database.BeginTransaction(); + + try + { + var product = context.Products.FirstOrDefault(rec => rec.Id == model.Id); + + if (product == null) + { + return null; + } + + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + + throw; + } + } + } +} diff --git a/FurnitureAssemblyDatabaseImplement/Migrations/20230521172613_InitMigration.Designer.cs b/FurnitureAssemblyDatabaseImplement/Migrations/20230521172613_InitMigration.Designer.cs new file mode 100644 index 0000000..6c8b2c5 --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Migrations/20230521172613_InitMigration.Designer.cs @@ -0,0 +1,175 @@ +// +using System; +using FurnitureAssemblyDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FurnitureAssemblyDatabaseImplement.Migrations +{ + [DbContext(typeof(FurnitureAssemblyDataBase))] + [Migration("20230521172613_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.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("FurnitureAssemblyDatabaseImplement.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("ProductId") + .HasColumnType("int"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.ProductComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ProductId"); + + b.ToTable("ProductComponents"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Product", "Product") + .WithMany("Orders") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.ProductComponent", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Component", "Component") + .WithMany("PackageComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Product", "Product") + .WithMany("Components") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Component", b => + { + b.Navigation("PackageComponents"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Product", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FurnitureAssemblyDatabaseImplement/Migrations/20230521172613_InitMigration.cs b/FurnitureAssemblyDatabaseImplement/Migrations/20230521172613_InitMigration.cs new file mode 100644 index 0000000..23764bc --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Migrations/20230521172613_InitMigration.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FurnitureAssemblyDatabaseImplement.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: "Products", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ProductName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Products", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ProductId = table.Column(type: "int", nullable: false), + ProductName = table.Column(type: "nvarchar(max)", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ProductComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ProductId = 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_ProductComponents", x => x.Id); + table.ForeignKey( + name: "FK_ProductComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ProductComponents_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ProductId", + table: "Orders", + column: "ProductId"); + + migrationBuilder.CreateIndex( + name: "IX_ProductComponents_ComponentId", + table: "ProductComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_ProductComponents_ProductId", + table: "ProductComponents", + column: "ProductId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "ProductComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Products"); + } + } +} diff --git a/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDataBaseModelSnapshot.cs b/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDataBaseModelSnapshot.cs new file mode 100644 index 0000000..37c77fc --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDataBaseModelSnapshot.cs @@ -0,0 +1,172 @@ +// +using System; +using FurnitureAssemblyDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FurnitureAssemblyDatabaseImplement.Migrations +{ + [DbContext(typeof(FurnitureAssemblyDataBase))] + partial class FurnitureAssemblyDataBaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.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("FurnitureAssemblyDatabaseImplement.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("ProductId") + .HasColumnType("int"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.ProductComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ProductId"); + + b.ToTable("ProductComponents"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Product", "Product") + .WithMany("Orders") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.ProductComponent", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Component", "Component") + .WithMany("PackageComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Product", "Product") + .WithMany("Components") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Component", b => + { + b.Navigation("PackageComponents"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Product", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FurnitureAssemblyDatabaseImplement/Models/Component.cs b/FurnitureAssemblyDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..6e8d8b7 --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Models/Component.cs @@ -0,0 +1,65 @@ +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 FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; + +namespace FurnitureAssemblyDatabaseImplement.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/FurnitureAssemblyDatabaseImplement/Models/Order.cs b/FurnitureAssemblyDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..3b96b3e --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Models/Order.cs @@ -0,0 +1,86 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Enums; +using FurnitureAssemblyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int ProductId { get; private set; } + + public string ProductName { get; private set; } = string.Empty; + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public virtual Product Product { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Order() + { + Id = model.Id, + ProductId = model.ProductId, + ProductName = model.ProductName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + + ProductId = model.ProductId; + ProductName = model.ProductName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + ProductId = ProductId, + ProductName = ProductName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/FurnitureAssemblyDatabaseImplement/Models/Product.cs b/FurnitureAssemblyDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..36c5b8e --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Models/Product.cs @@ -0,0 +1,106 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.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 FurnitureAssemblyDatabaseImplement.Models +{ + public class Product : IProductModel + { + public int Id { get; set; } + + [Required] + public string ProductName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _productComponents = null; + + [NotMapped] + public Dictionary ProductComponents + { + get + { + if (_productComponents == null) + { + _productComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _productComponents; + } + } + + [ForeignKey("ProductId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("ProductId")] + public virtual List Orders { get; set; } = new(); + + public static Product Create(FurnitureAssemblyDataBase context, ProductBindingModel model) + { + return new Product() + { + Id = model.Id, + ProductName = model.ProductName, + Price = model.Price, + Components = model.ProductComponents.Select(x => new ProductComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ProductBindingModel model) + { + ProductName = model.ProductName; + Price = model.Price; + } + + public ProductViewModel GetViewModel => new() + { + Id = Id, + ProductName = ProductName, + Price = Price, + ProductComponents = ProductComponents + }; + + public void UpdateComponents(FurnitureAssemblyDataBase context, ProductBindingModel model) + { + var productComponents = context.ProductComponents.Where(rec => rec.ProductId == model.Id).ToList(); + + if (productComponents != null && productComponents.Count > 0) + { // удалили те, которых нет в модели + context.ProductComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in productComponents) + { + updateComponent.Count = model.ProductComponents[updateComponent.ComponentId].Item2; + model.ProductComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + + var product = context.Products.First(x => x.Id == Id); + + foreach (var pc in model.ProductComponents) + { + context.ProductComponents.Add(new ProductComponent + { + Product = product, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _productComponents = null; + } + } +} diff --git a/FurnitureAssemblyDatabaseImplement/Models/ProductComponent.cs b/FurnitureAssemblyDatabaseImplement/Models/ProductComponent.cs new file mode 100644 index 0000000..8a57dd8 --- /dev/null +++ b/FurnitureAssemblyDatabaseImplement/Models/ProductComponent.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Models +{ + public class ProductComponent + { + public int Id { get; set; } + + [Required] + public int ProductId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} -- 2.25.1