From 5bb0036836b51c810acf996fa1ef83e5ca1ac703 Mon Sep 17 00:00:00 2001 From: leoevgeniy <98206383+leoevgeniy@users.noreply.github.com> Date: Mon, 18 Mar 2024 20:21:19 +0400 Subject: [PATCH 1/4] Lab3 done --- CarpentryWorkshop/CarpentryWorkshop.sln | 6 + .../CarpentryWorkshopDatabase.cs | 21 +++ .../CarpentryWorkshopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 85 +++++++++ .../Implements/OrderStorage.cs | 81 ++++++++ .../Implements/WoodStorage.cs | 106 +++++++++++ .../20240318160111_InitialCreate.Designer.cs | 173 ++++++++++++++++++ .../20240318160111_InitialCreate.cs | 126 +++++++++++++ .../CarpentryWorkshopDatabaseModelSnapshot.cs | 170 +++++++++++++++++ .../Models/Component.cs | 63 +++++++ .../Models/Order.cs | 72 ++++++++ .../Models/Wood.cs | 90 +++++++++ .../Models/WoodComponent.cs | 22 +++ .../CarpentryWorkshopView.csproj | 15 +- .../CarpentryWorkshopView/Program.cs | 2 +- 15 files changed, 1049 insertions(+), 6 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/20240318160111_InitialCreate.Designer.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.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 8128ce7..62281c6 100644 --- a/CarpentryWorkshop/CarpentryWorkshop.sln +++ b/CarpentryWorkshop/CarpentryWorkshop.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopListImplem EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarpentryWorkshopFileImplement", "CarpentryWorkshopFileImplement\CarpentryWorkshopFileImplement.csproj", "{C9BA55BB-A2E0-413E-8F93-74A0F455C682}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarpentryWorkshopDatabaseImplement", "CarpentryWorkshopDatabaseImplement\CarpentryWorkshopDatabaseImplement.csproj", "{E8A783E3-7330-4F9A-B4CB-913C70A0B08F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {C9BA55BB-A2E0-413E-8F93-74A0F455C682}.Debug|Any CPU.Build.0 = Debug|Any CPU {C9BA55BB-A2E0-413E-8F93-74A0F455C682}.Release|Any CPU.ActiveCfg = Release|Any CPU {C9BA55BB-A2E0-413E-8F93-74A0F455C682}.Release|Any CPU.Build.0 = Release|Any CPU + {E8A783E3-7330-4F9A-B4CB-913C70A0B08F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E8A783E3-7330-4F9A-B4CB-913C70A0B08F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E8A783E3-7330-4F9A-B4CB-913C70A0B08F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E8A783E3-7330-4F9A-B4CB-913C70A0B08F}.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..75ee4da --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs @@ -0,0 +1,21 @@ +using CarpentryWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +namespace CarpentryWorkshopDatabaseImplement +{ + public class CarpentryWorkshopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder + optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=KOMPUTER\SQLEXPRESS;Initial Catalog=CarpentryWorkshopDatabase;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; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj new file mode 100644 index 0000000..d9f920a --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net8.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..54d2792 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,85 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; + +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(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..f6d7a2d --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,81 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; + +namespace CarpentryWorkshopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + return context.Orders + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (model.Id == null) + { + return new(); + } + using var context = new CarpentryWorkshopDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (model.Id == null && !model.Id.HasValue) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + return context.Orders + .FirstOrDefault(x => (model.Id != null && x.Id == model.Id) || (model.Id.HasValue && 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 newOrder.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + + // + public OrderViewModel? 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 order.GetViewModel; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/WoodStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/WoodStorage.cs new file mode 100644 index 0000000..a4164dd --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/WoodStorage.cs @@ -0,0 +1,106 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +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(rec => rec.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; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.Designer.cs new file mode 100644 index 0000000..c31a556 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.Designer.cs @@ -0,0 +1,173 @@ +// +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("20240318160111_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .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.Property("WoodName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + 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", null) + .WithMany("Orders") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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/20240318160111_InitialCreate.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.cs new file mode 100644 index 0000000..4ccc59c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace CarpentryWorkshopDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : 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), + WoodName = table.Column(type: "nvarchar(max)", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_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..2d1d4e2 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs @@ -0,0 +1,170 @@ +// +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", "8.0.3") + .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.Property("WoodName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + 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", null) + .WithMany("Orders") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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..58551a4 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +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 + }; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..dd7746f --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs @@ -0,0 +1,72 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Enums; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + public int WoodId { get; set; } + public string WoodName { get; set; } = string.Empty; + + public int Count { get; set; } + + public double Sum { get; set; } + + public OrderStatus Status { get; set; } + + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + WoodId = model.WoodId, + WoodName = model.WoodName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Id = model.Id; + WoodId = model.WoodId; + WoodName = model.WoodName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + WoodId = WoodId, + WoodName = WoodName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs new file mode 100644 index 0000000..6d01520 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs @@ -0,0 +1,90 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +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(); + } + var wood = context.Woods.First(x => x.Id == Id); + foreach (var pc in model.WoodComponents) + { + context.WoodComponents.Add(new WoodComponent + { + Wood = wood, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _woodComponents = null; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/WoodComponent.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/WoodComponent.cs new file mode 100644 index 0000000..a83860a --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/WoodComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +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(); + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj b/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj index d0d744a..c79b34e 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj @@ -2,22 +2,27 @@ WinExe - net6.0-windows + net8.0-windows7.0 enable true enable - - - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopView/Program.cs b/CarpentryWorkshop/CarpentryWorkshopView/Program.cs index e33e82b..8a44ff1 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 From bf04eec28bd2282adadb6289f0704506ec6a0e35 Mon Sep 17 00:00:00 2001 From: Yourdax Date: Tue, 19 Mar 2024 14:20:44 +0400 Subject: [PATCH 2/4] fixed --- .../BusinessLogics/OrderLogic.cs | 4 +- .../BindingModels/OrderBindingModel .cs | 2 +- .../Models/IOrderModel.cs | 6 +- .../CarpentryWorkshopDatabase.cs | 15 ++- .../CarpentryWorkshopDatabaseImplement.csproj | 1 + .../Implements/OrderStorage.cs | 95 ++++++++++++------- ... 20240319095810_InitialCreate.Designer.cs} | 52 +++++----- ...ate.cs => 20240319095810_InitialCreate.cs} | 45 ++++----- .../CarpentryWorkshopDatabaseModelSnapshot.cs | 50 +++++----- .../Models/Order.cs | 8 +- 10 files changed, 151 insertions(+), 127 deletions(-) rename CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/{20240318160111_InitialCreate.Designer.cs => 20240319095810_InitialCreate.Designer.cs} (74%) rename CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/{20240318160111_InitialCreate.cs => 20240319095810_InitialCreate.cs} (64%) diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index dfbd4c1..48c3bbc 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -43,7 +43,7 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics Count = model.Count, Sum = model.Sum, Status = OrderStatus.Принят, - DateCreate = DateTime.Now + DateCreate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc) }); ; return true; } @@ -96,7 +96,7 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics Count = order.Count, Sum = order.Sum, DateCreate = order.DateCreate, - DateImplement = DateTime.Now, + DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), Status = OrderStatus.Готов }); return true; diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs index c44a424..e6d0d44 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel .cs @@ -16,7 +16,7 @@ namespace CarpentryWorkshopContracts.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.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs index 7276b02..ebc1d47 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs @@ -10,11 +10,7 @@ namespace CarpentryWorkshopDataModels.Models public interface IOrderModel : IId { int WoodId { get; } - string WoodName - { - get; - } - + string WoodName{ get;} int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs index 75ee4da..51d04d5 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs @@ -1,15 +1,20 @@ -using CarpentryWorkshopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; +using CarpentryWorkshopDatabaseImplement.Models; +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) + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=KOMPUTER\SQLEXPRESS;Initial Catalog=CarpentryWorkshopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=CarpentryWorkshopDatabaseFull;Username=postgres;Password=postgres"); } base.OnConfiguring(optionsBuilder); } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj index d9f920a..39785eb 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj @@ -13,6 +13,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs index f6d7a2d..3bfc675 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -3,41 +3,65 @@ 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() + public OrderViewModel? Delete(OrderBindingModel model) { using var context = new CarpentryWorkshopDatabase(); - return context.Orders - .Select(x => x.GetViewModel) - .ToList(); - } - public List GetFilteredList(OrderSearchModel model) - { - if (model.Id == null) + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) { - return new(); + context.Orders.Remove(element); + context.SaveChanges(); + + return GetViewModel(element); } - using var context = new CarpentryWorkshopDatabase(); - return context.Orders - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); + + return null; } + public OrderViewModel? GetElement(OrderSearchModel model) { - if (model.Id == null && !model.Id.HasValue) + if (!model.Id.HasValue) { return null; } + using var context = new CarpentryWorkshopDatabase(); - return context.Orders - .FirstOrDefault(x => (model.Id != null && x.Id == model.Id) || (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; + + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new CarpentryWorkshopDatabase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + + return context.Orders.Select(x => GetViewModel(x)).ToList(); + } + public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); @@ -45,37 +69,38 @@ namespace CarpentryWorkshopDatabaseImplement.Implements { return null; } + using var context = new CarpentryWorkshopDatabase(); + context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; - } - public OrderViewModel? Delete(OrderBindingModel model) - { - using var context = new CarpentryWorkshopDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Orders.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; + return GetViewModel(newOrder); } - - // 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 order.GetViewModel; + + return GetViewModel(order); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new CarpentryWorkshopDatabase(); + var element = context.Woods.FirstOrDefault(x => x.Id == order.WoodId); + viewModel.WoodName = element.WoodName; + return viewModel; } } -} \ No newline at end of file +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs similarity index 74% rename from CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.Designer.cs rename to CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs index c31a556..055a999 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs @@ -3,16 +3,16 @@ using System; using CarpentryWorkshopDatabaseImplement; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable namespace CarpentryWorkshopDatabaseImplement.Migrations { [DbContext(typeof(CarpentryWorkshopDatabase))] - [Migration("20240318160111_InitialCreate")] + [Migration("20240319095810_InitialCreate")] partial class InitialCreate { /// @@ -21,24 +21,24 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations #pragma warning disable 612, 618 modelBuilder .HasAnnotation("ProductVersion", "8.0.3") - .HasAnnotation("Relational:MaxIdentifierLength", 128); + .HasAnnotation("Relational:MaxIdentifierLength", 63); - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Component", b => { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("ComponentName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.Property("Cost") - .HasColumnType("float"); + .HasColumnType("double precision"); b.HasKey("Id"); @@ -49,31 +49,31 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("Count") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("DateCreate") - .HasColumnType("datetime2"); + .HasColumnType("timestamp with time zone"); b.Property("DateImplement") - .HasColumnType("datetime2"); + .HasColumnType("timestamp with time zone"); b.Property("Status") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("Sum") - .HasColumnType("float"); + .HasColumnType("double precision"); b.Property("WoodId") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("WoodName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.HasKey("Id"); @@ -86,16 +86,16 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("Price") - .HasColumnType("float"); + .HasColumnType("double precision"); b.Property("WoodName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.HasKey("Id"); @@ -106,18 +106,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("ComponentId") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("Count") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("WoodId") - .HasColumnType("int"); + .HasColumnType("integer"); b.HasKey("Id"); diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs similarity index 64% rename from CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.cs rename to CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs index 4ccc59c..6d91226 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240318160111_InitialCreate.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs @@ -1,5 +1,6 @@ using System; using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable @@ -15,10 +16,10 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations 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) + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) }, constraints: table => { @@ -29,10 +30,10 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations 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) + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + WoodName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) }, constraints: table => { @@ -43,15 +44,15 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations name: "Orders", columns: table => new { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - WoodId = table.Column(type: "int", nullable: false), - WoodName = table.Column(type: "nvarchar(max)", nullable: false), - Count = table.Column(type: "int", nullable: false), - Sum = table.Column(type: "float", nullable: false), - Status = table.Column(type: "int", nullable: false), - DateCreate = table.Column(type: "datetime2", nullable: false), - DateImplement = table.Column(type: "datetime2", nullable: true) + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + WoodId = table.Column(type: "integer", nullable: false), + WoodName = table.Column(type: "text", 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 => { @@ -68,11 +69,11 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations 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) + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + WoodId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) }, constraints: table => { diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs index 2d1d4e2..7375e3b 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs @@ -3,8 +3,8 @@ using System; using CarpentryWorkshopDatabaseImplement; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable @@ -18,24 +18,24 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations #pragma warning disable 612, 618 modelBuilder .HasAnnotation("ProductVersion", "8.0.3") - .HasAnnotation("Relational:MaxIdentifierLength", 128); + .HasAnnotation("Relational:MaxIdentifierLength", 63); - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Component", b => { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("ComponentName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.Property("Cost") - .HasColumnType("float"); + .HasColumnType("double precision"); b.HasKey("Id"); @@ -46,31 +46,31 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("Count") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("DateCreate") - .HasColumnType("datetime2"); + .HasColumnType("timestamp with time zone"); b.Property("DateImplement") - .HasColumnType("datetime2"); + .HasColumnType("timestamp with time zone"); b.Property("Status") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("Sum") - .HasColumnType("float"); + .HasColumnType("double precision"); b.Property("WoodId") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("WoodName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.HasKey("Id"); @@ -83,16 +83,16 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("Price") - .HasColumnType("float"); + .HasColumnType("double precision"); b.Property("WoodName") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("text"); b.HasKey("Id"); @@ -103,18 +103,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("int"); + .HasColumnType("integer"); - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("ComponentId") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("Count") - .HasColumnType("int"); + .HasColumnType("integer"); b.Property("WoodId") - .HasColumnType("int"); + .HasColumnType("integer"); b.HasKey("Id"); diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs index dd7746f..84bf173 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs @@ -47,13 +47,9 @@ namespace CarpentryWorkshopDatabaseImplement.Models { return; } - Id = model.Id; - WoodId = model.WoodId; - WoodName = model.WoodName; - Count = model.Count; - Sum = model.Sum; + Status = model.Status; - DateCreate = model.DateCreate; + DateImplement = model.DateImplement; } -- 2.25.1 From b6d15ab948de05b73398da42a1b73b2809532074 Mon Sep 17 00:00:00 2001 From: Yourdax Date: Sun, 12 May 2024 02:39:57 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=92=D0=BE=D1=82=20=D1=81=D0=B5=D0=B9?= =?UTF-8?q?=D1=87=D0=B0=D1=81=20=D0=BF=D1=80=D1=8F=D0=BC=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=87=D1=82=D0=B8=20=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20done?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CarpentryWorkshopDatabase.cs | 2 + .../Implements/ShopStorage.cs | 209 ++++++++++++++++++ ... 20240511223517_InitialCreate.Designer.cs} | 81 ++++++- ...ate.cs => 20240511223517_InitialCreate.cs} | 59 +++++ .../CarpentryWorkshopDatabaseModelSnapshot.cs | 79 +++++++ .../Models/Shop.cs | 131 +++++++++++ .../Models/ShopWood.cs | 27 +++ .../Models/Wood.cs | 2 + 8 files changed, 589 insertions(+), 1 deletion(-) create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs rename CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/{20240319095810_InitialCreate.Designer.cs => 20240511223517_InitialCreate.Designer.cs} (68%) rename CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/{20240319095810_InitialCreate.cs => 20240511223517_InitialCreate.cs} (67%) create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.cs diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs index 51d04d5..cd260dc 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs @@ -22,5 +22,7 @@ namespace CarpentryWorkshopDatabaseImplement public virtual DbSet Woods { set; get; } public virtual DbSet WoodComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { get; set; } + public virtual DbSet ShopWoods { get; set; } } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f1d2ef8 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,209 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; +using CarpentryWorkshopDataModels.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 ShopStorage : IShopStorage + { + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var element = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + return context.Shops + .Include(x => x.Woods) + .ThenInclude(x => x.Wood) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new CarpentryWorkshopDatabase(); + return context.Shops + .Include(x => x.Woods) + .ThenInclude(x => x.Wood) + .Select(x => x.GetViewModel) + .Where(x => x.ShopName.Contains(model.ShopName ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + return context.Shops + .Include(x => x.Woods) + .ThenInclude(x => x.Wood) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.ShopName == newShop.ShopName)) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + catch + { + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + try + { + if (context.Shops.Any(x => (x.ShopName == model.ShopName && x.Id != model.Id))) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + shop.Update(model); + shop.UpdateWoods(context, model); + context.SaveChanges(); + return shop.GetViewModel; + } + catch + { + throw; + } + } + public bool RestockingShops(SupplyBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var transaction = context.Database.BeginTransaction(); + var Shops = context.Shops.Include(x => x.Woods).ThenInclude(x => x.Wood).ToList(). + Where(x => x.WoodMaxCount > x.ShopWoods.Select(x => x.Value.Item2).Sum()).ToList(); + if (model == null) + { + return false; + } + try + { + foreach (Shop shop in Shops) + { + int difference = shop.WoodMaxCount - shop.ShopWoods.Select(x => x.Value.Item2).Sum(); + int refill = Math.Min(difference, model.Count); + model.Count -= refill; + if (shop.ShopWoods.ContainsKey(model.WoodId)) + { + var datePair = shop.ShopWoods[model.WoodId]; + datePair.Item2 += refill; + shop.ShopWoods[model.WoodId] = datePair; + } + else + { + var Wood = context.Woods.First(x => x.Id == model.WoodId); + shop.ShopWoods.Add(model.WoodId, (Wood, refill)); + } + shop.WoodsDictionatyUpdate(context); + if (model.Count == 0) + { + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public bool Sale(SupplySearchModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops.Include(x => x.Woods).ThenInclude(x => x.Wood).ToList(). + Where(x => x.ShopWoods.ContainsKey(model.WoodId.Value)).OrderByDescending(x => x.ShopWoods[model.WoodId.Value].Item2).ToList(); + + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.ShopWoods[model.WoodId.Value].Item2; + if (residue > 0) + { + shop.ShopWoods.Remove(model.WoodId.Value); + shop.WoodsDictionatyUpdate(context); + context.SaveChanges(); + model.Count = residue; + + } + else + { + if (residue == 0) + shop.ShopWoods.Remove(model.WoodId.Value); + else + { + var dataPair = shop.ShopWoods[model.WoodId.Value]; + dataPair.Item2 = -residue; + shop.ShopWoods[model.WoodId.Value] = dataPair; + } + + shop.WoodsDictionatyUpdate(context); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.Designer.cs similarity index 68% rename from CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs rename to CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.Designer.cs index 055a999..f579abb 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace CarpentryWorkshopDatabaseImplement.Migrations { [DbContext(typeof(CarpentryWorkshopDatabase))] - [Migration("20240319095810_InitialCreate")] + [Migration("20240511223517_InitialCreate")] partial class InitialCreate { /// @@ -82,6 +82,59 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateOpen") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.Property("WoodMaxCount") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.Property("WoodId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("WoodId"); + + b.ToTable("ShopWoods"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => { b.Property("Id") @@ -137,6 +190,25 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations .IsRequired(); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b => + { + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Woods") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood") + .WithMany("ShopWoods") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Wood"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b => { b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Component", "Component") @@ -161,11 +233,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations b.Navigation("WoodComponents"); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Woods"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => { b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("ShopWoods"); }); #pragma warning restore 612, 618 } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.cs similarity index 67% rename from CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs rename to CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.cs index 6d91226..2e504ac 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.cs @@ -26,6 +26,22 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations table.PrimaryKey("PK_Components", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopName = table.Column(type: "text", nullable: false), + Address = table.Column(type: "text", nullable: false), + DateOpen = table.Column(type: "timestamp with time zone", nullable: false), + WoodMaxCount = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Woods", columns: table => new @@ -65,6 +81,33 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopWoods", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + WoodId = table.Column(type: "integer", nullable: false), + ShopId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopWoods", x => x.Id); + table.ForeignKey( + name: "FK_ShopWoods_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopWoods_Woods_WoodId", + column: x => x.WoodId, + principalTable: "Woods", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "WoodComponents", columns: table => new @@ -97,6 +140,16 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations table: "Orders", column: "WoodId"); + migrationBuilder.CreateIndex( + name: "IX_ShopWoods_ShopId", + table: "ShopWoods", + column: "ShopId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopWoods_WoodId", + table: "ShopWoods", + column: "WoodId"); + migrationBuilder.CreateIndex( name: "IX_WoodComponents_ComponentId", table: "WoodComponents", @@ -114,9 +167,15 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Orders"); + migrationBuilder.DropTable( + name: "ShopWoods"); + migrationBuilder.DropTable( name: "WoodComponents"); + migrationBuilder.DropTable( + name: "Shops"); + migrationBuilder.DropTable( name: "Components"); diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs index 7375e3b..b756112 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs @@ -79,6 +79,59 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateOpen") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.Property("WoodMaxCount") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.Property("WoodId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("WoodId"); + + b.ToTable("ShopWoods"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => { b.Property("Id") @@ -134,6 +187,25 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations .IsRequired(); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b => + { + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Woods") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood") + .WithMany("ShopWoods") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Wood"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b => { b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Component", "Component") @@ -158,11 +230,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations b.Navigation("WoodComponents"); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Woods"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => { b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("ShopWoods"); }); #pragma warning restore 612, 618 } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..c8785c5 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs @@ -0,0 +1,131 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + + [Required] + public string ShopName { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime DateOpen { get; set; } + + [Required] + public int WoodMaxCount { get; set; } + + [ForeignKey("ShopId")] + public List Woods { get; set; } = new(); + + private Dictionary? _shopWoods = null; + + [NotMapped] + public Dictionary ShopWoods + { + get + { + if (_shopWoods == null) + { + _shopWoods = Woods + .ToDictionary(x => x.WoodId, x => + (x.Wood as IWoodModel, x.Count)); + } + return _shopWoods; + } + } + + public static Shop? Create(CarpentryWorkshopDatabase context, ShopBindingModel model) + { + if (model == null) + return null; + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + WoodMaxCount = model.WoodMaxCount, + Woods = model.ShopWoods.Select(x => new ShopWood + { + Wood = context.Woods.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + WoodMaxCount = model.WoodMaxCount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + ShopWoods = ShopWoods, + WoodMaxCount = WoodMaxCount + }; + + public void UpdateWoods(CarpentryWorkshopDatabase context, + ShopBindingModel model) + { + var shopWoods = context.ShopWoods.Where(rec => + rec.ShopId == model.Id).ToList(); + if (shopWoods != null && shopWoods.Count > 0) + { + context.ShopWoods.RemoveRange(shopWoods.Where(rec => !model.ShopWoods.ContainsKey(rec.WoodId))); + + context.SaveChanges(); + foreach (var updateWood in shopWoods) + { + updateWood.Count = + model.ShopWoods[updateWood.WoodId].Item2; + model.ShopWoods.Remove(updateWood.WoodId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var pc in model.ShopWoods) + { + context.ShopWoods.Add(new ShopWood + { + Shop = shop, + Wood = context.Woods.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _shopWoods = null; + } + public void WoodsDictionatyUpdate(CarpentryWorkshopDatabase context) + { + UpdateWoods(context, new ShopBindingModel + { + Id = Id, + ShopWoods = ShopWoods, + }); + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.cs new file mode 100644 index 0000000..4347fc5 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.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 ShopWood + { + public int Id { get; set; } + + [Required] + public int WoodId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Wood Wood { get; set; } = new(); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs index 6d01520..5ebd4d8 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs @@ -31,6 +31,8 @@ namespace CarpentryWorkshopDatabaseImplement.Models public virtual List Components { get; set; } = new(); [ForeignKey("WoodId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("WoodId")] + public virtual List ShopWoods { get; set; } = new(); public static Wood Create(CarpentryWorkshopDatabase context, WoodBindingModel model) { return new Wood() -- 2.25.1 From 195b51b68606a35cdb7d7ce7c3ba615ed1fdd839 Mon Sep 17 00:00:00 2001 From: Yourdax Date: Sun, 12 May 2024 03:02:05 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=92=D0=BE=D1=82=20=D1=82=D0=B5=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D1=8C=20done?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CarpentryWorkshopDatabase.cs | 2 ++ .../Models/Shop.cs | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs index cd260dc..4d992f5 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs @@ -17,6 +17,8 @@ namespace CarpentryWorkshopDatabaseImplement optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=CarpentryWorkshopDatabaseFull;Username=postgres;Password=postgres"); } base.OnConfiguring(optionsBuilder); + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); } public virtual DbSet Components { set; get; } public virtual DbSet Woods { set; get; } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs index c8785c5..dc356af 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs @@ -91,29 +91,27 @@ namespace CarpentryWorkshopDatabaseImplement.Models public void UpdateWoods(CarpentryWorkshopDatabase context, ShopBindingModel model) { - var shopWoods = context.ShopWoods.Where(rec => - rec.ShopId == model.Id).ToList(); - if (shopWoods != null && shopWoods.Count > 0) + var ShopWoods = context.ShopWoods.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopWoods != null && ShopWoods.Count > 0) { - context.ShopWoods.RemoveRange(shopWoods.Where(rec => !model.ShopWoods.ContainsKey(rec.WoodId))); - + context.ShopWoods.RemoveRange(ShopWoods.Where(rec => !model.ShopWoods.ContainsKey(rec.WoodId))); context.SaveChanges(); - foreach (var updateWood in shopWoods) + ShopWoods = context.ShopWoods.Where(rec => rec.ShopId == model.Id).ToList(); + foreach (var updateWood in ShopWoods) { - updateWood.Count = - model.ShopWoods[updateWood.WoodId].Item2; + updateWood.Count = model.ShopWoods[updateWood.WoodId].Item2; model.ShopWoods.Remove(updateWood.WoodId); } context.SaveChanges(); } var shop = context.Shops.First(x => x.Id == Id); - foreach (var pc in model.ShopWoods) + foreach (var ar in model.ShopWoods) { context.ShopWoods.Add(new ShopWood { Shop = shop, - Wood = context.Woods.First(x => x.Id == pc.Key), - Count = pc.Value.Item2 + Wood = context.Woods.First(x => x.Id == ar.Key), + Count = ar.Value.Item2 }); context.SaveChanges(); } -- 2.25.1