From 92b39905812f1cb9caae94969bb22bd809b1d5fc Mon Sep 17 00:00:00 2001 From: K Date: Thu, 4 Apr 2024 20:21:21 +0300 Subject: [PATCH 1/2] lab3 --- PrecastConcretePlant/PrecastConcretePlant.sln | 8 +- .../PrecastConcretePlantBusinessLogic.csproj | 6 + .../PrecastConcretePlantContracts.csproj | 6 + .../Models/IOrderModel.cs | 1 - .../PrecastConcretePlantDataModels.csproj | 6 + .../Implements/ComponentStorage.cs | 84 +++++++++ .../Implements/OrderStorage.cs | 97 ++++++++++ .../Implements/ReinforcedStorage.cs | 108 +++++++++++ .../20240404165842_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240404165842_InitialCreate.cs | 125 +++++++++++++ ...ecastConcretePlantDataBaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 65 +++++++ .../Models/Order.cs | 77 ++++++++ .../Models/Reinforced.cs | 109 +++++++++++ .../Models/ReinforcedComponent.cs | 27 +++ .../PrecastConcretePlantDatabase.cs | 28 +++ ...ecastConcretePlantDatabaseImplement.csproj | 25 +++ .../PrecastConcretePlantFileImplement.csproj | 13 +- .../PrecastConcretePlantListImplement.csproj | 7 + .../PrecastConcretePlantView.csproj | 14 +- .../PrecastConcretePlantView/Program.cs | 2 +- 21 files changed, 1141 insertions(+), 6 deletions(-) create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/OrderStorage.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ReinforcedStorage.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.Designer.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDataBaseModelSnapshot.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Component.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Order.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Reinforced.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ReinforcedComponent.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabaseImplement.csproj diff --git a/PrecastConcretePlant/PrecastConcretePlant.sln b/PrecastConcretePlant/PrecastConcretePlant.sln index eb34c37..0cfc01e 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", "{432CDD30-04CE-4505-999C-1FC85C2220B1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantFileImplement", "PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj", "{0260BB94-674A-490D-9B8F-0525FBD520A1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantFileImplement", "PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj", "{0260BB94-674A-490D-9B8F-0525FBD520A1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantDatabaseImplement", "PrecastConcretePlantDatabaseImplement\PrecastConcretePlantDatabaseImplement.csproj", "{5F09EAA0-7D95-4EE4-B42D-28FA4843FC18}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {0260BB94-674A-490D-9B8F-0525FBD520A1}.Debug|Any CPU.Build.0 = Debug|Any CPU {0260BB94-674A-490D-9B8F-0525FBD520A1}.Release|Any CPU.ActiveCfg = Release|Any CPU {0260BB94-674A-490D-9B8F-0525FBD520A1}.Release|Any CPU.Build.0 = Release|Any CPU + {5F09EAA0-7D95-4EE4-B42D-28FA4843FC18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5F09EAA0-7D95-4EE4-B42D-28FA4843FC18}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5F09EAA0-7D95-4EE4-B42D-28FA4843FC18}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5F09EAA0-7D95-4EE4-B42D-28FA4843FC18}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PrecastConcretePlant/PrecastConcretePlantBusinessLogic/PrecastConcretePlantBusinessLogic.csproj b/PrecastConcretePlant/PrecastConcretePlantBusinessLogic/PrecastConcretePlantBusinessLogic.csproj index d47aac7..678cc93 100644 --- a/PrecastConcretePlant/PrecastConcretePlantBusinessLogic/PrecastConcretePlantBusinessLogic.csproj +++ b/PrecastConcretePlant/PrecastConcretePlantBusinessLogic/PrecastConcretePlantBusinessLogic.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/PrecastConcretePlant/PrecastConcretePlantContracts/PrecastConcretePlantContracts.csproj b/PrecastConcretePlant/PrecastConcretePlantContracts/PrecastConcretePlantContracts.csproj index 80977c8..8ba51d6 100644 --- a/PrecastConcretePlant/PrecastConcretePlantContracts/PrecastConcretePlantContracts.csproj +++ b/PrecastConcretePlant/PrecastConcretePlantContracts/PrecastConcretePlantContracts.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/PrecastConcretePlant/PrecastConcretePlantDataModels/Models/IOrderModel.cs b/PrecastConcretePlant/PrecastConcretePlantDataModels/Models/IOrderModel.cs index 50d8b16..db89e4e 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDataModels/Models/IOrderModel.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDataModels/Models/IOrderModel.cs @@ -10,7 +10,6 @@ namespace PrecastConcretePlantDataModels.Models public interface IOrderModel : IId { int ReinforcedId { get; } - string ReinforcedName { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/PrecastConcretePlant/PrecastConcretePlantDataModels/PrecastConcretePlantDataModels.csproj b/PrecastConcretePlant/PrecastConcretePlantDataModels/PrecastConcretePlantDataModels.csproj index 7b8c1c6..0af6a0e 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDataModels/PrecastConcretePlantDataModels.csproj +++ b/PrecastConcretePlant/PrecastConcretePlantDataModels/PrecastConcretePlantDataModels.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ComponentStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..9bfd4fd --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,84 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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/Implements/OrderStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..ca07814 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,97 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new PrecastConcretePlantDataBase(); + return context.Orders + .Include(x => x.Reinforced) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new PrecastConcretePlantDataBase(); + return context.Orders + .Include(x => x.Reinforced) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel).ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new PrecastConcretePlantDataBase(); + return context.Orders + .Include(x => x.Reinforced) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new PrecastConcretePlantDataBase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Reinforced) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new PrecastConcretePlantDataBase(); + var order = context.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders + .Include(x => x.Reinforced) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new PrecastConcretePlantDataBase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + var deletedElement = context.Orders + .Include(x => x.Reinforced) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ReinforcedStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ReinforcedStorage.cs new file mode 100644 index 0000000..8040823 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ReinforcedStorage.cs @@ -0,0 +1,108 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDatabaseImplement.Implements +{ + public class ReinforcedStorage : IReinforcedStorage + { + public List GetFullList() + { + using var context = new PrecastConcretePlantDataBase(); + return context.Reinforceds + .Include(x => x.Components)//подтягивание зависимостей вместе с исходной записью(тот же join) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + 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) + .Where(x => x.ReinforcedName.Contains(model.ReinforcedName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + 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 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(rec => + rec.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; + } + } + public ReinforcedViewModel? Delete(ReinforcedBindingModel model) + { + using var context = new PrecastConcretePlantDataBase(); + var element = context.Reinforceds + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Reinforceds.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.Designer.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.Designer.cs new file mode 100644 index 0000000..482e1e4 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.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("20240404165842_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("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", "Reinforced") + .WithMany("Components") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Reinforced"); + }); + + 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/20240404165842_InitialCreate.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.cs new file mode 100644 index 0000000..cd4beed --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PrecastConcretePlantDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "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"), + 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), + ReinforcedId = table.Column(type: "int", nullable: false) + }, + 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/PrecastConcretePlantDataBaseModelSnapshot.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDataBaseModelSnapshot.cs new file mode 100644 index 0000000..49f864c --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDataBaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +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", "8.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", "Reinforced") + .WithMany("Components") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Reinforced"); + }); + + 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/Models/Component.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..bf70c0e --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Component.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PrecastConcretePlantDataModels.Models; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; + +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/Models/Order.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..026b549 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Order.cs @@ -0,0 +1,77 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Enums; +using PrecastConcretePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { 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; } + + [Required] + public int ReinforcedId { get; private set; } + + public virtual Reinforced Reinforced { get; set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + ReinforcedId = model.ReinforcedId, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + ReinforcedId = ReinforcedId, + ReinforcedName = Reinforced.ReinforcedName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Reinforced.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Reinforced.cs new file mode 100644 index 0000000..33a4d2e --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Reinforced.cs @@ -0,0 +1,109 @@ +using PrecastConcretePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; + +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) + { + updateComponent.Count = + model.ReinforcedComponents[updateComponent.ComponentId].Item2; + model.ReinforcedComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Reinforced = context.Reinforceds.First(x => x.Id == Id); + foreach (var pc in model.ReinforcedComponents) + { + context.ReinforcedComponents.Add(new ReinforcedComponent + { + Reinforced = Reinforced, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _ReinforcedComponents = null; + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ReinforcedComponent.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ReinforcedComponent.cs new file mode 100644 index 0000000..ccf8a49 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ReinforcedComponent.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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 Reinforced { get; set; } = new(); + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs new file mode 100644 index 0000000..ee2f033 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDatabaseImplement +{ + public class PrecastConcretePlantDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=PRETTYNAME;Initial Catalog=PrecastConcretePlantDatabase;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; } + + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabaseImplement.csproj b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabaseImplement.csproj new file mode 100644 index 0000000..6e56921 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabaseImplement.csproj @@ -0,0 +1,25 @@ + + + + net8.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + diff --git a/PrecastConcretePlant/PrecastConcretePlantFileImplement/PrecastConcretePlantFileImplement.csproj b/PrecastConcretePlant/PrecastConcretePlantFileImplement/PrecastConcretePlantFileImplement.csproj index 9e035ab..9c63de4 100644 --- a/PrecastConcretePlant/PrecastConcretePlantFileImplement/PrecastConcretePlantFileImplement.csproj +++ b/PrecastConcretePlant/PrecastConcretePlantFileImplement/PrecastConcretePlantFileImplement.csproj @@ -6,9 +6,20 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + - + diff --git a/PrecastConcretePlant/PrecastConcretePlantListImplement/PrecastConcretePlantListImplement.csproj b/PrecastConcretePlant/PrecastConcretePlantListImplement/PrecastConcretePlantListImplement.csproj index 91e38df..6e56921 100644 --- a/PrecastConcretePlant/PrecastConcretePlantListImplement/PrecastConcretePlantListImplement.csproj +++ b/PrecastConcretePlant/PrecastConcretePlantListImplement/PrecastConcretePlantListImplement.csproj @@ -7,6 +7,13 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/PrecastConcretePlant/PrecastConcretePlantView/PrecastConcretePlantView.csproj b/PrecastConcretePlant/PrecastConcretePlantView/PrecastConcretePlantView.csproj index 593d673..8327540 100644 --- a/PrecastConcretePlant/PrecastConcretePlantView/PrecastConcretePlantView.csproj +++ b/PrecastConcretePlant/PrecastConcretePlantView/PrecastConcretePlantView.csproj @@ -2,13 +2,23 @@ WinExe - net8.0-windows + net8.0-windows7.0 enable true enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,7 +26,7 @@ - + diff --git a/PrecastConcretePlant/PrecastConcretePlantView/Program.cs b/PrecastConcretePlant/PrecastConcretePlantView/Program.cs index 4de5eb5..d5db612 100644 --- a/PrecastConcretePlant/PrecastConcretePlantView/Program.cs +++ b/PrecastConcretePlant/PrecastConcretePlantView/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using PrecastConcretePlantBusinessLogic.BusinessLogics; using PrecastConcretePlantContracts.BusinessLogicsContracts; using PrecastConcretePlantContracts.StoragesContracts; -using PrecastConcretePlantFileImplement.Implements; +using PrecastConcretePlantDatabaseImplement.Implements; using System; namespace PrecastConcretePlantView -- 2.25.1 From 29a989cc6c9ebc4843dd01c4bb70f37d7e6d78d0 Mon Sep 17 00:00:00 2001 From: K Date: Fri, 17 May 2024 00:03:34 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ComponentStorage.cs | 4 +- .../Implements/OrderStorage.cs | 4 +- .../Implements/ReinforcedStorage.cs | 4 +- .../Implements/ShopStorage.cs | 147 ++++++++++ .../20240404165842_InitialCreate.Designer.cs | 26 +- .../20240404165842_InitialCreate.cs | 2 +- .../20240516193152_initial.Designer.cs | 250 ++++++++++++++++++ .../Migrations/20240516193152_initial.cs | 78 ++++++ ...ecastConcretePlantDataBaseModelSnapshot.cs | 105 +++++++- .../Models/Component.cs | 2 +- .../Models/Order.cs | 2 +- .../Models/Reinforced.cs | 5 +- .../Models/ReinforcedComponent.cs | 2 +- .../Models/Shop.cs | 110 ++++++++ .../Models/ShopReinforced.cs | 22 ++ .../PrecastConcretePlantDatabase.cs | 9 +- .../PrecastConcretePlantView.csproj | 2 +- .../PrecastConcretePlantView/Program.cs | 3 + 18 files changed, 735 insertions(+), 42 deletions(-) create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240516193152_initial.Designer.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240516193152_initial.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs create mode 100644 PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ShopReinforced.cs diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ComponentStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ComponentStorage.cs index 9bfd4fd..c7697d6 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ComponentStorage.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ComponentStorage.cs @@ -2,14 +2,14 @@ using PrecastConcretePlantContracts.SearchModels; using PrecastConcretePlantContracts.StoragesContracts; using PrecastConcretePlantContracts.ViewModels; -using PrecastConcretePlantDatabaseImplement.Models; +using PrecastConcretePlantDataBaseImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PrecastConcretePlantDatabaseImplement.Implements +namespace PrecastConcretePlantDataBaseImplement.Implements { public class ComponentStorage : IComponentStorage { diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/OrderStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/OrderStorage.cs index ca07814..6783f5a 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/OrderStorage.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/OrderStorage.cs @@ -3,14 +3,14 @@ using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.SearchModels; using PrecastConcretePlantContracts.StoragesContracts; using PrecastConcretePlantContracts.ViewModels; -using PrecastConcretePlantDatabaseImplement.Models; +using PrecastConcretePlantDataBaseImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PrecastConcretePlantDatabaseImplement.Implements +namespace PrecastConcretePlantDataBaseImplement.Implements { public class OrderStorage : IOrderStorage { diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ReinforcedStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ReinforcedStorage.cs index 8040823..0d99201 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ReinforcedStorage.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ReinforcedStorage.cs @@ -3,7 +3,7 @@ using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.SearchModels; using PrecastConcretePlantContracts.StoragesContracts; using PrecastConcretePlantContracts.ViewModels; -using PrecastConcretePlantDatabaseImplement.Models; +using PrecastConcretePlantDataBaseImplement.Models; using System; using System.Collections.Generic; using System.Diagnostics; @@ -11,7 +11,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PrecastConcretePlantDatabaseImplement.Implements +namespace PrecastConcretePlantDataBaseImplement.Implements { public class ReinforcedStorage : IReinforcedStorage { diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..68f0da4 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,147 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataBaseImplement.Models; +using PrecastConcretePlantDataModels.Models; +using Microsoft.EntityFrameworkCore; + +namespace PrecastConcretePlantDataBaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new PrecastConcretePlantDataBase(); + return context.Shops + .Include(x => x.Reinforceds) + .ThenInclude(x => x.Reinforced) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new PrecastConcretePlantDataBase(); + return context.Shops + .Include(x => x.Reinforceds) + .ThenInclude(x => x.Reinforced) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new PrecastConcretePlantDataBase(); + return context.Shops + .Include(x => x.Reinforceds) + .ThenInclude(x => x.Reinforced) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new PrecastConcretePlantDataBase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new PrecastConcretePlantDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateReinforceds(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new PrecastConcretePlantDataBase(); + var element = context.Shops + .Include(x => x.Reinforceds) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + //продажа изделий + public bool MakeSale(IReinforcedModel model, int count) + { + using var context = new PrecastConcretePlantDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var shop in context.Shops.Include(x => x.Reinforceds).ThenInclude(x => x.Reinforced) + .Where(x => x.Reinforceds.Any(x => x.ReinforcedId == model.Id)) + .ToList()) + { + var reinforced = shop.ShopReinforceds[model.Id]; + int min = Math.Min(reinforced.Item2, count); + if (min == reinforced.Item2) + { + shop.ShopReinforceds.Remove(model.Id); + } + else + { + shop.ShopReinforceds[model.Id] = (reinforced.Item1, reinforced.Item2 - min); + } + shop.UpdateReinforceds(context, new() { Id = shop.Id, ShopReinforceds = shop.ShopReinforceds }); + count -= min; + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.Designer.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.Designer.cs index 482e1e4..15d6415 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.Designer.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.Designer.cs @@ -5,11 +5,11 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using PrecastConcretePlantDatabaseImplement; +using PrecastConcretePlantDataBaseImplement; #nullable disable -namespace PrecastConcretePlantDatabaseImplement.Migrations +namespace PrecastConcretePlantDataBaseImplement.Migrations { [DbContext(typeof(PrecastConcretePlantDataBase))] [Migration("20240404165842_InitialCreate")] @@ -25,7 +25,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Component", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -45,7 +45,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.ToTable("Components"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Order", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -78,7 +78,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.ToTable("Orders"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -98,7 +98,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.ToTable("Reinforceds"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -124,9 +124,9 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.ToTable("ReinforcedComponents"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Order", b => { - b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced") .WithMany("Orders") .HasForeignKey("ReinforcedId") .OnDelete(DeleteBehavior.Cascade) @@ -135,15 +135,15 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.Navigation("Reinforced"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b => { - b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component") + b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Component", "Component") .WithMany("ReinforcedComponents") .HasForeignKey("ComponentId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced") .WithMany("Components") .HasForeignKey("ReinforcedId") .OnDelete(DeleteBehavior.Cascade) @@ -154,12 +154,12 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.Navigation("Reinforced"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Component", b => { b.Navigation("ReinforcedComponents"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b => { b.Navigation("Components"); diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.cs index cd4beed..eaed367 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240404165842_InitialCreate.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations; #nullable disable -namespace PrecastConcretePlantDatabaseImplement.Migrations +namespace PrecastConcretePlantDataBaseImplement.Migrations { /// public partial class InitialCreate : Migration diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240516193152_initial.Designer.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240516193152_initial.Designer.cs new file mode 100644 index 0000000..0685bff --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240516193152_initial.Designer.cs @@ -0,0 +1,250 @@ +// +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("20240516193152_initial")] + partial class initial + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("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.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("ReinforcedsMax") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.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", "Reinforced") + .WithMany("Components") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Reinforced"); + }); + + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ShopReinforced", b => + { + b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced") + .WithMany("ShopReinforceds") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Shop", "Shop") + .WithMany("Reinforceds") + .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"); + + b.Navigation("ShopReinforceds"); + }); + + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Shop", b => + { + b.Navigation("Reinforceds"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240516193152_initial.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240516193152_initial.cs new file mode 100644 index 0000000..049fe2d --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20240516193152_initial.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PrecastConcretePlantDataBaseImplement.Migrations +{ + /// + public partial class initial : 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"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + ReinforcedsMax = table.Column(type: "int", 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"), + ShopId = table.Column(type: "int", nullable: false), + ReinforcedId = 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 index 49f864c..a58218f 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDataBaseModelSnapshot.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDataBaseModelSnapshot.cs @@ -4,11 +4,11 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using PrecastConcretePlantDatabaseImplement; +using PrecastConcretePlantDataBaseImplement; #nullable disable -namespace PrecastConcretePlantDatabaseImplement.Migrations +namespace PrecastConcretePlantDataBaseImplement.Migrations { [DbContext(typeof(PrecastConcretePlantDataBase))] partial class PrecastConcretePlantDataBaseModelSnapshot : ModelSnapshot @@ -22,7 +22,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Component", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -42,7 +42,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.ToTable("Components"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Order", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -75,7 +75,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.ToTable("Orders"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -95,7 +95,7 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.ToTable("Reinforceds"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -121,9 +121,62 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.ToTable("ReinforcedComponents"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Shop", b => { - b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + 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("ReinforcedsMax") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.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) @@ -132,15 +185,15 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.Navigation("Reinforced"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ReinforcedComponent", b => { - b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component") + b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Component", "Component") .WithMany("ReinforcedComponents") .HasForeignKey("ComponentId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced") .WithMany("Components") .HasForeignKey("ReinforcedId") .OnDelete(DeleteBehavior.Cascade) @@ -151,16 +204,42 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.Navigation("Reinforced"); }); - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.ShopReinforced", b => + { + b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Reinforced", "Reinforced") + .WithMany("ShopReinforceds") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PrecastConcretePlantDataBaseImplement.Models.Shop", "Shop") + .WithMany("Reinforceds") + .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 => + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Reinforced", b => { b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("ShopReinforceds"); + }); + + modelBuilder.Entity("PrecastConcretePlantDataBaseImplement.Models.Shop", b => + { + b.Navigation("Reinforceds"); }); #pragma warning restore 612, 618 } diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Component.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Component.cs index bf70c0e..02fd5ca 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Component.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Component.cs @@ -9,7 +9,7 @@ using PrecastConcretePlantDataModels.Models; using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.ViewModels; -namespace PrecastConcretePlantDatabaseImplement.Models +namespace PrecastConcretePlantDataBaseImplement.Models { public class Component : IComponentModel { diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Order.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Order.cs index 026b549..0c2b8d7 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Order.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Order.cs @@ -9,7 +9,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PrecastConcretePlantDatabaseImplement.Models +namespace PrecastConcretePlantDataBaseImplement.Models { public class Order : IOrderModel { diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Reinforced.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Reinforced.cs index 33a4d2e..072a32c 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Reinforced.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Reinforced.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.ViewModels; -namespace PrecastConcretePlantDatabaseImplement.Models +namespace PrecastConcretePlantDataBaseImplement.Models { public class Reinforced : IReinforcedModel { @@ -45,6 +45,9 @@ namespace PrecastConcretePlantDatabaseImplement.Models [ForeignKey("ReinforcedId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("ReinforcedId")] + public virtual List ShopReinforceds { get; set; } = new(); + public static Reinforced Create(PrecastConcretePlantDataBase context, ReinforcedBindingModel model) { return new Reinforced() diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ReinforcedComponent.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ReinforcedComponent.cs index ccf8a49..933bdbd 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ReinforcedComponent.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ReinforcedComponent.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PrecastConcretePlantDatabaseImplement.Models +namespace PrecastConcretePlantDataBaseImplement.Models { public class ReinforcedComponent { diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..d7a7901 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs @@ -0,0 +1,110 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace PrecastConcretePlantDataBaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + + [Required] + public string ShopName { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime DateOpening { get; set; } + + [Required] + public int ReinforcedsMax { get; set; } + + private Dictionary? _shopReinforceds = null; + + [NotMapped] + public Dictionary ShopReinforceds + { + get + { + if (_shopReinforceds == null) + { + _shopReinforceds = Reinforceds + .ToDictionary(x => x.ReinforcedId, x => (x.Reinforced as IReinforcedModel, x.Count)); + } + return _shopReinforceds; + } + } + //3ий вопрос + [ForeignKey("ShopId")]//список записей класса-связи + public virtual List Reinforceds { get; set; } = new(); + + public static Shop Create(PrecastConcretePlantDataBase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + ReinforcedsMax = model.ReinforcedsMax, + Reinforceds = model.ShopReinforceds.Select(x => new ShopReinforced + { + Reinforced = context.Reinforceds.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + ReinforcedsMax = model.ReinforcedsMax; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + ReinforcedsMax = ReinforcedsMax, + ShopReinforceds = ShopReinforceds + }; + + 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.ShopReinforceds.ContainsKey(rec.ReinforcedId))); + context.SaveChanges(); + foreach (var updateReinforced in shopReinforceds) + { + if (model.ShopReinforceds.ContainsKey(updateReinforced.ReinforcedId)) + { + updateReinforced.Count = model.ShopReinforceds[updateReinforced.ReinforcedId].Item2; + model.ShopReinforceds.Remove(updateReinforced.ReinforcedId); + } + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ic in model.ShopReinforceds) + { + context.ShopReinforceds.Add(new ShopReinforced + { + Shop = shop, + Reinforced = context.Reinforceds.First(x => x.Id == ic.Key), + Count = ic.Value.Item2 + }); + context.SaveChanges(); + } + _shopReinforceds = null; + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ShopReinforced.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ShopReinforced.cs new file mode 100644 index 0000000..7350452 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/ShopReinforced.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace PrecastConcretePlantDataBaseImplement.Models +{ + public class ShopReinforced + { + public int Id { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int ReinforcedId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Reinforced Reinforced { get; set; } = new(); + + public virtual Shop Shop { get; set; } = new(); + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs index ee2f033..d31bc68 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/PrecastConcretePlantDatabase.cs @@ -1,5 +1,5 @@ using Microsoft.EntityFrameworkCore; -using PrecastConcretePlantDatabaseImplement.Models; +using PrecastConcretePlantDataBaseImplement.Models; using System; using System.Collections.Generic; using System.Diagnostics; @@ -7,7 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PrecastConcretePlantDatabaseImplement +namespace PrecastConcretePlantDataBaseImplement { public class PrecastConcretePlantDataBase : DbContext { @@ -15,7 +15,7 @@ namespace PrecastConcretePlantDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=PRETTYNAME;Initial Catalog=PrecastConcretePlantDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=PRETTYNAME;Initial Catalog=PrecastConcretePlantDataBaseHard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -23,6 +23,7 @@ namespace PrecastConcretePlantDatabaseImplement 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/PrecastConcretePlantView/PrecastConcretePlantView.csproj b/PrecastConcretePlant/PrecastConcretePlantView/PrecastConcretePlantView.csproj index 5db35b1..bf7ef5d 100644 --- a/PrecastConcretePlant/PrecastConcretePlantView/PrecastConcretePlantView.csproj +++ b/PrecastConcretePlant/PrecastConcretePlantView/PrecastConcretePlantView.csproj @@ -27,7 +27,7 @@ - + diff --git a/PrecastConcretePlant/PrecastConcretePlantView/Program.cs b/PrecastConcretePlant/PrecastConcretePlantView/Program.cs index d1dbea4..bdbf122 100644 --- a/PrecastConcretePlant/PrecastConcretePlantView/Program.cs +++ b/PrecastConcretePlant/PrecastConcretePlantView/Program.cs @@ -3,6 +3,9 @@ using PrecastConcretePlantBusinessLogic.BusinessLogics; using PrecastConcretePlantContracts.StoragesContracts; using PrecastConcretePlantFileImplement.Implements; using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; namespace PrecastConcretePlantView { -- 2.25.1