diff --git a/FurnitureAssembly/FurnitureAssembly.sln b/FurnitureAssembly/FurnitureAssembly.sln index 1df31a9..9919bc9 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}") = "FurnitureAssemblyBusinessLogic", "FurnitureAssemblyBusinessLogic\FurnitureAssemblyBusinessLogic.csproj", "{D5118A99-17DE-4722-AA56-3A358CE0BB62}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyFileImplement", "FurnitureAssemblyFileImplement\FurnitureAssemblyFileImplement.csproj", "{AD1729BF-2142-4014-9C7C-A2BAF426DFEB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyFileImplement", "FurnitureAssemblyFileImplement\FurnitureAssemblyFileImplement.csproj", "{AD1729BF-2142-4014-9C7C-A2BAF426DFEB}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyDatabaseImplement", "FurnitureAssemblyDatabaseImplement\FurnitureAssemblyDatabaseImplement.csproj", "{C069851C-EA8C-4738-A31B-3FC6BA27634D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {AD1729BF-2142-4014-9C7C-A2BAF426DFEB}.Debug|Any CPU.Build.0 = Debug|Any CPU {AD1729BF-2142-4014-9C7C-A2BAF426DFEB}.Release|Any CPU.ActiveCfg = Release|Any CPU {AD1729BF-2142-4014-9C7C-A2BAF426DFEB}.Release|Any CPU.Build.0 = Release|Any CPU + {C069851C-EA8C-4738-A31B-3FC6BA27634D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C069851C-EA8C-4738-A31B-3FC6BA27634D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C069851C-EA8C-4738-A31B-3FC6BA27634D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C069851C-EA8C-4738-A31B-3FC6BA27634D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj b/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj index 35f5384..bf7b9ef 100644 --- a/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj +++ b/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -28,6 +32,7 @@ + diff --git a/FurnitureAssembly/FurnitureAssembly/Program.cs b/FurnitureAssembly/FurnitureAssembly/Program.cs index 5cadfa4..9d25fa9 100644 --- a/FurnitureAssembly/FurnitureAssembly/Program.cs +++ b/FurnitureAssembly/FurnitureAssembly/Program.cs @@ -2,7 +2,7 @@ using FurnitureAssemblyBusinessLogic; using FurnitureAssemblyBusinessLogic.BusinessLogics; using FurnitureAssemblyContracts.BusinessLogicsContracts; using FurnitureAssemblyContracts.StoragesContracts; -using FurnitureAssemblyFileImplement.Implements; +using FurnitureAssemblyDatabaseImplement.Implements; using FurnitureAssemblyView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs index 3635c52..7f45e76 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs @@ -134,7 +134,7 @@ namespace FurnitureAssemblyBusinessLogic if (model.Status == OrderStatus.Выдан) { - model.DateImplement = DateTime.Now; + model.DateImplement = DateTime.Now.ToUniversalTime(); } else { diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/OrderBindingModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/OrderBindingModel.cs index 280e941..574198a 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/OrderBindingModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/OrderBindingModel.cs @@ -15,7 +15,7 @@ namespace FurnitureAssemblyContracts.BindingModels public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.Now.ToUniversalTime(); public DateTime? DateImplement { get; set; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs new file mode 100644 index 0000000..ccd91eb --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs @@ -0,0 +1,33 @@ +using FurnitureAssemblyDatabaseImplement.Models; +using FurnitureAssemblyListImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +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.UseNpgsql("Host=10.211.55.3;Database=FurnitureAssemblyDatabaseFull;Username=postgres;Password=postgres"); + } + + 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..03780b4 --- /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..14188b5 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/FurnitureStorage.cs @@ -0,0 +1,126 @@ +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 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..08f318e --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,103 @@ +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..da8fc44 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/WorkpieceStorage.cs @@ -0,0 +1,100 @@ +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/20240401175259_init.Designer.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240401175259_init.Designer.cs new file mode 100644 index 0000000..a52a6cc --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240401175259_init.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using FurnitureAssemblyDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FurnitureAssemblyDatabaseImplement.Migrations +{ + [DbContext(typeof(FurnitureAssemblyDatabase))] + [Migration("20240401175259_init")] + partial class init + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.7") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("FurnitureName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Furnitures"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureWorkpiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("FurnitureId") + .HasColumnType("integer"); + + b.Property("WorkpieceId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("WorkpieceId"); + + b.ToTable("FurnitureWorkpieces"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("FurnitureId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Workpiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("WorkpieceName") + .IsRequired() + .HasColumnType("text"); + + 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/20240401175259_init.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240401175259_init.cs new file mode 100644 index 0000000..f4b8ee5 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20240401175259_init.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FurnitureAssemblyDatabaseImplement.Migrations +{ + /// + public partial class init : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Furnitures", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + FurnitureName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Furnitures", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Workpieces", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + WorkpieceName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Workpieces", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + FurnitureId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with time zone", 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: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + FurnitureId = table.Column(type: "integer", nullable: false), + WorkpieceId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", 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..01a640f --- /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.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#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.7") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("FurnitureName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Furnitures"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureWorkpiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("FurnitureId") + .HasColumnType("integer"); + + b.Property("WorkpieceId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("WorkpieceId"); + + b.ToTable("FurnitureWorkpieces"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("FurnitureId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Workpiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("WorkpieceName") + .IsRequired() + .HasColumnType("text"); + + 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..2dcdd8f --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Furniture.cs @@ -0,0 +1,118 @@ +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..6437f2b --- /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..13eb771 --- /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.ToUniversalTime(); + + 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..171c0b3 --- /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 + }; + } +}