From 20aa4971d583e5aa129b3221ebb57d0991074f6b Mon Sep 17 00:00:00 2001 From: "nikbel2004@outlook.com" Date: Mon, 11 Mar 2024 12:12:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FurnitureAssembly/FurnitureAssembly.sln | 8 +- .../FurnitureAssemblyDatabase.cs | 31 ++++ .../FurnitureAssemblyDatabaseImplement.csproj | 23 +++ .../Implements/FurnitureStorage.cs | 127 +++++++++++++ .../Implements/OrderStorage.cs | 104 +++++++++++ .../Implements/WorkPieceStorage.cs | 102 +++++++++++ .../20240311045740_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240311045740_InitialCreate.cs | 125 +++++++++++++ .../FurnitureAssemblyDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Furniture.cs | 119 ++++++++++++ .../Models/FurnitureWorkPiece.cs | 27 +++ .../Models/Order.cs | 80 ++++++++ .../Models/WorkPiece.cs | 71 ++++++++ .../FurnitureAssemblyView.csproj | 5 + .../FurnitureAssemblyView/Program.cs | 2 +- 15 files changed, 1161 insertions(+), 2 deletions(-) create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabaseImplement.csproj create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/FurnitureStorage.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/WorkPieceStorage.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240311045740_InitialCreate.Designer.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240311045740_InitialCreate.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Furniture.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/FurnitureWorkPiece.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Order.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/WorkPiece.cs diff --git a/FurnitureAssembly/FurnitureAssembly.sln b/FurnitureAssembly/FurnitureAssembly.sln index f8ab1b3..c7c3e4a 100644 --- a/FurnitureAssembly/FurnitureAssembly.sln +++ b/FurnitureAssembly/FurnitureAssembly.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyListImplem EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyView", "FurnitureAssemblyView\FurnitureAssemblyView.csproj", "{77944C5A-53FC-437B-A563-E7C09C769859}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyFileImplement", "FurnitureAssemblyFileImplement\FurnitureAssemblyFileImplement.csproj", "{2EF175D6-B6BD-442B-B81E-67E368553726}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyFileImplement", "FurnitureAssemblyFileImplement\FurnitureAssemblyFileImplement.csproj", "{2EF175D6-B6BD-442B-B81E-67E368553726}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyDatabaseImplement", "FurnitureAssemblyDatabaseImplement\FurnitureAssemblyDatabaseImplement.csproj", "{C2302631-5038-4592-B75F-C03C3ABD510D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {2EF175D6-B6BD-442B-B81E-67E368553726}.Debug|Any CPU.Build.0 = Debug|Any CPU {2EF175D6-B6BD-442B-B81E-67E368553726}.Release|Any CPU.ActiveCfg = Release|Any CPU {2EF175D6-B6BD-442B-B81E-67E368553726}.Release|Any CPU.Build.0 = Release|Any CPU + {C2302631-5038-4592-B75F-C03C3ABD510D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C2302631-5038-4592-B75F-C03C3ABD510D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C2302631-5038-4592-B75F-C03C3ABD510D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C2302631-5038-4592-B75F-C03C3ABD510D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs new file mode 100644 index 0000000..ef1bba3 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs @@ -0,0 +1,31 @@ +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=KatanaNik\SQLEXPRESS;Initial Catalog=FurnitureAssemblyDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet WorkPieces { set; get; } + + public virtual DbSet Furnitures { set; get; } + + public virtual DbSet FurnitureWorkPieces { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabaseImplement.csproj b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabaseImplement.csproj new file mode 100644 index 0000000..481038d --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/FurnitureStorage.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/FurnitureStorage.cs new file mode 100644 index 0000000..4516b25 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/FurnitureStorage.cs @@ -0,0 +1,127 @@ +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.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Implements +{ + public class FurnitureStorage : IFurnitureStorage + { + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + + return context.Furnitures + .Include(x => x.WorkPieces) + .ThenInclude(x => x.WorkPiece) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(FurnitureSearchModel model) + { + if (string.IsNullOrEmpty(model.FurnitureName)) + { + return new(); + } + + using var context = new FurnitureAssemblyDatabase(); + + return context.Furnitures + .Include(x => x.WorkPieces) + .ThenInclude(x => x.WorkPiece) + .Where(x => x.FurnitureName.Contains(model.FurnitureName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public FurnitureViewModel? GetElement(FurnitureSearchModel model) + { + if(string.IsNullOrEmpty(model.FurnitureName) && !model.Id.HasValue) + { + return null; + } + + using var context = new FurnitureAssemblyDatabase(); + + return context.Furnitures + .Include(x => x.WorkPieces) + .ThenInclude(x => x.WorkPiece) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.FurnitureName) && x.FurnitureName == model.FurnitureName) || + model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public FurnitureViewModel? Insert(FurnitureBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var newFurniture = Furniture.Create(context, model); + + if (newFurniture == null) + { + return null; + } + + context.Furnitures.Add(newFurniture); + context.SaveChanges(); + + return newFurniture.GetViewModel; + } + + public FurnitureViewModel? Update(FurnitureBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var furniture = context.Furnitures.FirstOrDefault(rec => rec.Id == model.Id); + + if (furniture == null) + { + return null; + } + + furniture.Update(model); + context.SaveChanges(); + furniture.UpdateWorkPieces(context, model); + transaction.Commit(); + + return furniture.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public FurnitureViewModel? Delete(FurnitureBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var element = context.Furnitures + .Include(x => x.WorkPieces) + .Include(x => x.Orders) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Furnitures.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/OrderStorage.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..ced1ec8 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,104 @@ +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 OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new FurnitureAssemblyDatabase(); + + return context.Orders.Include(x => x.Furniture). + 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 + .Include(x => x.Furniture) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + + return context.Orders.Include(x => x.Furniture) + .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 context.Orders.Include(x => x.Furniture) + .FirstOrDefault(x => x.Id == newOrder.Id)?.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 context.Orders.Include(x => x.Furniture).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + 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; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/WorkPieceStorage.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/WorkPieceStorage.cs new file mode 100644 index 0000000..4b7f266 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/WorkPieceStorage.cs @@ -0,0 +1,102 @@ +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 WorkPieceStorage : IWorkPieceStorage + { + public List GetFullList() + { + using var context = new FurnitureAssemblyDatabase(); + + return context.WorkPieces + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(WorkPieceSearchModel model) + { + if (string.IsNullOrEmpty(model.WorkPieceName)) + { + return new(); + } + + using var context = new FurnitureAssemblyDatabase(); + + return context.WorkPieces + .Where(x => x.WorkPieceName.Contains(model.WorkPieceName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public WorkPieceViewModel? GetElement(WorkPieceSearchModel model) + { + if(string.IsNullOrEmpty(model.WorkPieceName) && !model.Id.HasValue) + { + return null; + } + + using var context = new FurnitureAssemblyDatabase(); + + return context.WorkPieces + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.WorkPieceName) && x.WorkPieceName == model.WorkPieceName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public WorkPieceViewModel? Insert(WorkPieceBindingModel model) + { + var newWorkPiece = WorkPiece.Create(model); + + if(newWorkPiece == null) + { + return null; + } + + using var context = new FurnitureAssemblyDatabase(); + context.WorkPieces.Add(newWorkPiece); + context.SaveChanges(); + + return newWorkPiece.GetViewModel; + } + + public WorkPieceViewModel? Update(WorkPieceBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var workPiece = context.WorkPieces.FirstOrDefault(x => x.Id == model.Id); + + if (workPiece == null) + { + return null; + } + + workPiece.Update(model); + context.SaveChanges(); + + return workPiece.GetViewModel; + } + + public WorkPieceViewModel? Delete(WorkPieceBindingModel model) + { + using var context = new FurnitureAssemblyDatabase(); + var element = context.WorkPieces.FirstOrDefault(rec => rec.Id == model.Id); + + if(element != null) + { + context.WorkPieces.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240311045740_InitialCreate.Designer.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240311045740_InitialCreate.Designer.cs new file mode 100644 index 0000000..4dd4e17 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240311045740_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +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("20240311045740_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("FurnitureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Furnitures"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureWorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FurnitureId") + .HasColumnType("int"); + + b.Property("WorkPieceId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("WorkPieceId"); + + b.ToTable("FurnitureWorkPieces"); + }); + + 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("FurnitureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.WorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("WorkPieceName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("WorkPieces"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureWorkPiece", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture") + .WithMany("WorkPieces") + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.WorkPiece", "WorkPiece") + .WithMany("FurnitureWorkPieces") + .HasForeignKey("WorkPieceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + + b.Navigation("WorkPiece"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture") + .WithMany("Orders") + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b => + { + b.Navigation("Orders"); + + b.Navigation("WorkPieces"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.WorkPiece", b => + { + b.Navigation("FurnitureWorkPieces"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240311045740_InitialCreate.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240311045740_InitialCreate.cs new file mode 100644 index 0000000..6413913 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240311045740_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FurnitureAssemblyDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Furnitures", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FurnitureName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Furnitures", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "WorkPieces", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WorkPieceName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_WorkPieces", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FurnitureId = 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_Furnitures_FurnitureId", + column: x => x.FurnitureId, + principalTable: "Furnitures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "FurnitureWorkPieces", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FurnitureId = table.Column(type: "int", nullable: false), + WorkPieceId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_FurnitureWorkPieces", x => x.Id); + table.ForeignKey( + name: "FK_FurnitureWorkPieces_Furnitures_FurnitureId", + column: x => x.FurnitureId, + principalTable: "Furnitures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_FurnitureWorkPieces_WorkPieces_WorkPieceId", + column: x => x.WorkPieceId, + principalTable: "WorkPieces", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_FurnitureWorkPieces_FurnitureId", + table: "FurnitureWorkPieces", + column: "FurnitureId"); + + migrationBuilder.CreateIndex( + name: "IX_FurnitureWorkPieces_WorkPieceId", + table: "FurnitureWorkPieces", + column: "WorkPieceId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_FurnitureId", + table: "Orders", + column: "FurnitureId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "FurnitureWorkPieces"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "WorkPieces"); + + migrationBuilder.DropTable( + name: "Furnitures"); + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs new file mode 100644 index 0000000..97e2bb5 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +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.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("FurnitureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Furnitures"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureWorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FurnitureId") + .HasColumnType("int"); + + b.Property("WorkPieceId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("WorkPieceId"); + + b.ToTable("FurnitureWorkPieces"); + }); + + 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("FurnitureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.WorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("WorkPieceName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("WorkPieces"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureWorkPiece", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture") + .WithMany("WorkPieces") + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.WorkPiece", "WorkPiece") + .WithMany("FurnitureWorkPieces") + .HasForeignKey("WorkPieceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + + b.Navigation("WorkPiece"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture") + .WithMany("Orders") + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b => + { + b.Navigation("Orders"); + + b.Navigation("WorkPieces"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.WorkPiece", b => + { + b.Navigation("FurnitureWorkPieces"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Furniture.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Furniture.cs new file mode 100644 index 0000000..b2f1f02 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Furniture.cs @@ -0,0 +1,119 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using Microsoft.EntityFrameworkCore.Diagnostics; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Models +{ + public class Furniture : IFurnitureModel + { + public int Id { get; set; } + + [Required] + public string FurnitureName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + public Dictionary? _furnitureWorkPieces = null; + + // Это поле не будет "мапиться" в бд + [NotMapped] + public Dictionary FurnitureWorkPieces + { + get + { + if(_furnitureWorkPieces == null) + { + _furnitureWorkPieces = WorkPieces + .ToDictionary(recPC => recPC.WorkPieceId, recPC => (recPC.WorkPiece as IWorkPieceModel, recPC.Count)); + } + + return _furnitureWorkPieces; + } + } + + // Для реализации связи многие ко многим с заготовками + [ForeignKey("FurnitureId")] + public virtual List WorkPieces { get; set; } = new(); + + [ForeignKey("FurnitureId")] + public virtual List Orders { get; set; } = new(); + + public static Furniture Create(FurnitureAssemblyDatabase context, FurnitureBindingModel model) + { + return new Furniture() + { + Id = model.Id, + FurnitureName = model.FurnitureName, + Price = model.Price, + WorkPieces = model.FurnitureWorkPieces.Select(x => new FurnitureWorkPiece + { + WorkPiece = context.WorkPieces.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(FurnitureBindingModel model) + { + FurnitureName = model.FurnitureName; + Price = model.Price; + } + + public FurnitureViewModel GetViewModel => new() + { + Id = Id, + FurnitureName = FurnitureName, + Price = Price, + FurnitureWorkPieces = FurnitureWorkPieces + }; + + Dictionary IFurnitureModel.FurnitureWorkPieces => throw new NotImplementedException(); + + + public void UpdateWorkPieces(FurnitureAssemblyDatabase context, FurnitureBindingModel model) + { + var furnitureWorkPieces = context.FurnitureWorkPieces.Where(rec => rec.FurnitureId == model.Id).ToList(); + + if(furnitureWorkPieces != null && furnitureWorkPieces.Count > 0) + { + // удалили те, которых нет в модели + context.FurnitureWorkPieces.RemoveRange(furnitureWorkPieces.Where(rec => !model.FurnitureWorkPieces.ContainsKey(rec.FurnitureId))); + context.SaveChanges(); + + // обновили количество у существующих записей + foreach (var updateFurniture in furnitureWorkPieces) + { + updateFurniture.Count = model.FurnitureWorkPieces[updateFurniture.FurnitureId].Item2; + model.FurnitureWorkPieces.Remove(updateFurniture.FurnitureId); + } + + context.SaveChanges(); + } + + var furniture = context.Furnitures.First(x => x.Id == Id); + + foreach(var pc in model.FurnitureWorkPieces) + { + context.FurnitureWorkPieces.Add(new FurnitureWorkPiece + { + Furniture = furniture, + WorkPiece = context.WorkPieces.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + + context.SaveChanges(); + } + + _furnitureWorkPieces = null; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/FurnitureWorkPiece.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/FurnitureWorkPiece.cs new file mode 100644 index 0000000..688440d --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/FurnitureWorkPiece.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 FurnitureAssemblyDatabaseImplement.Models +{ + public class FurnitureWorkPiece + { + public int Id { get; set; } + + [Required] + public int FurnitureId { get; set; } + + [Required] + public int WorkPieceId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual WorkPiece WorkPiece { get; set; } = new(); + + public virtual Furniture Furniture { get; set; } = new(); + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Order.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..07268ab --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Order.cs @@ -0,0 +1,80 @@ +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; } + + [Required] + public int FurnitureId { 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 Furniture Furniture { get; set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + + return new Order() + { + Id = model.Id, + FurnitureId = model.FurnitureId, + 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, + FurnitureId = FurnitureId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + FurnitureName = Furniture.FurnitureName + }; + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/WorkPiece.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/WorkPiece.cs new file mode 100644 index 0000000..7bf4529 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/WorkPiece.cs @@ -0,0 +1,71 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Models +{ + public class WorkPiece : IWorkPieceModel + { + public int Id { get; private set; } + + [Required] + public string WorkPieceName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + // для реализации связи многие ко многим с изделиями + [ForeignKey("WorkPieceId")] + public virtual List FurnitureWorkPieces { get; set; } = new(); + + public static WorkPiece? Create(WorkPieceBindingModel model) + { + if(model == null) + { + return null; + } + + return new WorkPiece() + { + Id = model.Id, + WorkPieceName = model.WorkPieceName, + Cost = model.Cost + }; + } + + public static WorkPiece Create(WorkPieceViewModel model) + { + return new WorkPiece + { + Id = model.Id, + WorkPieceName = model.WorkPieceName, + Cost = model.Cost + }; + } + + public void Update(WorkPieceBindingModel model) + { + if(model == null) + { + return; + } + + WorkPieceName = model.WorkPieceName; + Cost = model.Cost; + } + + public WorkPieceViewModel GetViewModel => new() + { + Id = Id, + WorkPieceName = WorkPieceName, + Cost = Cost + }; + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyView/FurnitureAssemblyView.csproj b/FurnitureAssembly/FurnitureAssemblyView/FurnitureAssemblyView.csproj index afbf708..f2d0856 100644 --- a/FurnitureAssembly/FurnitureAssemblyView/FurnitureAssemblyView.csproj +++ b/FurnitureAssembly/FurnitureAssemblyView/FurnitureAssemblyView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/FurnitureAssembly/FurnitureAssemblyView/Program.cs b/FurnitureAssembly/FurnitureAssemblyView/Program.cs index 6fc4d21..542087c 100644 --- a/FurnitureAssembly/FurnitureAssemblyView/Program.cs +++ b/FurnitureAssembly/FurnitureAssemblyView/Program.cs @@ -1,7 +1,7 @@ using FurnitureAssemblyBusinessLogic.BussinessLogic; using FurnitureAssemblyContracts.BusinessLogicsContracts; using FurnitureAssemblyContracts.StoragesContracts; -using FurnitureAssemblyFileImplement.Implements; +using FurnitureAssemblyDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging;