From fe555f44a9a950e5d48a7ced36b615b5f9fdf725 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Mon, 27 Feb 2023 19:38:57 +0400 Subject: [PATCH 1/9] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=203-?= =?UTF-8?q?=D0=B9=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BlacksmithWorkshop/BlacksmithWorkshop.sln | 8 ++- ...BlacksmithWorkshopDatabaseImplement.csproj | 9 +++ .../Models/Manufacture.cs | 20 ++++++ .../Models/WorkPiece.cs | 71 +++++++++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/WorkPiece.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshop.sln b/BlacksmithWorkshop/BlacksmithWorkshop.sln index 0834832..9bee79b 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop.sln +++ b/BlacksmithWorkshop/BlacksmithWorkshop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopBusinessL EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopListImplement", "BlacksmithWorkshopListImplement\BlacksmithWorkshopListImplement.csproj", "{2942D468-0C3B-4B8D-A15F-184F5D7C969A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopFileImplement", "BlacksmithWorkshopFileImplement\BlacksmithWorkshopFileImplement.csproj", "{58737D93-9A12-4D07-BF3F-86AC512CE626}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopFileImplement", "BlacksmithWorkshopFileImplement\BlacksmithWorkshopFileImplement.csproj", "{58737D93-9A12-4D07-BF3F-86AC512CE626}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopDatabaseImplement", "BlacksmithWorkshopDatabaseImplement\BlacksmithWorkshopDatabaseImplement.csproj", "{EE03BAE3-52EE-4E3B-8992-382D89E61E1A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {58737D93-9A12-4D07-BF3F-86AC512CE626}.Debug|Any CPU.Build.0 = Debug|Any CPU {58737D93-9A12-4D07-BF3F-86AC512CE626}.Release|Any CPU.ActiveCfg = Release|Any CPU {58737D93-9A12-4D07-BF3F-86AC512CE626}.Release|Any CPU.Build.0 = Release|Any CPU + {EE03BAE3-52EE-4E3B-8992-382D89E61E1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE03BAE3-52EE-4E3B-8992-382D89E61E1A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE03BAE3-52EE-4E3B-8992-382D89E61E1A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE03BAE3-52EE-4E3B-8992-382D89E61E1A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs new file mode 100644 index 0000000..fe1589b --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs @@ -0,0 +1,20 @@ +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + internal class Manufacture : IManufactureModel + { + public string ManufactureName => throw new NotImplementedException(); + + public double Price => throw new NotImplementedException(); + + public Dictionary ManufactureWorkPieces => throw new NotImplementedException(); + + public int Id => throw new NotImplementedException(); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/WorkPiece.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/WorkPiece.cs new file mode 100644 index 0000000..4702c6e --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/WorkPiece.cs @@ -0,0 +1,71 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class WorkPiece : IWorkPieceModel + { + public int Id { get; private set; } + + [Required] + public string WorkPieceName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [ForeignKey("WorkPieceId")] + public virtual List manufactureWorkPieces { get; set; } = new(); + + public static WorkPiece? Create(WorkPieceBindingModel model) + { + if(model == null) + { + return null; + } + + return new WorkPiece() + { + Id = model.Id, + WorkPieceName = model.WorkPieceName, + Cost = model.Cost + }; + } + + public static WorkPiece Create(WorkPieceViewModel model) + { + return new WorkPiece + { + Id = model.Id, + WorkPieceName = model.WorkPieceName, + Cost = model.Cost + }; + } + + public void Update(WorkPieceBindingModel model) + { + if(model == null) + { + return; + } + + WorkPieceName = model.WorkPieceName; + Cost = model.Cost; + } + + public WorkPieceViewModel GetViewModel => new() + { + Id = Id, + WorkPieceName = WorkPieceName, + Cost = Cost + }; + } +} From 6f31bcd0a07ec29ed1626fa966220ba4e119cc56 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Mon, 27 Feb 2023 21:26:22 +0400 Subject: [PATCH 2/9] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlacksmithWorkshop.csproj | 5 + .../BlacksmithWorkshopDatabase.cs | 32 ++++ ...BlacksmithWorkshopDatabaseImplement.csproj | 14 ++ .../20230227172536_InitMigration.Designer.cs | 170 ++++++++++++++++++ .../20230227172536_InitMigration.cs | 125 +++++++++++++ ...BlacksmithWorkshopDatabaseModelSnapshot.cs | 167 +++++++++++++++++ .../Models/Manufacture.cs | 110 +++++++++++- .../Models/ManufactureWorkPiece.cs | 27 +++ .../Models/Order.cs | 71 ++++++++ .../Models/WorkPiece.cs | 3 +- 10 files changed, 717 insertions(+), 7 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230227172536_InitMigration.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230227172536_InitMigration.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/ManufactureWorkPiece.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshop.csproj b/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshop.csproj index f51fc76..99ddb32 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshop.csproj +++ b/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshop.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs new file mode 100644 index 0000000..98fe0e9 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs @@ -0,0 +1,32 @@ +using BlacksmithWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement +{ + public class BlacksmithWorkshopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"DataSource=CHESHIR\SQLEXPRESS;Initial Catalog=AbstractShopDatabaseFull;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet WorkPieces { set; get; } + + public virtual DbSet Manufactures { set; get; } + + public virtual DbSet ManufactureWorkPieces { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj index 132c02c..bdecbbd 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj @@ -6,4 +6,18 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230227172536_InitMigration.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230227172536_InitMigration.Designer.cs new file mode 100644 index 0000000..fc42141 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230227172536_InitMigration.Designer.cs @@ -0,0 +1,170 @@ +// +using System; +using BlacksmithWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(BlacksmithWorkshopDatabase))] + [Migration("20230227172536_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Manufactures"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureWorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.Property("WorkPieceId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ManufactureId"); + + b.HasIndex("WorkPieceId"); + + b.ToTable("ManufactureWorkPieces"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("ManufactureId") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.WorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("WorkPieceName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("WorkPieces"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureWorkPiece", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("WorkPieces") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.WorkPiece", "WorkPiece") + .WithMany("ManufactureWorkPieces") + .HasForeignKey("WorkPieceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Manufacture"); + + b.Navigation("WorkPiece"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null) + .WithMany("Orders") + .HasForeignKey("ProductId"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Navigation("Orders"); + + b.Navigation("WorkPieces"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.WorkPiece", b => + { + b.Navigation("ManufactureWorkPieces"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230227172536_InitMigration.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230227172536_InitMigration.cs new file mode 100644 index 0000000..c1fbbc3 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230227172536_InitMigration.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Manufactures", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ManufactureName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Manufactures", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "WorkPieces", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WorkPieceName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_WorkPieces", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ManufactureId = 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), + ProductId = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Manufactures_ProductId", + column: x => x.ProductId, + principalTable: "Manufactures", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "ManufactureWorkPieces", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ManufactureId = table.Column(type: "int", nullable: false), + WorkPieceId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ManufactureWorkPieces", x => x.Id); + table.ForeignKey( + name: "FK_ManufactureWorkPieces_Manufactures_ManufactureId", + column: x => x.ManufactureId, + principalTable: "Manufactures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ManufactureWorkPieces_WorkPieces_WorkPieceId", + column: x => x.WorkPieceId, + principalTable: "WorkPieces", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ManufactureWorkPieces_ManufactureId", + table: "ManufactureWorkPieces", + column: "ManufactureId"); + + migrationBuilder.CreateIndex( + name: "IX_ManufactureWorkPieces_WorkPieceId", + table: "ManufactureWorkPieces", + column: "WorkPieceId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ProductId", + table: "Orders", + column: "ProductId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ManufactureWorkPieces"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "WorkPieces"); + + migrationBuilder.DropTable( + name: "Manufactures"); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..931b2ae --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs @@ -0,0 +1,167 @@ +// +using System; +using BlacksmithWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(BlacksmithWorkshopDatabase))] + partial class BlacksmithWorkshopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Manufactures"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureWorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.Property("WorkPieceId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ManufactureId"); + + b.HasIndex("WorkPieceId"); + + b.ToTable("ManufactureWorkPieces"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("ManufactureId") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.WorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("WorkPieceName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("WorkPieces"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureWorkPiece", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("WorkPieces") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.WorkPiece", "WorkPiece") + .WithMany("ManufactureWorkPieces") + .HasForeignKey("WorkPieceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Manufacture"); + + b.Navigation("WorkPiece"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null) + .WithMany("Orders") + .HasForeignKey("ProductId"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Navigation("Orders"); + + b.Navigation("WorkPieces"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.WorkPiece", b => + { + b.Navigation("ManufactureWorkPieces"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs index fe1589b..82c98c6 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs @@ -1,20 +1,118 @@ -using BlacksmithWorkshopDataModels.Models; +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; using System; using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BlacksmithWorkshopDatabaseImplement.Models { - internal class Manufacture : IManufactureModel + public class Manufacture : IManufactureModel { - public string ManufactureName => throw new NotImplementedException(); + public int Id { get; set; } - public double Price => throw new NotImplementedException(); + [Required] + public string ManufactureName { get; set; } = string.Empty; - public Dictionary ManufactureWorkPieces => throw new NotImplementedException(); + [Required] + public double Price { get; set; } - public int Id => throw new NotImplementedException(); + public Dictionary? ManufactureWorkPieces = null; + + //это поле не будет "мапиться" в бд + [NotMapped] + public Dictionary _manufactureWorkPieces + { + get + { + if(ManufactureWorkPieces == null) + { + ManufactureWorkPieces = WorkPieces + .ToDictionary(recPC => recPC.WorkPieceId, recPC => (recPC.WorkPiece as IWorkPieceModel, recPC.Count)); + } + + return ManufactureWorkPieces; + } + } + + //для реализации связи многие ко многим с заготовками + [ForeignKey("ManufactureId")] + public virtual List WorkPieces { get; set; } = new(); + + [ForeignKey("ProductId")] + public virtual List Orders { get; set; } = new(); + + public static Manufacture Create(BlacksmithWorkshopDatabase context, ManufactureBindingModel model) + { + return new Manufacture() + { + Id = model.Id, + ManufactureName = model.ManufactureName, + Price = model.Price, + WorkPieces = model.ManufactureWorkPieces.Select(x => new ManufactureWorkPiece + { + WorkPiece = context.WorkPieces.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ManufactureBindingModel model) + { + ManufactureName = model.ManufactureName; + Price = model.Price; + } + + public ManufactureViewModel GetViewModel => new() + { + Id = Id, + ManufactureName = ManufactureName, + Price = Price, + ManufactureWorkPieces = ManufactureWorkPieces + }; + + Dictionary IManufactureModel.ManufactureWorkPieces => throw new NotImplementedException(); + + public void UpdateWorkPieces(BlacksmithWorkshopDatabase context, ManufactureBindingModel model) + { + var manufactureWorkPieces = context.ManufactureWorkPieces.Where(rec => rec.ManufactureId == model.Id).ToList(); + + if (manufactureWorkPieces != null && manufactureWorkPieces.Count > 0) + { + // удалили те, которых нет в модели + context.ManufactureWorkPieces.RemoveRange(manufactureWorkPieces.Where(rec => !model.ManufactureWorkPieces.ContainsKey(rec.ManufactureId))); + context.SaveChanges(); + + // обновили количество у существующих записей + foreach (var updateManufacture in manufactureWorkPieces) + { + updateManufacture.Count = model.ManufactureWorkPieces[updateManufacture.ManufactureId].Item2; + model.ManufactureWorkPieces.Remove(updateManufacture.ManufactureId); + } + + context.SaveChanges(); + } + + var manufacture = context.Manufactures.First(x => x.Id == Id); + + foreach (var pc in model.ManufactureWorkPieces) + { + context.ManufactureWorkPieces.Add(new ManufactureWorkPiece + { + Manufacture = manufacture, + WorkPiece = context.WorkPieces.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + + context.SaveChanges(); + } + + ManufactureWorkPieces = null; + } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/ManufactureWorkPiece.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/ManufactureWorkPiece.cs new file mode 100644 index 0000000..dbe90f6 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/ManufactureWorkPiece.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 BlacksmithWorkshopDatabaseImplement.Models +{ + public class ManufactureWorkPiece + { + public int Id { get; set; } + + [Required] + public int ManufactureId { get; set; } + + [Required] + public int WorkPieceId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual WorkPiece WorkPiece { get; set; } = new(); + + public virtual Manufacture Manufacture { get; set; } = new(); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..e3ad019 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs @@ -0,0 +1,71 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Enums; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int ManufactureId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + + return new Order() + { + Id = model.Id, + ManufactureId = model.ManufactureId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + ManufactureId = ManufactureId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/WorkPiece.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/WorkPiece.cs index 4702c6e..8ef7ca3 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/WorkPiece.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/WorkPiece.cs @@ -22,8 +22,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models [Required] public double Cost { get; set; } + //для реализации связи многие ко многим с изделиями [ForeignKey("WorkPieceId")] - public virtual List manufactureWorkPieces { get; set; } = new(); + public virtual List ManufactureWorkPieces { get; set; } = new(); public static WorkPiece? Create(WorkPieceBindingModel model) { From 0ce85a004c93f47be217acac6c0ba4c4fcceb171 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Mon, 27 Feb 2023 23:18:18 +0400 Subject: [PATCH 3/9] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlacksmithWorkshop/Program.cs | 2 +- .../BlacksmithWorkshopDatabase.cs | 2 +- .../Implements/WorkPieceStorage.cs | 103 ++++++++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs index e76d044..a42137a 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs @@ -1,7 +1,7 @@ using BlacksmithWorkshopBusinessLogic.BusinessLogic; using BlacksmithWorkshopContracts.BusinessLogicsContracts; using BlacksmithWorkshopContracts.StoragesContracts; -using BlacksmithWorkshopFileImplement.Implements; +using BlacksmithWorkshopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs index 98fe0e9..124313c 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs @@ -15,7 +15,7 @@ namespace BlacksmithWorkshopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"DataSource=CHESHIR\SQLEXPRESS;Initial Catalog=AbstractShopDatabaseFull;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"DataSource=LAPTOP-CFLH20EE\SQLEXPRESS;Initial Catalog=BlacksmithWorkshopDatabase;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs new file mode 100644 index 0000000..fe53ba7 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs @@ -0,0 +1,103 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + internal class WorkPieceStorage : IWorkPieceStorage + { + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + + return context.WorkPieces + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(WorkPieceSearchModel model) + { + if (string.IsNullOrEmpty(model.WorkPieceName)) + { + return new(); + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.WorkPieces + .Where(x => x.WorkPieceName.Contains(model.WorkPieceName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public WorkPieceViewModel? GetElement(WorkPieceSearchModel model) + { + if (string.IsNullOrEmpty(model.WorkPieceName) && !model.Id.HasValue) + { + return null; + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.WorkPieces + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.WorkPieceName) && x.WorkPieceName == model.WorkPieceName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public WorkPieceViewModel? Insert(WorkPieceBindingModel model) + { + var newWorkPiece = WorkPiece.Create(model); + + if (newWorkPiece == null) + { + return null; + } + + using var context = new BlacksmithWorkshopDatabase(); + context.WorkPieces.Add(newWorkPiece); + context.SaveChanges(); + + return newWorkPiece.GetViewModel; + } + + public WorkPieceViewModel? Update(WorkPieceBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var workPiece = context.WorkPieces.FirstOrDefault(x => x.Id == model.Id); + + if (workPiece == null) + { + return null; + } + + workPiece.Update(model); + context.SaveChanges(); + + return workPiece.GetViewModel; + } + + public WorkPieceViewModel? Delete(WorkPieceBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var element = context.WorkPieces.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.WorkPieces.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} From 65f5de994300c4279cbbb26b6fbf9f29b5733d2d Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Mon, 27 Feb 2023 23:51:07 +0400 Subject: [PATCH 4/9] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ManufactureStorage.cs | 127 ++++++++++++++++++ .../Implements/OrderStorage.cs | 125 +++++++++++++++++ .../Implements/WorkPieceStorage.cs | 2 +- 3 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ManufactureStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ManufactureStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ManufactureStorage.cs new file mode 100644 index 0000000..2273dda --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ManufactureStorage.cs @@ -0,0 +1,127 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class ManufactureStorage : IManufactureStorage + { + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + + return context.Manufactures + .Include(x => x.WorkPieces) + .ThenInclude(x => x.WorkPiece) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName)) + { + return new(); + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.Manufactures + .Include(x => x.WorkPieces) + .ThenInclude(x => x.WorkPiece) + .Where(x => x.ManufactureName.Contains(model.ManufactureName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ManufactureViewModel? GetElement(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName) && !model.Id.HasValue) + { + return null; + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.Manufactures + .Include(x => x.WorkPieces) + .ThenInclude(x => x.WorkPiece) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ManufactureName) && x.ManufactureName == model.ManufactureName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ManufactureViewModel? Insert(ManufactureBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var newManufacture = Manufacture.Create(context, model); + + if (newManufacture == null) + { + return null; + } + + context.Manufactures.Add(newManufacture); + context.SaveChanges(); + + return newManufacture.GetViewModel; + } + + public ManufactureViewModel? Update(ManufactureBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var manufacture = context.Manufactures.FirstOrDefault(rec => rec.Id == model.Id); + + if (manufacture == null) + { + return null; + } + + manufacture.Update(model); + context.SaveChanges(); + manufacture.UpdateWorkPieces(context, model); + transaction.Commit(); + + return manufacture.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ManufactureViewModel? Delete(ManufactureBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var element = context.Manufactures + .Include(x => x.WorkPieces) + .Include(x => x.Orders) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Manufactures.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..67dceeb --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,125 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var element = context.Orders + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new BlacksmithWorkshopDatabase(); + var element = context.Manufactures + .FirstOrDefault(x => x.Id == order.ManufactureId); + viewModel.ManufactureName = element.ManufactureName; + + return viewModel; + } + + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + + return context.Orders + .Select(x => new OrderViewModel + { + Id = x.Id, + ManufactureId = x.ManufactureId, + Count = x.Count, + Sum = x.Sum, + Status = x.Status, + DateCreate = x.DateCreate, + DateImplement = x.DateImplement, + //ManufactureName = x.Manufacture.ManufactureName //тут не будет работать + }) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + using var context = new BlacksmithWorkshopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return order.GetViewModel; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs index fe53ba7..ec2cc00 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs @@ -11,7 +11,7 @@ using System.Threading.Tasks; namespace BlacksmithWorkshopDatabaseImplement.Implements { - internal class WorkPieceStorage : IWorkPieceStorage + public class WorkPieceStorage : IWorkPieceStorage { public List GetFullList() { From 24d020220c20c1efb677ce5dd9c32c7978aa612e Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Mon, 27 Feb 2023 23:59:19 +0400 Subject: [PATCH 5/9] =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D0=BA=D0=B8=D0=B9=20(?= =?UTF-8?q?=D0=9D=D0=9E=20=D0=9E=D0=A7=D0=95=D0=9D=D0=AC=20=D0=92=D0=9D?= =?UTF-8?q?=D0=95=D0=97=D0=90=D0=9F=D0=9D=D0=AB=D0=99)=20=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D1=81!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlacksmithWorkshopDatabase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs index 124313c..d4e6e68 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs @@ -15,7 +15,7 @@ namespace BlacksmithWorkshopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"DataSource=LAPTOP-CFLH20EE\SQLEXPRESS;Initial Catalog=BlacksmithWorkshopDatabase;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-CFLH20EE\SQLEXPRESS;Initial Catalog=BlacksmithWorkshopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); From 0f46fbef9f7124463f9f4619d271e2fe9c6adf99 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Tue, 28 Feb 2023 00:13:54 +0400 Subject: [PATCH 6/9] =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D0=BA=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 2 +- .../BlacksmithWorkshopDatabaseImplement/Models/Order.cs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs index 67dceeb..8dfa776 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -85,7 +85,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements Status = x.Status, DateCreate = x.DateCreate, DateImplement = x.DateImplement, - //ManufactureName = x.Manufacture.ManufactureName //тут не будет работать + ManufactureName = x.Manufacture.ManufactureName }) .ToList(); } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs index e3ad019..dd91c74 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs @@ -27,6 +27,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models public DateTime? DateImplement { get; private set; } + //для передачи названия изделия + public virtual Manufacture Manufacture { get; set; } + public static Order? Create(OrderBindingModel model) { if (model == null) From 27384ba263fee5b8d5e68e433eb83d99290acd1a Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Tue, 28 Feb 2023 00:32:59 +0400 Subject: [PATCH 7/9] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs index 82c98c6..bac69ed 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs @@ -44,7 +44,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models [ForeignKey("ManufactureId")] public virtual List WorkPieces { get; set; } = new(); - [ForeignKey("ProductId")] + [ForeignKey("ManufactureId")] public virtual List Orders { get; set; } = new(); public static Manufacture Create(BlacksmithWorkshopDatabase context, ManufactureBindingModel model) From 4ec9fa5f308556ab77e3ea29576e32194cfc6efa Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Tue, 28 Feb 2023 08:36:02 +0400 Subject: [PATCH 8/9] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F=20=D0=BC=D0=B8=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20230228043436_SecondMigration.Designer.cs | 171 ++++++++++++++++++ .../20230228043436_SecondMigration.cs | 69 +++++++ ...BlacksmithWorkshopDatabaseModelSnapshot.cs | 13 +- 3 files changed, 247 insertions(+), 6 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230228043436_SecondMigration.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230228043436_SecondMigration.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230228043436_SecondMigration.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230228043436_SecondMigration.Designer.cs new file mode 100644 index 0000000..0cef030 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230228043436_SecondMigration.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using BlacksmithWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(BlacksmithWorkshopDatabase))] + [Migration("20230228043436_SecondMigration")] + partial class SecondMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Manufactures"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureWorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.Property("WorkPieceId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ManufactureId"); + + b.HasIndex("WorkPieceId"); + + b.ToTable("ManufactureWorkPieces"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("ManufactureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ManufactureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.WorkPiece", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("WorkPieceName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("WorkPieces"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureWorkPiece", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("WorkPieces") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.WorkPiece", "WorkPiece") + .WithMany("ManufactureWorkPieces") + .HasForeignKey("WorkPieceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Manufacture"); + + b.Navigation("WorkPiece"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("Orders") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Manufacture"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Navigation("Orders"); + + b.Navigation("WorkPieces"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.WorkPiece", b => + { + b.Navigation("ManufactureWorkPieces"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230228043436_SecondMigration.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230228043436_SecondMigration.cs new file mode 100644 index 0000000..c5e65a8 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230228043436_SecondMigration.cs @@ -0,0 +1,69 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + /// + public partial class SecondMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_Manufactures_ProductId", + table: "Orders"); + + migrationBuilder.DropIndex( + name: "IX_Orders_ProductId", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "ProductId", + table: "Orders"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ManufactureId", + table: "Orders", + column: "ManufactureId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_Manufactures_ManufactureId", + table: "Orders", + column: "ManufactureId", + principalTable: "Manufactures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_Manufactures_ManufactureId", + table: "Orders"); + + migrationBuilder.DropIndex( + name: "IX_Orders_ManufactureId", + table: "Orders"); + + migrationBuilder.AddColumn( + name: "ProductId", + table: "Orders", + type: "int", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ProductId", + table: "Orders", + column: "ProductId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_Manufactures_ProductId", + table: "Orders", + column: "ProductId", + principalTable: "Manufactures", + principalColumn: "Id"); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs index 931b2ae..54acdb6 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs @@ -88,9 +88,6 @@ namespace BlacksmithWorkshopDatabaseImplement.Migrations b.Property("ManufactureId") .HasColumnType("int"); - b.Property("ProductId") - .HasColumnType("int"); - b.Property("Status") .HasColumnType("int"); @@ -99,7 +96,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Migrations b.HasKey("Id"); - b.HasIndex("ProductId"); + b.HasIndex("ManufactureId"); b.ToTable("Orders"); }); @@ -145,9 +142,13 @@ namespace BlacksmithWorkshopDatabaseImplement.Migrations modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => { - b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null) + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") .WithMany("Orders") - .HasForeignKey("ProductId"); + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Manufacture"); }); modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => From b25a248c54999a5b9efb6080c05b43b2886ecb08 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Tue, 14 Mar 2023 11:09:37 +0400 Subject: [PATCH 9/9] =?UTF-8?q?=D0=9C=D0=B5=D0=BB=D0=BA=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20(=D0=BF=D0=BE=D1=87?= =?UTF-8?q?=D0=B5=D0=BC=D1=83-=D1=82=D0=BE=20=D0=BD=D0=B5=20=D0=BE=D1=82?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D0=BE=D1=81=D1=8C=20=D1=81?= =?UTF-8?q?=D1=80=D0=B0=D0=B7=D1=83).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/Manufacture.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs index bac69ed..742aa11 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs @@ -22,21 +22,21 @@ namespace BlacksmithWorkshopDatabaseImplement.Models [Required] public double Price { get; set; } - public Dictionary? ManufactureWorkPieces = null; + public Dictionary? _manufactureWorkPieces = null; //это поле не будет "мапиться" в бд [NotMapped] - public Dictionary _manufactureWorkPieces + public Dictionary ManufactureWorkPieces { get { - if(ManufactureWorkPieces == null) + if(_manufactureWorkPieces == null) { - ManufactureWorkPieces = WorkPieces + _manufactureWorkPieces = WorkPieces .ToDictionary(recPC => recPC.WorkPieceId, recPC => (recPC.WorkPiece as IWorkPieceModel, recPC.Count)); } - return ManufactureWorkPieces; + return _manufactureWorkPieces; } } @@ -112,7 +112,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models context.SaveChanges(); } - ManufactureWorkPieces = null; + _manufactureWorkPieces = null; } } }