From b28240a98620a93e0470925db849d592f9ca002c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Sat, 18 Mar 2023 18:19:08 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=BB=D1=803=20=D0=BA=D0=BE=D0=BC=D0=BC?= =?UTF-8?q?=D0=B8=D1=821?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PrecastConcretePlant/PrecastConcretePlant.sln | 8 +++++++- .../PrecastConcretePlantView.csproj | 4 ++++ .../Implements/ComponentStorage.cs | 12 ++++++++++++ .../Implements/OrderStorage.cs | 12 ++++++++++++ .../Implements/ReinforcedStorage.cs | 12 ++++++++++++ .../Models/Component.cs | 12 ++++++++++++ .../Models/Order.cs | 12 ++++++++++++ .../Models/Reinforced.cs | 12 ++++++++++++ ...recastConcretePlantDataBaseImplement.csproj | 18 ++++++++++++++++++ 9 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 PrecastConcretePlantDataBaseImplemet/Implements/ComponentStorage.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Implements/ReinforcedStorage.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Models/Component.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Models/Order.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Models/Reinforced.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDataBaseImplement.csproj diff --git a/PrecastConcretePlant/PrecastConcretePlant.sln b/PrecastConcretePlant/PrecastConcretePlant.sln index 49a1aec..b5c500a 100644 --- a/PrecastConcretePlant/PrecastConcretePlant.sln +++ b/PrecastConcretePlant/PrecastConcretePlant.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantContrac EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantListImplement", "..\PrecastConcretePlantListImplement\PrecastConcretePlantListImplement.csproj", "{9EFFE92D-1414-4FD9-B424-56A32797499A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantFileImplement", "..\PrecastConcreteFileImplement\PrecastConcretePlantFileImplement.csproj", "{921B695C-0124-4295-8E39-1CC60E0D1D56}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantFileImplement", "..\PrecastConcreteFileImplement\PrecastConcretePlantFileImplement.csproj", "{921B695C-0124-4295-8E39-1CC60E0D1D56}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantDataBaseImplement", "..\PrecastConcretePlantDataBaseImplemet\PrecastConcretePlantDataBaseImplement.csproj", "{8E649C11-1085-49FC-BC7A-CACB558CB39A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {921B695C-0124-4295-8E39-1CC60E0D1D56}.Debug|Any CPU.Build.0 = Debug|Any CPU {921B695C-0124-4295-8E39-1CC60E0D1D56}.Release|Any CPU.ActiveCfg = Release|Any CPU {921B695C-0124-4295-8E39-1CC60E0D1D56}.Release|Any CPU.Build.0 = Release|Any CPU + {8E649C11-1085-49FC-BC7A-CACB558CB39A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E649C11-1085-49FC-BC7A-CACB558CB39A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E649C11-1085-49FC-BC7A-CACB558CB39A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E649C11-1085-49FC-BC7A-CACB558CB39A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PrecastConcretePlant/PrecastConcretePlantView.csproj b/PrecastConcretePlant/PrecastConcretePlantView.csproj index aa58eb5..4db65e5 100644 --- a/PrecastConcretePlant/PrecastConcretePlantView.csproj +++ b/PrecastConcretePlant/PrecastConcretePlantView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/PrecastConcretePlantDataBaseImplemet/Implements/ComponentStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/ComponentStorage.cs new file mode 100644 index 0000000..5bf3dfa --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Implements/ComponentStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDataBaseImplement.Implements +{ + internal class ComponentStorage + { + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs new file mode 100644 index 0000000..a7ca9bc --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDataBaseImplement.Implements +{ + internal class OrderStorage + { + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Implements/ReinforcedStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/ReinforcedStorage.cs new file mode 100644 index 0000000..9c403dc --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Implements/ReinforcedStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDataBaseImplement.Implements +{ + internal class ReinforcedStorage + { + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Models/Component.cs b/PrecastConcretePlantDataBaseImplemet/Models/Component.cs new file mode 100644 index 0000000..dae2fb8 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Models/Component.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDataBaseImplement.Models +{ + internal class Component + { + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Models/Order.cs b/PrecastConcretePlantDataBaseImplemet/Models/Order.cs new file mode 100644 index 0000000..d18a69b --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Models/Order.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDataBaseImplement.Models +{ + internal class Order + { + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Models/Reinforced.cs b/PrecastConcretePlantDataBaseImplemet/Models/Reinforced.cs new file mode 100644 index 0000000..21081a2 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Models/Reinforced.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDataBaseImplement.Models +{ + internal class Reinforced + { + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDataBaseImplement.csproj b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDataBaseImplement.csproj new file mode 100644 index 0000000..71d4739 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDataBaseImplement.csproj @@ -0,0 +1,18 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + From 1cf2ff71735cc0b1a6a3ca1acb58a358d8ec7b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Sat, 18 Mar 2023 19:33:11 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BB=D1=803?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PrecastConcretePlant/PrecastConcretePlant.sln | 2 +- .../PrecastConcretePlantView.csproj | 1 + PrecastConcretePlant/Program.cs | 2 +- .../Implements/ComponentStorage.cs | 83 ++++++++- .../Implements/OrderStorage.cs | 96 +++++++++- .../Implements/ReinforcedStorage.cs | 105 ++++++++++- .../20230318153144_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20230318153144_InitialCreate.cs | 125 +++++++++++++ ...ecastConcretePlantDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 56 +++++- .../Models/Order.cs | 64 ++++++- .../Models/Reinforced.cs | 93 +++++++++- .../Models/ReinforcedComponent.cs | 22 +++ ...ecastConcretePlantDataBaseImplement.csproj | 8 + .../PrecastConcretePlantDatabase.cs | 27 +++ 15 files changed, 1003 insertions(+), 20 deletions(-) create mode 100644 PrecastConcretePlantDataBaseImplemet/Migrations/20230318153144_InitialCreate.Designer.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Migrations/20230318153144_InitialCreate.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/Models/ReinforcedComponent.cs create mode 100644 PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs diff --git a/PrecastConcretePlant/PrecastConcretePlant.sln b/PrecastConcretePlant/PrecastConcretePlant.sln index b5c500a..ec3631c 100644 --- a/PrecastConcretePlant/PrecastConcretePlant.sln +++ b/PrecastConcretePlant/PrecastConcretePlant.sln @@ -15,7 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantListImp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantFileImplement", "..\PrecastConcreteFileImplement\PrecastConcretePlantFileImplement.csproj", "{921B695C-0124-4295-8E39-1CC60E0D1D56}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantDataBaseImplement", "..\PrecastConcretePlantDataBaseImplemet\PrecastConcretePlantDataBaseImplement.csproj", "{8E649C11-1085-49FC-BC7A-CACB558CB39A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantDatabaseImplement", "..\PrecastConcretePlantDataBaseImplemet\PrecastConcretePlantDatabaseImplement.csproj", "{8E649C11-1085-49FC-BC7A-CACB558CB39A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/PrecastConcretePlant/PrecastConcretePlantView.csproj b/PrecastConcretePlant/PrecastConcretePlantView.csproj index 4db65e5..e053994 100644 --- a/PrecastConcretePlant/PrecastConcretePlantView.csproj +++ b/PrecastConcretePlant/PrecastConcretePlantView.csproj @@ -20,6 +20,7 @@ + diff --git a/PrecastConcretePlant/Program.cs b/PrecastConcretePlant/Program.cs index 706296f..54ca4c8 100644 --- a/PrecastConcretePlant/Program.cs +++ b/PrecastConcretePlant/Program.cs @@ -5,7 +5,7 @@ using NLog.Extensions.Logging; using PrecastConcretePlantBusinessLogic.BusinessLogic; using PrecastConcretePlantContracts.BusinessLogicsContracts; using PrecastConcretePlantContracts.StoragesContracts; -using PrecastConcretePlantFileImplement.Implements; +using PrecastConcretePlantDatabaseImplement.Implements; namespace PrecastConcretePlant { diff --git a/PrecastConcretePlantDataBaseImplemet/Implements/ComponentStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/ComponentStorage.cs index 5bf3dfa..494b1e2 100644 --- a/PrecastConcretePlantDataBaseImplemet/Implements/ComponentStorage.cs +++ b/PrecastConcretePlantDataBaseImplemet/Implements/ComponentStorage.cs @@ -1,12 +1,89 @@ -using System; +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 +namespace PrecastConcretePlantDatabaseImplement.Implements { - internal class ComponentStorage + 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/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs index a7ca9bc..ddf9812 100644 --- a/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs +++ b/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs @@ -1,12 +1,102 @@ -using System; +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 +namespace PrecastConcretePlantDatabaseImplement.Implements { - internal class OrderStorage + public class OrderStorage : IOrderStorage { + 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; + } + + 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 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 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 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; + } } } diff --git a/PrecastConcretePlantDataBaseImplemet/Implements/ReinforcedStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/ReinforcedStorage.cs index 9c403dc..88c9b7e 100644 --- a/PrecastConcretePlantDataBaseImplemet/Implements/ReinforcedStorage.cs +++ b/PrecastConcretePlantDataBaseImplemet/Implements/ReinforcedStorage.cs @@ -1,12 +1,111 @@ -using System; +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 +namespace PrecastConcretePlantDatabaseImplement.Implements { - internal class ReinforcedStorage + public class ReinforcedStorage : IReinforcedStorage { + public List GetFullList() + { + using var context = new PrecastConcretePlantDatabase(); + return context.Reinforceds + .Include(x => x.Components) + .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) + .Include(x=>x.Orders) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Reinforceds.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/PrecastConcretePlantDataBaseImplemet/Migrations/20230318153144_InitialCreate.Designer.cs b/PrecastConcretePlantDataBaseImplemet/Migrations/20230318153144_InitialCreate.Designer.cs new file mode 100644 index 0000000..b3bec6f --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Migrations/20230318153144_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("20230318153144_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .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/PrecastConcretePlantDataBaseImplemet/Migrations/20230318153144_InitialCreate.cs b/PrecastConcretePlantDataBaseImplemet/Migrations/20230318153144_InitialCreate.cs new file mode 100644 index 0000000..b2eb22d --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Migrations/20230318153144_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"), + 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/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs b/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs new file mode 100644 index 0000000..ff469be --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/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", "7.0.4") + .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/PrecastConcretePlantDataBaseImplemet/Models/Component.cs b/PrecastConcretePlantDataBaseImplemet/Models/Component.cs index dae2fb8..449d63a 100644 --- a/PrecastConcretePlantDataBaseImplemet/Models/Component.cs +++ b/PrecastConcretePlantDataBaseImplemet/Models/Component.cs @@ -1,12 +1,62 @@ -using System; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; +using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PrecastConcretePlantDataBaseImplement.Models +namespace PrecastConcretePlantDatabaseImplement.Models { - internal class Component + 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/PrecastConcretePlantDataBaseImplemet/Models/Order.cs b/PrecastConcretePlantDataBaseImplemet/Models/Order.cs index d18a69b..c81dbd0 100644 --- a/PrecastConcretePlantDataBaseImplemet/Models/Order.cs +++ b/PrecastConcretePlantDataBaseImplemet/Models/Order.cs @@ -1,12 +1,70 @@ -using System; +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 +namespace PrecastConcretePlantDatabaseImplement.Models { - internal class Order + 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; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { 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, + ReinforcedId = model.ReinforcedId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + ReinforcedId = ReinforcedId, + ReinforcedName = Reinforced.ReinforcedName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + + }; } } diff --git a/PrecastConcretePlantDataBaseImplemet/Models/Reinforced.cs b/PrecastConcretePlantDataBaseImplemet/Models/Reinforced.cs index 21081a2..53f235d 100644 --- a/PrecastConcretePlantDataBaseImplemet/Models/Reinforced.cs +++ b/PrecastConcretePlantDataBaseImplemet/Models/Reinforced.cs @@ -1,12 +1,99 @@ -using System; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; +using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PrecastConcretePlantDataBaseImplement.Models +namespace PrecastConcretePlantDatabaseImplement.Models { - internal class Reinforced + 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 rc in model.ReinforcedComponents) + { + context.ReinforcedComponents.Add(new ReinforcedComponent + { + Reinforced = Reinforced, + Component = context.Components.First(x => x.Id == rc.Key), + Count = rc.Value.Item2 + }); + context.SaveChanges(); + } + _reinforcedComponents = null; + } } } diff --git a/PrecastConcretePlantDataBaseImplemet/Models/ReinforcedComponent.cs b/PrecastConcretePlantDataBaseImplemet/Models/ReinforcedComponent.cs new file mode 100644 index 0000000..b7e1dc4 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Models/ReinforcedComponent.cs @@ -0,0 +1,22 @@ +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 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/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDataBaseImplement.csproj b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDataBaseImplement.csproj index 71d4739..2e44ad2 100644 --- a/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDataBaseImplement.csproj +++ b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDataBaseImplement.csproj @@ -13,6 +13,14 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs new file mode 100644 index 0000000..8cf3f6c --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +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=DESKTOP-EJNKGL2;Initial Catalog= PrecastConcretePlantDatabaseFull;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; } + } +}