diff --git a/PrecastConcretePlant/PrecastConcretePlant.sln b/PrecastConcretePlant/PrecastConcretePlant.sln index bed8fc1..e983ea1 100644 --- a/PrecastConcretePlant/PrecastConcretePlant.sln +++ b/PrecastConcretePlant/PrecastConcretePlant.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantBusines EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantListImplement", "PrecastConcretePlantListImplement\PrecastConcretePlantListImplement.csproj", "{471A4697-59C0-40C0-8B5E-8BEC01BD20BD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantFileImplement", "PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj", "{6251B4B7-A78D-458B-A926-FD0A8DF31076}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantFileImplement", "PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj", "{6251B4B7-A78D-458B-A926-FD0A8DF31076}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantDatabaseImplement", "PrecastConcretePlantDatabaseImplement\PrecastConcretePlantDatabaseImplement.csproj", "{4BD779AC-8B0F-4523-ADB5-29184D1030D2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {6251B4B7-A78D-458B-A926-FD0A8DF31076}.Debug|Any CPU.Build.0 = Debug|Any CPU {6251B4B7-A78D-458B-A926-FD0A8DF31076}.Release|Any CPU.ActiveCfg = Release|Any CPU {6251B4B7-A78D-458B-A926-FD0A8DF31076}.Release|Any CPU.Build.0 = Release|Any CPU + {4BD779AC-8B0F-4523-ADB5-29184D1030D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4BD779AC-8B0F-4523-ADB5-29184D1030D2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4BD779AC-8B0F-4523-ADB5-29184D1030D2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4BD779AC-8B0F-4523-ADB5-29184D1030D2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PrecastConcretePlant/PrecastConcretePlant/PrecastConcretePlantView.csproj b/PrecastConcretePlant/PrecastConcretePlant/PrecastConcretePlantView.csproj index 34aaa18..6ed204a 100644 --- a/PrecastConcretePlant/PrecastConcretePlant/PrecastConcretePlantView.csproj +++ b/PrecastConcretePlant/PrecastConcretePlant/PrecastConcretePlantView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +20,7 @@ + diff --git a/PrecastConcretePlant/PrecastConcretePlant/Program.cs b/PrecastConcretePlant/PrecastConcretePlant/Program.cs index d6086ea..8616518 100644 --- a/PrecastConcretePlant/PrecastConcretePlant/Program.cs +++ b/PrecastConcretePlant/PrecastConcretePlant/Program.cs @@ -1,8 +1,8 @@ -using PrecastConcretePlantFileImplement.Implements; +using PrecastConcretePlantDatabaseImplement.Implements; using PrecastConcretePlantBusinessLogic.BusinessLogic; using PrecastConcretePlantContracts.BusinessLogicsContracts; using PrecastConcretePlantContracts.StoragesContract; -using PrecastConcretePlantFileImplement; +using PrecastConcretePlantDatabaseImplement; using PrecastConcretePlantView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Component.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Component.cs new file mode 100644 index 0000000..4a5259b --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Component.cs @@ -0,0 +1,57 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace PrecastConcretePlantDatabaseImplement.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 ReinforcedComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } + +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ComponentStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ComponentStorage.cs new file mode 100644 index 0000000..1da7661 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ComponentStorage.cs @@ -0,0 +1,80 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContract; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDatabaseImplement.Models; + +namespace PrecastConcretePlantDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new PrecastConcretePlantDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new PrecastConcretePlantDatabase(); + 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 PrecastConcretePlantDatabase(); + 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 PrecastConcretePlantDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + 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 PrecastConcretePlantDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230306170846_InitMigration.Designer.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230306170846_InitMigration.Designer.cs new file mode 100644 index 0000000..d3f2cde --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230306170846_InitMigration.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PrecastConcretePlantDatabaseImplement; + +#nullable disable + +namespace PrecastConcretePlantDatabaseImplement.Migrations +{ + [DbContext(typeof(PrecastConcretePlantDatabase))] + [Migration("20230306170846_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("PrecastConcretePlantDatabaseImplement.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("PrecastConcretePlantDatabaseImplement.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("ReinforcedId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ReinforcedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Reinforceds"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("ReinforcedComponents"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + .WithMany("Orders") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Reinforced"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component") + .WithMany("ReinforcedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinfordced") + .WithMany("Components") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Reinfordced"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + { + b.Navigation("ReinforcedComponents"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230306170846_InitMigration.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230306170846_InitMigration.cs new file mode 100644 index 0000000..2cbfdb7 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230306170846_InitMigration.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PrecastConcretePlantDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Reinforceds", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ReinforcedName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Reinforceds", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ReinforcedId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Reinforceds_ReinforcedId", + column: x => x.ReinforcedId, + principalTable: "Reinforceds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ReinforcedComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ReinforcedId = 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_ReinforcedComponents", x => x.Id); + table.ForeignKey( + name: "FK_ReinforcedComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ReinforcedComponents_Reinforceds_ReinforcedId", + column: x => x.ReinforcedId, + principalTable: "Reinforceds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ReinforcedId", + table: "Orders", + column: "ReinforcedId"); + + migrationBuilder.CreateIndex( + name: "IX_ReinforcedComponents_ComponentId", + table: "ReinforcedComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_ReinforcedComponents_ReinforcedId", + table: "ReinforcedComponents", + column: "ReinforcedId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "ReinforcedComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Reinforceds"); + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230509090707_Create_shop.Designer.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230509090707_Create_shop.Designer.cs new file mode 100644 index 0000000..548433f --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230509090707_Create_shop.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PrecastConcretePlantDatabaseImplement; + +#nullable disable + +namespace PrecastConcretePlantDatabaseImplement.Migrations +{ + [DbContext(typeof(PrecastConcretePlantDatabase))] + [Migration("20230509090707_Create_shop")] + partial class Create_shop + { + /// + 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("PrecastConcretePlantDatabaseImplement.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("PrecastConcretePlantDatabaseImplement.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("ReinforcedId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ReinforcedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Reinforceds"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("ReinforcedComponents"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountReinforceds") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.ShopReinforced", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ReinforcedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopReinforceds"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + .WithMany("Orders") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Reinforced"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component") + .WithMany("ReinforcedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinfordced") + .WithMany("Components") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Reinfordced"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.ShopReinforced", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + .WithMany() + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PrecastConcretePlantDatabaseImplement.Shop", "Shop") + .WithMany("ShopReinforceds") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Reinforced"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + { + b.Navigation("ReinforcedComponents"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Shop", b => + { + b.Navigation("ShopReinforceds"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230509090707_Create_shop.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230509090707_Create_shop.cs new file mode 100644 index 0000000..555994c --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230509090707_Create_shop.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PrecastConcretePlantDatabaseImplement.Migrations +{ + /// + public partial class Create_shop : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + MaxCountReinforceds = table.Column(type: "int", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopReinforceds", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ReinforcedId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopReinforceds", x => x.Id); + table.ForeignKey( + name: "FK_ShopReinforceds_Reinforceds_ReinforcedId", + column: x => x.ReinforcedId, + principalTable: "Reinforceds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopReinforceds_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopReinforceds_ReinforcedId", + table: "ShopReinforceds", + column: "ReinforcedId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopReinforceds_ShopId", + table: "ShopReinforceds", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopReinforceds"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs new file mode 100644 index 0000000..03d891f --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs @@ -0,0 +1,245 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PrecastConcretePlantDatabaseImplement; + +#nullable disable + +namespace PrecastConcretePlantDatabaseImplement.Migrations +{ + [DbContext(typeof(PrecastConcretePlantDatabase))] + partial class PrecastConcretePlantDatabaseModelSnapshot : 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("PrecastConcretePlantDatabaseImplement.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", (string)null); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.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("ReinforcedId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("Orders", (string)null); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ReinforcedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Reinforceds", (string)null); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("ReinforcedComponents", (string)null); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountReinforceds") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops", (string)null); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.ShopReinforced", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ReinforcedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopReinforceds", (string)null); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + .WithMany("Orders") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Reinforced"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component") + .WithMany("ReinforcedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinfordced") + .WithMany("Components") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Reinfordced"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.ShopReinforced", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + .WithMany() + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PrecastConcretePlantDatabaseImplement.Shop", "Shop") + .WithMany("ShopReinforceds") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Reinforced"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + { + b.Navigation("ReinforcedComponents"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Shop", b => + { + b.Navigation("ShopReinforceds"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Order.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Order.cs new file mode 100644 index 0000000..850e265 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Order.cs @@ -0,0 +1,87 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Enums; +using PrecastConcretePlantDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.Reflection; + + +namespace PrecastConcretePlantDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + [Required] + public int ReinforcedId { get; private set; } + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } + + [Required] + public DateTime DateCreate { get; private set; } + + public DateTime? DateImplement { get; private set; } + + public Reinforced Reinforced { get; private set; } + + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + ReinforcedId = model.ReinforcedId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + ReinforcedId = model.ReinforcedId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + Id = model.Id; + } + public OrderViewModel GetViewModel + { + get + { + var context = new PrecastConcretePlantDatabase(); + return new() + { + ReinforcedName = Reinforced?.ReinforcedName ?? string.Empty, + ReinforcedId = ReinforcedId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + } + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/OrderStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/OrderStorage.cs new file mode 100644 index 0000000..4c9df08 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/OrderStorage.cs @@ -0,0 +1,77 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContract; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDatabaseImplement.Models; + +namespace PrecastConcretePlantDatabaseImplement +{ + public class OrderStorage : IOrderStorage + { + + public OrderViewModel? GetElement(OrderSearchModel model) + { + using var context = new PrecastConcretePlantDatabase(); + if (!model.Id.HasValue) + { + return null; + } + return context.Orders.Include(x => x.Reinforced).FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + var result = GetElement(model); + return result != null ? new() { result } : new(); + } + + public List GetFullList() + { + using var context = new PrecastConcretePlantDatabase(); + return context.Orders + .Include(x => x.Reinforced) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new PrecastConcretePlantDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + var order = context.Orders.Include(x => x.Reinforced).FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + var element = context.Orders.Include(x => x.Reinforced).FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs new file mode 100644 index 0000000..fc33c11 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantDatabaseImplement.Models; + +namespace PrecastConcretePlantDatabaseImplement +{ + public class PrecastConcretePlantDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + // Перед работой необходимо установить SQL Express + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@" + Server = localhost\SQLEXPRESS; + Initial Catalog = PrecastConcretePlantDatabaseDatabaseFull; + Integrated Security = True; + MultipleActiveResultSets = True; + TrustServerCertificate = True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Reinforceds { set; get; } + public virtual DbSet ReinforcedComponents { set; get; } + public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopReinforceds { set; get; } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabaseImplement.csproj b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabaseImplement.csproj new file mode 100644 index 0000000..77c9e22 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Reinforced.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Reinforced.cs new file mode 100644 index 0000000..fd15074 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Reinforced.cs @@ -0,0 +1,99 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Xml.Linq; + +namespace PrecastConcretePlantDatabaseImplement.Models +{ + public class Reinforced : IReinforcedModel + { + public int Id { get; set; } + [Required] + public string ReinforcedName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _reinforcedComponents = null; + + [NotMapped] + public Dictionary ReinforcedComponents + { + get + { + if (_reinforcedComponents == null) + { + _reinforcedComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _reinforcedComponents; + } + } + + [ForeignKey("ReinforcedId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("ReinforcedId")] + public virtual List Orders { get; set; } = new(); + + public static Reinforced Create(PrecastConcretePlantDatabase context, ReinforcedBindingModel model) + { + return new Reinforced() + { + Id = model.Id, + ReinforcedName = model.ReinforcedName, + Price = model.Price, + Components = model.ReinforcedComponents.Select(x => new ReinforcedComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ReinforcedBindingModel model) + { + ReinforcedName = model.ReinforcedName; + Price = model.Price; + } + public ReinforcedViewModel GetViewModel => new() + { + Id = Id, + ReinforcedName = ReinforcedName, + Price = Price, + ReinforcedComponents = ReinforcedComponents + }; + public void UpdateComponents(PrecastConcretePlantDatabase context, ReinforcedBindingModel model) + { + var reinforcedComponents = context.ReinforcedComponents + .Where(rec => rec.ReinforcedId == model.Id) + .ToList(); + if (reinforcedComponents != null && reinforcedComponents.Count > 0) + { // удалили те, которых нет в модели + context.ReinforcedComponents + .RemoveRange(reinforcedComponents + .Where(rec => !model.ReinforcedComponents + .ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in reinforcedComponents.Where(x => model.ReinforcedComponents.ContainsKey(x.ComponentId))) + { + updateComponent.Count = model.ReinforcedComponents[updateComponent.ComponentId].Item2; + model.ReinforcedComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var reinfordced = context.Reinforceds.First(x => x.Id == Id); + foreach (var pc in model.ReinforcedComponents) + { + context.ReinforcedComponents.Add(new ReinforcedComponent + { + Reinfordced = reinfordced, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _reinforcedComponents = null; + } + } +} \ No newline at end of file diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ReinforcedComponent.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ReinforcedComponent.cs new file mode 100644 index 0000000..8779ccb --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ReinforcedComponent.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace PrecastConcretePlantDatabaseImplement.Models +{ + public class ReinforcedComponent + { + public int Id { get; set; } + [Required] + public int ReinforcedId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Reinforced Reinfordced { get; set; } = new(); + } + +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ReinforcedStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ReinforcedStorage.cs new file mode 100644 index 0000000..243887c --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ReinforcedStorage.cs @@ -0,0 +1,103 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContract; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace PrecastConcretePlantDatabaseImplement +{ + public class ReinforcedStorage : IReinforcedStorage + { + public ReinforcedViewModel? Delete(ReinforcedBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + var element = context.Reinforceds.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Reinforceds.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ReinforcedViewModel? GetElement(ReinforcedSearchModel model) + { + if (string.IsNullOrEmpty(model.ReinforcedName) && !model.Id.HasValue) + { + return null; + } + using var context = new PrecastConcretePlantDatabase(); + return context.Reinforceds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault + (x => (!string.IsNullOrEmpty(model.ReinforcedName) && x.ReinforcedName == model.ReinforcedName) || + (model.Id.HasValue && x.Id == model.Id) + )?.GetViewModel; + } + + public List GetFilteredList(ReinforcedSearchModel model) + { + if (string.IsNullOrEmpty(model.ReinforcedName)) + { + return new(); + } + using var context = new PrecastConcretePlantDatabase(); + return context.Reinforceds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Select(x => x.GetViewModel) + .Where(x => x.ReinforcedName.Contains(model.ReinforcedName)) + .ToList(); + } + + public List GetFullList() + { + using var context = new PrecastConcretePlantDatabase(); + return context.Reinforceds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ReinforcedViewModel? Insert(ReinforcedBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + var newReinforced = Reinforced.Create(context, model); + if (newReinforced == null) + { + return null; + } + context.Reinforceds.Add(newReinforced); + context.SaveChanges(); + return newReinforced.GetViewModel; + } + + public ReinforcedViewModel? Update(ReinforcedBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var reinforced = context.Reinforceds.FirstOrDefault(x => x.Id == model.Id); + if (reinforced == null) + { + return null; + } + reinforced.Update(model); + context.SaveChanges(); + reinforced.UpdateComponents(context, model); + transaction.Commit(); + return reinforced.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Shop.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Shop.cs new file mode 100644 index 0000000..71664eb --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Shop.cs @@ -0,0 +1,115 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using PrecastConcretePlantDataModels.Models; + +namespace PrecastConcretePlantDatabaseImplement +{ + public class Shop : IShopModel + { + [Required] + public string Name { get; private set; } = string.Empty; + + [Required] + public string Address { get; private set; } = string.Empty; + + [Required] + public int MaxCountReinforceds { get; private set; } + + public DateTime DateOpening { get; private set; } + + private Dictionary? _cachedReinforceds = null; + [NotMapped] + public Dictionary Reinforceds + { + get + { + if (_cachedReinforceds == null) + { + using var context = new PrecastConcretePlantDatabase(); + _cachedReinforceds = ShopReinforceds + .ToDictionary(x => x.ReinforcedId, x => (context.Reinforceds + .FirstOrDefault(y => y.Id == x.ReinforcedId)! as IReinforcedModel, x.Count)); + } + return _cachedReinforceds; + } + } + + public int Id { get; private set; } + + [ForeignKey("ShopId")] + public virtual List ShopReinforceds { get; set; } = new(); + + public static Shop? Create(PrecastConcretePlantDatabase context, ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + DateOpening = model.DateOpening, + MaxCountReinforceds = model.MaxCountReinforceds, + ShopReinforceds = model.Reinforceds.Select(x => new ShopReinforced + { + Reinforced = context.Reinforceds.FirstOrDefault(y => y.Id == x.Key)!, + Count = x.Value.Item2, + }).ToList() + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + DateOpening = model.DateOpening; + MaxCountReinforceds = model.MaxCountReinforceds; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + Reinforceds = Reinforceds, + DateOpening = DateOpening, + MaxCountReinforceds = MaxCountReinforceds, + }; + + public void UpdateReinforceds(PrecastConcretePlantDatabase context, ShopBindingModel model) + { + var shopReinforceds = context.ShopReinforceds + .Where(rec => rec.ShopId == model.Id) + .ToList(); + // удалили те, которых нет в модели + if (shopReinforceds != null && shopReinforceds.Count > 0) + { + context.ShopReinforceds + .RemoveRange(shopReinforceds + .Where(rec => !model.Reinforceds + .ContainsKey(rec.ReinforcedId))); + // обновили количество у существующих записей + foreach (var updateReinforced in shopReinforceds.Where(x => model.Reinforceds.ContainsKey(x.ReinforcedId))) + { + updateReinforced.Count = model.Reinforceds[updateReinforced.ReinforcedId].Item2; + model.Reinforceds.Remove(updateReinforced.ReinforcedId); + } + } + var shop = context.Shops.First(x => x.Id == model.Id); + shop.ShopReinforceds.AddRange(model.Reinforceds.Select(x => new ShopReinforced + { + Reinforced = context.Reinforceds.First(y => y.Id == x.Key), + Count = x.Value.Item2, + }).Except(shopReinforceds ?? new())); + context.SaveChanges(); + _cachedReinforceds = null; + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ShopReinforced.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ShopReinforced.cs new file mode 100644 index 0000000..715a630 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ShopReinforced.cs @@ -0,0 +1,20 @@ +using PrecastConcretePlantDatabaseImplement.Models; +using System.ComponentModel.DataAnnotations; + + +namespace PrecastConcretePlantDatabaseImplement +{ + public class ShopReinforced + { + public int Id { get; set; } + [Required] + public int ReinforcedId { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + public virtual Reinforced Reinforced { get; set; } = new(); + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ShopStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ShopStorage.cs new file mode 100644 index 0000000..794beb5 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/ShopStorage.cs @@ -0,0 +1,136 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContract; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; + +namespace PrecastConcretePlantDatabaseImplement +{ + public class ShopStorage : IShopStorage + { + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + 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 (!model.Id.HasValue) + { + return null; + } + using var context = new PrecastConcretePlantDatabase(); + return context.Shops + .Include(x => x.ShopReinforceds) + .ThenInclude(x => x.Reinforced) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new PrecastConcretePlantDatabase(); + return context.Shops + .Include(x => x.ShopReinforceds) + .ThenInclude(x => x.Reinforced) + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new PrecastConcretePlantDatabase(); + return context.Shops + .Include(x => x.ShopReinforceds) + .ThenInclude(x => x.Reinforced) + .Select(shop => shop.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.Name == newShop.Name)) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + context.Shops.Add(newShop); + context.SaveChanges(); + transaction.Commit(); + return newShop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + shop.UpdateReinforceds(context, model); + context.SaveChanges(); + return shop.GetViewModel; + } + + public bool SellReinforceds(IReinforcedModel reinforced, int needCount) + { + using var context = new PrecastConcretePlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + foreach (var sp in context.ShopReinforceds.Where(x => x.ReinforcedId == reinforced.Id)) + { + var res = Math.Min(needCount, sp.Count); + sp.Count -= res; + needCount -= res; + if (sp.Count == 0) // Изделия больше нет в магазине, значит удаляем его + { + context.ShopReinforceds.Remove(sp); + } + if (needCount == 0) // Нельзя коммитить изменения в цикле, что использует контекст, поэтому выходим + { + break; + } + } + if (needCount == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + else + { + transaction.Rollback(); + } + return needCount == 0; + } + } +}