From 9a001e5a7b578528192b6155d9ce680e807d17d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D0=B1=D0=B5=D0=B5=D0=B2=20=D0=90=D0=BB=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Fri, 5 Apr 2024 21:44:17 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=203=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CarpentryWorkshop/CarpentryWorkshop.sln | 8 +- .../CarpentryWorkshopDatabase.cs | 27 +++ .../CarpentryWorkshopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 81 +++++++++ .../Implements/OrderStorage.cs | 83 +++++++++ .../Implements/WoodStorage.cs | 93 ++++++++++ .../20240405163856_InitMigration.Designer.cs | 171 ++++++++++++++++++ .../20240405163856_InitMigration.cs | 125 +++++++++++++ .../CarpentryWorkshopDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 65 +++++++ .../Models/Order.cs | 75 ++++++++ .../Models/Wood.cs | 90 +++++++++ .../Models/WoodComponent.cs | 27 +++ .../CarpentryWorkshopView.csproj | 5 + .../CarpentryWorkshopView/Program.cs | 2 +- 15 files changed, 1041 insertions(+), 2 deletions(-) create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/WoodStorage.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240405163856_InitMigration.Designer.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240405163856_InitMigration.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Component.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/WoodComponent.cs diff --git a/CarpentryWorkshop/CarpentryWorkshop.sln b/CarpentryWorkshop/CarpentryWorkshop.sln index ca80d14..8c0a4fb 100644 --- a/CarpentryWorkshop/CarpentryWorkshop.sln +++ b/CarpentryWorkshop/CarpentryWorkshop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopListImplem EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopBusinessLogic", "CarpentryWorkshopBusinessLogic\CarpentryWorkshopBusinessLogic.csproj", "{D550ED8D-31C0-42D2-A129-5BB96EDEBF3F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarpentryWorkshopFileImplement", "CarpentryWorkshopFileImplement\CarpentryWorkshopFileImplement.csproj", "{EFC90D78-298D-457A-9959-09FC58632F75}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopFileImplement", "CarpentryWorkshopFileImplement\CarpentryWorkshopFileImplement.csproj", "{EFC90D78-298D-457A-9959-09FC58632F75}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarpentryWorkshopDatabaseImplement", "CarpentryWorkshopDatabaseImplement\CarpentryWorkshopDatabaseImplement.csproj", "{C757AC3F-9408-48DA-B781-8CA1C0421F19}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {EFC90D78-298D-457A-9959-09FC58632F75}.Debug|Any CPU.Build.0 = Debug|Any CPU {EFC90D78-298D-457A-9959-09FC58632F75}.Release|Any CPU.ActiveCfg = Release|Any CPU {EFC90D78-298D-457A-9959-09FC58632F75}.Release|Any CPU.Build.0 = Release|Any CPU + {C757AC3F-9408-48DA-B781-8CA1C0421F19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C757AC3F-9408-48DA-B781-8CA1C0421F19}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C757AC3F-9408-48DA-B781-8CA1C0421F19}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C757AC3F-9408-48DA-B781-8CA1C0421F19}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs new file mode 100644 index 0000000..a255388 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs @@ -0,0 +1,27 @@ +using CarpentryWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement +{ + public class CarpentryWorkshopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=CarpentryWorkshopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Woods { set; get; } + public virtual DbSet WoodComponents { set; get; } + public virtual DbSet Orders { set; get; } + + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj new file mode 100644 index 0000000..96db0ca --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ComponentStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..6c7ad29 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,81 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new CarpentryWorkshopDatabase(); + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName)) && x.ComponentName == model.ComponentName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var element = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..0b3fcf3 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,83 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + return context.Orders.Include(x => x.Wood).Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new CarpentryWorkshopDatabase(); + return context.Orders.Include(x => x.Wood).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + return context.Orders.Include(x => x.Wood).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders.Include(x => x.Wood).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + 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.Wood).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + var deletedElement = context.Orders.Include(x => x.Wood).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/WoodStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/WoodStorage.cs new file mode 100644 index 0000000..77a350c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/WoodStorage.cs @@ -0,0 +1,93 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement.Implements +{ + public class WoodStorage : IWoodStorage + { + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + return context.Woods.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(WoodSearchModel model) + { + if (string.IsNullOrEmpty(model.WoodName)) + { + return new(); + } + using var context = new CarpentryWorkshopDatabase(); + return context.Woods.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.WoodName.Contains(model.WoodName)).ToList().Select(x => x.GetViewModel).ToList(); + } + + public WoodViewModel? GetElement(WoodSearchModel model) + { + if (string.IsNullOrEmpty(model.WoodName) && !model.Id.HasValue) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + return context.Woods.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.WoodName) && x.WoodName == model.WoodName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public WoodViewModel? Insert(WoodBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var newWood = Wood.Create(context, model); + if (newWood == null) + { + return null; + } + context.Woods.Add(newWood); + context.SaveChanges(); + return newWood.GetViewModel; + } + + public WoodViewModel? Update(WoodBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var wood = context.Woods.FirstOrDefault(x => x.Id == model.Id); + if (wood == null) + { + return null; + } + wood.Update(model); + context.SaveChanges(); + wood.UpdateComponents(context, model); + transaction.Commit(); + return wood.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public WoodViewModel? Delete(WoodBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var element = context.Woods.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Woods.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240405163856_InitMigration.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240405163856_InitMigration.Designer.cs new file mode 100644 index 0000000..db5f401 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240405163856_InitMigration.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using CarpentryWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace CarpentryWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(CarpentryWorkshopDatabase))] + [Migration("20240405163856_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.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("CarpentryWorkshopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("WoodId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("WoodId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("WoodName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Woods"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("WoodId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("WoodId"); + + b.ToTable("WoodComponents"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood") + .WithMany("Orders") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Wood"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b => + { + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Component", "Component") + .WithMany("WoodComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood") + .WithMany("Components") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Wood"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Component", b => + { + b.Navigation("WoodComponents"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240405163856_InitMigration.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240405163856_InitMigration.cs new file mode 100644 index 0000000..382a1ed --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240405163856_InitMigration.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace CarpentryWorkshopDatabaseImplement.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: "Woods", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WoodName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Woods", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WoodId = 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_Woods_WoodId", + column: x => x.WoodId, + principalTable: "Woods", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "WoodComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WoodId = 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_WoodComponents", x => x.Id); + table.ForeignKey( + name: "FK_WoodComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_WoodComponents_Woods_WoodId", + column: x => x.WoodId, + principalTable: "Woods", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_WoodId", + table: "Orders", + column: "WoodId"); + + migrationBuilder.CreateIndex( + name: "IX_WoodComponents_ComponentId", + table: "WoodComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_WoodComponents_WoodId", + table: "WoodComponents", + column: "WoodId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "WoodComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Woods"); + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..912c18b --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using CarpentryWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace CarpentryWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(CarpentryWorkshopDatabase))] + partial class CarpentryWorkshopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.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("CarpentryWorkshopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("WoodId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("WoodId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("WoodName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Woods"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("WoodId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("WoodId"); + + b.ToTable("WoodComponents"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood") + .WithMany("Orders") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Wood"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b => + { + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Component", "Component") + .WithMany("WoodComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood") + .WithMany("Components") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Wood"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Component", b => + { + b.Navigation("WoodComponents"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Component.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..5be7e93 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Component.cs @@ -0,0 +1,65 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.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 CarpentryWorkshopDatabaseImplement.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 WoodComponents { 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/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..7654d2e --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs @@ -0,0 +1,75 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Enums; +using CarpentryWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + + [Required] + public int WoodId { get; set; } + + [Required] + public int Count { get; set; } + + [Required] + public double Sum { get; set; } + + [Required] + public OrderStatus Status { get; set; } + + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + public virtual Wood Wood { get; set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + WoodId = model.WoodId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + WoodId = WoodId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + WoodName = Wood.WoodName + }; + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs new file mode 100644 index 0000000..79278e0 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs @@ -0,0 +1,90 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.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 CarpentryWorkshopDatabaseImplement.Models +{ + public class Wood : IWoodModel + { + public int Id { get; set; } + + [Required] + public string WoodName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _woodComponents = null; + + [NotMapped] + public Dictionary WoodComponents + { + get + { + if (_woodComponents == null) + { + _woodComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _woodComponents; + } + } + + [ForeignKey("WoodId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("WoodId")] + public virtual List Orders { get; set; } = new(); + + public static Wood Create(CarpentryWorkshopDatabase context, WoodBindingModel model) + { + return new Wood() + { + Id = model.Id, + WoodName = model.WoodName, + Price = model.Price, + Components = model.WoodComponents.Select(x => new WoodComponent + { + Component = context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(WoodBindingModel model) + { + WoodName = model.WoodName; + Price = model.Price; + } + + public WoodViewModel GetViewModel => new() + { + Id = Id, + WoodName = WoodName, + Price = Price, + WoodComponents = WoodComponents + }; + + public void UpdateComponents(CarpentryWorkshopDatabase context, WoodBindingModel model) + { + var woodComponents = context.WoodComponents.Where(rec => rec.WoodId == model.Id).ToList(); + if (woodComponents != null && woodComponents.Count > 0) + { + context.WoodComponents.RemoveRange(woodComponents.Where(rec => !model.WoodComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + foreach (var updateComponent in woodComponents) + { + updateComponent.Count = model.WoodComponents[updateComponent.ComponentId].Item2; + model.WoodComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + _woodComponents = null; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/WoodComponent.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/WoodComponent.cs new file mode 100644 index 0000000..4a32e1b --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/WoodComponent.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 CarpentryWorkshopDatabaseImplement.Models +{ + public class WoodComponent + { + public int Id { get; set; } + + [Required] + public int WoodId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Wood Wood { get; set; } = new(); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj b/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj index a3a6577..bf8da62 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -28,6 +32,7 @@ + diff --git a/CarpentryWorkshop/CarpentryWorkshopView/Program.cs b/CarpentryWorkshop/CarpentryWorkshopView/Program.cs index 8e97d60..178a4b2 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/Program.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/Program.cs @@ -1,7 +1,7 @@ using CarpentryWorkshopBusinessLogic.BusinessLogics; using CarpentryWorkshopContracts.BusinessLogicsContracts; using CarpentryWorkshopContracts.StoragesContracts; -using CarpentryWorkshopFileImplement.Implements; +using CarpentryWorkshopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1