From e4abf1b41aec6e9ae4f209ff5e68af2ad543745c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20=D0=94?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BB=D0=BE=D0=B2?= Date: Sat, 23 Mar 2024 07:22:52 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=92=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?UTF-8?q?=D1=81=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RenovationWork/RenovationWork.sln | 8 ++- .../Models/Component.cs | 63 +++++++++++++++++ .../Models/Order.cs | 70 +++++++++++++++++++ .../Models/Repair.cs | 54 ++++++++++++++ .../RenovationWorkDatabaseImplement.csproj | 27 +++++++ 5 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Models/Repair.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj diff --git a/RenovationWork/RenovationWork.sln b/RenovationWork/RenovationWork.sln index 4aeae29..9f55ce3 100644 --- a/RenovationWork/RenovationWork.sln +++ b/RenovationWork/RenovationWork.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenovationWorkBusinessLogic EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenovationWorkListImplement", "RenovationWorkListImplement\RenovationWorkListImplement.csproj", "{CBEF7964-AEB9-4FE9-B85F-9FBDEE198EED}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RenovationWorkFileImplement", "RenovationWorkFileImplement\RenovationWorkFileImplement.csproj", "{C52182B7-2CA7-4D36-BC9F-81F165A4539A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenovationWorkFileImplement", "RenovationWorkFileImplement\RenovationWorkFileImplement.csproj", "{C52182B7-2CA7-4D36-BC9F-81F165A4539A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RenovationWorkDatabaseImplement", "RenovationWorkDatabaseImplement\RenovationWorkDatabaseImplement.csproj", "{2BB32959-A1A6-4C2B-8C9F-68EA6D60C1EC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {C52182B7-2CA7-4D36-BC9F-81F165A4539A}.Debug|Any CPU.Build.0 = Debug|Any CPU {C52182B7-2CA7-4D36-BC9F-81F165A4539A}.Release|Any CPU.ActiveCfg = Release|Any CPU {C52182B7-2CA7-4D36-BC9F-81F165A4539A}.Release|Any CPU.Build.0 = Release|Any CPU + {2BB32959-A1A6-4C2B-8C9F-68EA6D60C1EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2BB32959-A1A6-4C2B-8C9F-68EA6D60C1EC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2BB32959-A1A6-4C2B-8C9F-68EA6D60C1EC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2BB32959-A1A6-4C2B-8C9F-68EA6D60C1EC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..44a53e0 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Models; + +namespace RenovationWorkDatabaseImplement.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 ProductComponents { 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/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..14ad066 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Enums; +using RenovationWorkDataModels.Models; + +namespace RenovationWorkDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int RepairId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; set; } = DateTime.Now; + + public DateTime? DateImplement { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + RepairId = model.RepairId, + 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; + if (model.DateImplement != null) + { + DateImplement = model.DateImplement; + } + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + RepairId = RepairId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} + diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Repair.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Repair.cs new file mode 100644 index 0000000..e2ee449 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Repair.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Models; + +namespace RenovationWorkDatabaseImplement.Models +{ + public class Repair : IRepairModel + { + public int Id { get; private set; } + public string RepairName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary RepairComponents + { + get; + private set; + } = new Dictionary(); + public static Repair? Create(RepairBindingModel? model) + { + if (model == null) + { + return null; + } + return new Repair() + { + Id = model.Id, + RepairName = model.RepairName, + Price = model.Price, + RepairComponents = model.RepairComponents + }; + } + public void Update(RepairBindingModel? model) + { + if (model == null) + { + return; + } + RepairName = model.RepairName; + Price = model.Price; + RepairComponents = model.RepairComponents; + } + public RepairViewModel GetViewModel => new() + { + Id = Id, + RepairName = RepairName, + Price = Price, + RepairComponents = RepairComponents + }; + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj new file mode 100644 index 0000000..4cb968c --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + -- 2.25.1 From be264cb02baae535af9902e2adf12bebd32bb8fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20=D0=94?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BB=D0=BE=D0=B2?= Date: Fri, 5 Apr 2024 10:40:09 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=B0=D0=B0=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RenovationWork/RenovationWork/Program.cs | 2 +- .../RenovationWork/RenovationWorkView.csproj | 5 + .../RenovationWorkBusinessLogic.csproj | 2 +- .../RenovationWorkContracts.csproj | 2 +- .../RenovationWorkDataModels.csproj | 2 +- .../Implements/ComponentStorage.cs | 81 +++++++++ .../Implements/OrderStorage.cs | 94 ++++++++++ .../Implements/RepairStorage.cs | 103 +++++++++++ .../20240405063901_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240405063901_InitialCreate.cs | 126 +++++++++++++ .../RenovationWorkDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 2 +- .../Models/Order.cs | 72 ++++---- .../Models/Repair.cs | 79 +++++--- .../Models/RepairComponent.cs | 22 +++ .../RenovationWorkDatabase.cs | 31 ++++ .../RenovationWorkDatabaseImplement.csproj | 8 +- .../RenovationWorkFileImplement.csproj | 2 +- .../RenovationWorkListImplement.csproj | 2 +- 19 files changed, 908 insertions(+), 66 deletions(-) create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Models/RepairComponent.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs diff --git a/RenovationWork/RenovationWork/Program.cs b/RenovationWork/RenovationWork/Program.cs index 632e3c8..100b404 100644 --- a/RenovationWork/RenovationWork/Program.cs +++ b/RenovationWork/RenovationWork/Program.cs @@ -1,7 +1,7 @@ using RenovationWorkBusinessLogic.BusinessLogics; using RenovationWorkContracts.BusinessLogicsContracts; using RenovationWorkContracts.StoragesContracts; -using RenovationWorkFileImplement.Implements; +using RenovationWorkDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/RenovationWork/RenovationWork/RenovationWorkView.csproj b/RenovationWork/RenovationWork/RenovationWorkView.csproj index 709081e..328de6e 100644 --- a/RenovationWork/RenovationWork/RenovationWorkView.csproj +++ b/RenovationWork/RenovationWork/RenovationWorkView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +20,7 @@ + diff --git a/RenovationWork/RenovationWorkBusinessLogic/RenovationWorkBusinessLogic.csproj b/RenovationWork/RenovationWorkBusinessLogic/RenovationWorkBusinessLogic.csproj index 62a4b97..cdb3aba 100644 --- a/RenovationWork/RenovationWorkBusinessLogic/RenovationWorkBusinessLogic.csproj +++ b/RenovationWork/RenovationWorkBusinessLogic/RenovationWorkBusinessLogic.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable diff --git a/RenovationWork/RenovationWorkContracts/RenovationWorkContracts.csproj b/RenovationWork/RenovationWorkContracts/RenovationWorkContracts.csproj index 96ab30f..5d92d02 100644 --- a/RenovationWork/RenovationWorkContracts/RenovationWorkContracts.csproj +++ b/RenovationWork/RenovationWorkContracts/RenovationWorkContracts.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable diff --git a/RenovationWork/RenovationWorkDataModels/RenovationWorkDataModels.csproj b/RenovationWork/RenovationWorkDataModels/RenovationWorkDataModels.csproj index 132c02c..30402ac 100644 --- a/RenovationWork/RenovationWorkDataModels/RenovationWorkDataModels.csproj +++ b/RenovationWork/RenovationWorkDataModels/RenovationWorkDataModels.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..66535a6 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,81 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement; +using RenovationWorkDatabaseImplement.Models; + +namespace RenovationWorkDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new RenovationWorkDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + 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/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..561b83d --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,94 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace RenovationWorkDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var Context = new RenovationWorkDatabase(); + + return Context.Orders + .Include(x => x.Repair) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel Model) + { + if (!Model.Id.HasValue) + return new(); + + using var Context = new RenovationWorkDatabase(); + + return Context.Orders + .Include(x => x.Repair) + .Where(x => x.Id == Model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel Model) + { + if (!Model.Id.HasValue) + return new(); + + using var Context = new RenovationWorkDatabase(); + + return Context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => x.Id == Model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel Model) + { + using var Context = new RenovationWorkDatabase(); + + if (Model == null) + return null; + + var NewOrder = Order.Create(Context, Model); + if (NewOrder == null) + return null; + + Context.Orders.Add(NewOrder); + Context.SaveChanges(); + + return NewOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel Model) + { + using var Context = new RenovationWorkDatabase(); + + var Order = Context.Orders.FirstOrDefault(x => x.Id == Model.Id); + if (Order == null) + return null; + + Order.Update(Model); + Context.SaveChanges(); + + return Order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel Model) + { + using var Context = new RenovationWorkDatabase(); + var Order = Context.Orders.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Order == null) + return null; + + Context.Orders.Remove(Order); + Context.SaveChanges(); + + return Order.GetViewModel; + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs new file mode 100644 index 0000000..837c6b7 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs @@ -0,0 +1,103 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +namespace RenovationWorkDatabaseImplement.Implements +{ + public class RepairStorage : IRepairStorage + { + public List GetFullList() + { + using var context = new RenovationWorkDatabase(); + return context.Repairs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(RepairSearchModel model) + { + if (string.IsNullOrEmpty(model.RepairName)) + { + return new(); + } + using var context = new RenovationWorkDatabase(); + return context.Repairs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.RepairName.Contains(model.RepairName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public RepairViewModel? GetElement(RepairSearchModel model) + { + if (string.IsNullOrEmpty(model.RepairName) && + !model.Id.HasValue) + { + return null; + } + using var context = new RenovationWorkDatabase(); + return context.Repairs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RepairName) && + x.RepairName == model.RepairName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public RepairViewModel? Insert(RepairBindingModel model) + { + using var context = new RenovationWorkDatabase(); + var newRepair = Repair.Create(context, model); + if (newRepair == null) + { + return null; + } + context.Repairs.Add(newRepair); + context.SaveChanges(); + return newRepair.GetViewModel; + } + public RepairViewModel? Update(RepairBindingModel model) + { + using var context = new RenovationWorkDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var repair = context.Repairs.FirstOrDefault(rec => + rec.Id == model.Id); + if (repair == null) + { + return null; + } + repair.Update(model); + context.SaveChanges(); + repair.UpdateComponents(context, model); + transaction.Commit(); + return repair.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public RepairViewModel? Delete(RepairBindingModel model) + { + using var context = new RenovationWorkDatabase(); + var element = context.Repairs + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Repairs.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} + diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs new file mode 100644 index 0000000..7830f94 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using RenovationWorkDatabaseImplement; + +#nullable disable + +namespace RenovationWorkDatabaseImplement.Migrations +{ + [DbContext(typeof(RenovationWorkDatabase))] + [Migration("20240405063901_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp without time zone"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("double precision"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Order", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Models.Component", "Component") + .WithMany("RepairComponent") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Component", b => + { + b.Navigation("RepairComponent"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.cs new file mode 100644 index 0000000..24f1e69 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace RenovationWorkDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Repairs", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RepairName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Repairs", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RepairId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp without time zone", nullable: false), + DateImplement = table.Column(type: "timestamp without time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Repairs_RepairId", + column: x => x.RepairId, + principalTable: "Repairs", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "RepairComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RepairId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RepairComponents", x => x.Id); + table.ForeignKey( + name: "FK_RepairComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RepairComponents_Repairs_RepairId", + column: x => x.RepairId, + principalTable: "Repairs", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_RepairId", + table: "Orders", + column: "RepairId"); + + migrationBuilder.CreateIndex( + name: "IX_RepairComponents_ComponentId", + table: "RepairComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_RepairComponents_RepairId", + table: "RepairComponents", + column: "RepairId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "RepairComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Repairs"); + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs new file mode 100644 index 0000000..856a482 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using RenovationWorkDatabaseImplement; + +#nullable disable + +namespace RenovationWorkDatabaseImplement.Migrations +{ + [DbContext(typeof(RenovationWorkDatabase))] + partial class RenovationWorkDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp without time zone"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("double precision"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Order", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Models.Component", "Component") + .WithMany("RepairComponent") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Component", b => + { + b.Navigation("RepairComponent"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs index 44a53e0..169c024 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs @@ -20,7 +20,7 @@ namespace RenovationWorkDatabaseImplement.Models [Required] public double Cost { get; set; } [ForeignKey("ComponentId")] - public virtual List ProductComponents { get; set; } = new(); + public virtual List RepairComponent { get; set; } = new(); public static Component? Create(ComponentBindingModel model) { if (model == null) diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs index 14ad066..a946e8e 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs @@ -1,5 +1,7 @@ using System; +using System.Collections; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -13,57 +15,63 @@ namespace RenovationWorkDatabaseImplement.Models public class Order : IOrderModel { public int Id { get; private set; } - + + [Required] public int RepairId { get; private set; } - + + public virtual Repair Repair { get; set; } = new(); + + [Required] public int Count { get; private set; } - + + [Required] public double Sum { get; private set; } - - public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - - public DateTime DateCreate { get; set; } = DateTime.Now; - - public DateTime? DateImplement { get; set; } - - public static Order? Create(OrderBindingModel? model) + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order Create(RenovationWorkDatabase context, OrderBindingModel Model) { - if (model == null) - { - return null; - } return new Order() { - Id = model.Id, - RepairId = model.RepairId, - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement + Id = Model.Id, + RepairId = Model.RepairId, + Count = Model.Count, + Sum = Model.Sum, + Status = Model.Status, + DateCreate = Model.DateCreate, + DateImplement = Model.DateImplement, }; } - public void Update(OrderBindingModel? model) + + public void Update(OrderBindingModel Model) { - if (model == null) - { + if (Model == null) return; - } - Status = model.Status; - if (model.DateImplement != null) - { - DateImplement = model.DateImplement; - } + + Id = Model.Id; + RepairId = Model.RepairId; + Sum = Model.Sum; + Status = Model.Status; + DateCreate = Model.DateCreate; + DateImplement = Model.DateImplement; } + public OrderViewModel GetViewModel => new() { Id = Id, RepairId = RepairId, + RepairName = Repair.RepairName, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement + DateImplement = DateImplement, }; } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Repair.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Repair.cs index e2ee449..a73825e 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Models/Repair.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Repair.cs @@ -1,47 +1,53 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.BindingModels; using RenovationWorkContracts.ViewModels; using RenovationWorkDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace RenovationWorkDatabaseImplement.Models { public class Repair : IRepairModel { - public int Id { get; private set; } - public string RepairName { get; private set; } = string.Empty; - public double Price { get; private set; } + public int Id { get; set; } + [Required] + public string RepairName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _repairComponents = null; + [NotMapped] public Dictionary RepairComponents { - get; - private set; - } = new Dictionary(); - public static Repair? Create(RepairBindingModel? model) - { - if (model == null) + get { - return null; + if (_repairComponents == null) + { + _repairComponents = Components.ToDictionary(recDC => recDC.ComponentId, recDC => (recDC.Component as IComponentModel, recDC.Count)); + } + return _repairComponents; } + } + [ForeignKey("RepairId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("RepairId")] + public virtual List Orders { get; set; } = new(); + public static Repair Create(RenovationWorkDatabase context, RepairBindingModel model) + { return new Repair() { Id = model.Id, RepairName = model.RepairName, Price = model.Price, - RepairComponents = model.RepairComponents + Components = model.RepairComponents.Select(x => new RepairComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() }; } - public void Update(RepairBindingModel? model) + public void Update(RepairBindingModel model) { - if (model == null) - { - return; - } RepairName = model.RepairName; Price = model.Price; - RepairComponents = model.RepairComponents; } public RepairViewModel GetViewModel => new() { @@ -50,5 +56,32 @@ namespace RenovationWorkDatabaseImplement.Models Price = Price, RepairComponents = RepairComponents }; + public void UpdateComponents(RenovationWorkDatabase context, RepairBindingModel model) + { + var repairComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList(); + if (repairComponents != null && repairComponents.Count > 0) + { + context.RepairComponents.RemoveRange(repairComponents.Where(rec => !model.RepairComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + foreach (var updateComponent in repairComponents) + { + updateComponent.Count = model.RepairComponents[updateComponent.ComponentId].Item2; + model.RepairComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var repair = context.Repairs.First(x => x.Id == Id); + foreach (var pc in model.RepairComponents) + { + context.RepairComponents.Add(new RepairComponent + { + Repair = repair, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _repairComponents = null; + } } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/RepairComponent.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/RepairComponent.cs new file mode 100644 index 0000000..badf66c --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/RepairComponent.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 RenovationWorkDatabaseImplement.Models +{ + public class RepairComponent + { + public int Id { get; set; } + [Required] + public int RepairId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Repair Repair { get; set; } = new(); + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs new file mode 100644 index 0000000..19309f2 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using RenovationWorkDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement +{ + public class RenovationWorkDatabase: DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=RenovationWorkDatabaseFull;Username=postgres;Password=postgres"); + } + + base.OnConfiguring(optionsBuilder); + + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Repairs { set; get; } + public virtual DbSet RepairComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj index 4cb968c..db012e9 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj +++ b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable @@ -11,12 +11,12 @@ - - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/RenovationWork/RenovationWorkFileImplement/RenovationWorkFileImplement.csproj b/RenovationWork/RenovationWorkFileImplement/RenovationWorkFileImplement.csproj index 2d896a8..3d6a79f 100644 --- a/RenovationWork/RenovationWorkFileImplement/RenovationWorkFileImplement.csproj +++ b/RenovationWork/RenovationWorkFileImplement/RenovationWorkFileImplement.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable diff --git a/RenovationWork/RenovationWorkListImplement/RenovationWorkListImplement.csproj b/RenovationWork/RenovationWorkListImplement/RenovationWorkListImplement.csproj index 2d896a8..3d6a79f 100644 --- a/RenovationWork/RenovationWorkListImplement/RenovationWorkListImplement.csproj +++ b/RenovationWork/RenovationWorkListImplement/RenovationWorkListImplement.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable -- 2.25.1 From d696e4d6b7600ac1cc4ed814df67e7b19a4883ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20=D0=94?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BB=D0=BE=D0=B2?= Date: Thu, 18 Apr 2024 16:51:50 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9D=D0=B0=D0=B4=D0=B5=D0=B6=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/RepairLogic.cs | 6 +- .../Implements/ComponentStorage.cs | 25 ++-- .../Implements/OrderStorage.cs | 120 ++++++++---------- .../Implements/RepairStorage.cs | 42 ++---- ... 20240418122550_InitialCreate.Designer.cs} | 2 +- ...ate.cs => 20240418122550_InitialCreate.cs} | 0 .../Models/Component.cs | 3 +- .../Models/Order.cs | 21 ++- 8 files changed, 92 insertions(+), 127 deletions(-) rename RenovationWork/RenovationWorkDatabaseImplement/Migrations/{20240405063901_InitialCreate.Designer.cs => 20240418122550_InitialCreate.Designer.cs} (99%) rename RenovationWork/RenovationWorkDatabaseImplement/Migrations/{20240405063901_InitialCreate.cs => 20240418122550_InitialCreate.cs} (100%) diff --git a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs index 3301975..bb0fc84 100644 --- a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs +++ b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs @@ -92,11 +92,11 @@ namespace RenovationWorkBusinessLogic.BusinessLogics } if (string.IsNullOrEmpty(model.RepairName)) { - throw new ArgumentNullException("Нет названия компьютера", nameof(model.RepairName)); + throw new ArgumentNullException("Нет названия ремонта", nameof(model.RepairName)); } if (model.Price <= 0) { - throw new ArgumentNullException("Цена компьютера должна быть больше 0", nameof(model.Price)); + throw new ArgumentNullException("Цена ремонта должна быть больше 0", nameof(model.Price)); } _logger.LogInformation("Repair. RepairName:{RepairName}.Price:{ Price}. Id: { Id} ", model.RepairName, model.Price, model.Id); var element = _repairStorage.GetElement(new RepairSearchModel @@ -105,7 +105,7 @@ namespace RenovationWorkBusinessLogic.BusinessLogics }); if (element != null && element.Id != model.Id) { - throw new InvalidOperationException("Компьютер с таким названием уже есть"); + throw new InvalidOperationException("Ремонт с таким названием уже есть"); } } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs index 66535a6..066bb08 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs @@ -12,9 +12,7 @@ namespace RenovationWorkDatabaseImplement.Implements public List GetFullList() { using var context = new RenovationWorkDatabase(); - return context.Components - .Select(x => x.GetViewModel) - .ToList(); + return context.Components.Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(ComponentSearchModel model) { @@ -23,10 +21,7 @@ namespace RenovationWorkDatabaseImplement.Implements return new(); } using var context = new RenovationWorkDatabase(); - return context.Components - .Where(x => x.ComponentName.Contains(model.ComponentName)) - .Select(x => x.GetViewModel) - .ToList(); + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); } public ComponentViewModel? GetElement(ComponentSearchModel model) { @@ -35,11 +30,7 @@ namespace RenovationWorkDatabaseImplement.Implements return null; } using var context = new RenovationWorkDatabase(); - return context.Components - .FirstOrDefault(x => - (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == - model.ComponentName) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + 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) { @@ -69,13 +60,13 @@ namespace RenovationWorkDatabaseImplement.Implements { using var context = new RenovationWorkDatabase(); var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + if (element == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; } } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs index 561b83d..fefaecb 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs @@ -11,84 +11,76 @@ namespace RenovationWorkDatabaseImplement.Implements { public List GetFullList() { - using var Context = new RenovationWorkDatabase(); - - return Context.Orders - .Include(x => x.Repair) - .Select(x => x.GetViewModel) - .ToList(); + using var context = new RenovationWorkDatabase(); + return context.Orders.Include(x => x.Repair).Select(x => x.GetViewModel).ToList(); } - - public List GetFilteredList(OrderSearchModel Model) + public List GetFilteredList(OrderSearchModel model) { - if (!Model.Id.HasValue) + if (!model.Id.HasValue) + { return new(); - - using var Context = new RenovationWorkDatabase(); - - return Context.Orders - .Include(x => x.Repair) - .Where(x => x.Id == Model.Id) - .Select(x => x.GetViewModel) - .ToList(); + } + using var context = new RenovationWorkDatabase(); + return context.Orders + .Include(x => x.Repair) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } - - public OrderViewModel? GetElement(OrderSearchModel Model) + public OrderViewModel? GetElement(OrderSearchModel model) { - if (!Model.Id.HasValue) - return new(); - - using var Context = new RenovationWorkDatabase(); - - return Context.Orders - .Include(x => x.Repair) - .FirstOrDefault(x => x.Id == Model.Id) - ?.GetViewModel; + if (!model.Id.HasValue) + { + return null; + } + using var context = new RenovationWorkDatabase(); + return context.Orders.Include(x => x.Repair) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } - - public OrderViewModel? Insert(OrderBindingModel Model) + public OrderViewModel? Insert(OrderBindingModel model) { - using var Context = new RenovationWorkDatabase(); - - if (Model == null) + using var context = new RenovationWorkDatabase(); + if (model == null) return null; - - var NewOrder = Order.Create(Context, Model); - if (NewOrder == null) + var newOrder = Order.Create(context, model); + if (newOrder == null) + { return null; - - Context.Orders.Add(NewOrder); - Context.SaveChanges(); - - return NewOrder.GetViewModel; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; } - - public OrderViewModel? Update(OrderBindingModel Model) + public OrderViewModel? Update(OrderBindingModel model) { - using var Context = new RenovationWorkDatabase(); - - var Order = Context.Orders.FirstOrDefault(x => x.Id == Model.Id); - if (Order == null) + using var context = new RenovationWorkDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { return null; - - Order.Update(Model); - Context.SaveChanges(); - - return Order.GetViewModel; + } + order.Update(model); + context.SaveChanges(); + return context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } - - public OrderViewModel? Delete(OrderBindingModel Model) + public OrderViewModel? Delete(OrderBindingModel model) { - using var Context = new RenovationWorkDatabase(); - var Order = Context.Orders.FirstOrDefault(rec => rec.Id == Model.Id); - - if (Order == null) - return null; - - Context.Orders.Remove(Order); - Context.SaveChanges(); - - return Order.GetViewModel; + using var context = new RenovationWorkDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; } } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs index 837c6b7..2c27e22 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs @@ -11,12 +11,7 @@ namespace RenovationWorkDatabaseImplement.Implements public List GetFullList() { using var context = new RenovationWorkDatabase(); - return context.Repairs - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); + return context.Repairs.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(RepairSearchModel model) { @@ -25,13 +20,8 @@ namespace RenovationWorkDatabaseImplement.Implements return new(); } using var context = new RenovationWorkDatabase(); - return context.Repairs - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Where(x => x.RepairName.Contains(model.RepairName)) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); + return context.Repairs.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.RepairName.Contains(model.RepairName)).ToList().Select(x => x.GetViewModel).ToList(); + } public RepairViewModel? GetElement(RepairSearchModel model) { @@ -41,24 +31,20 @@ namespace RenovationWorkDatabaseImplement.Implements return null; } using var context = new RenovationWorkDatabase(); - return context.Repairs - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RepairName) && - x.RepairName == model.RepairName) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Repairs.Include(x => x.Components).ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RepairName) && x.RepairName == model.RepairName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public RepairViewModel? Insert(RepairBindingModel model) { using var context = new RenovationWorkDatabase(); - var newRepair = Repair.Create(context, model); - if (newRepair == null) + var newEngine = Repair.Create(context, model); + if (newEngine == null) { return null; } - context.Repairs.Add(newRepair); + context.Repairs.Add(newEngine); context.SaveChanges(); - return newRepair.GetViewModel; + return newEngine.GetViewModel; } public RepairViewModel? Update(RepairBindingModel model) { @@ -66,17 +52,17 @@ namespace RenovationWorkDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var repair = context.Repairs.FirstOrDefault(rec => + var engine = context.Repairs.FirstOrDefault(rec => rec.Id == model.Id); - if (repair == null) + if (engine == null) { return null; } - repair.Update(model); + engine.Update(model); context.SaveChanges(); - repair.UpdateComponents(context, model); + engine.UpdateComponents(context, model); transaction.Commit(); - return repair.GetViewModel; + return engine.GetViewModel; } catch { diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.Designer.cs similarity index 99% rename from RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs rename to RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.Designer.cs index 7830f94..60d0019 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.Designer.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using RenovationWorkDatabaseImplement; namespace RenovationWorkDatabaseImplement.Migrations { [DbContext(typeof(RenovationWorkDatabase))] - [Migration("20240405063901_InitialCreate")] + [Migration("20240418122550_InitialCreate")] partial class InitialCreate { /// diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.cs similarity index 100% rename from RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240405063901_InitialCreate.cs rename to RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240418122550_InitialCreate.cs diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs index 169c024..767a238 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Component.cs @@ -27,7 +27,7 @@ namespace RenovationWorkDatabaseImplement.Models { return null; } - return new Component() + return new Component { Id = model.Id, ComponentName = model.ComponentName, @@ -58,6 +58,5 @@ namespace RenovationWorkDatabaseImplement.Models ComponentName = ComponentName, Cost = Cost }; - } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs index a946e8e..bc93c79 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs @@ -35,17 +35,18 @@ namespace RenovationWorkDatabaseImplement.Models public DateTime? DateImplement { get; private set; } - public static Order Create(RenovationWorkDatabase context, OrderBindingModel Model) + public static Order Create(RenovationWorkDatabase context, OrderBindingModel model) { return new Order() { - Id = Model.Id, - RepairId = Model.RepairId, - Count = Model.Count, - Sum = Model.Sum, - Status = Model.Status, - DateCreate = Model.DateCreate, - DateImplement = Model.DateImplement, + Id = model.Id, + RepairId = model.RepairId, + Repair = context.Repairs.First(x => x.Id == model.RepairId), + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, }; } @@ -54,11 +55,7 @@ namespace RenovationWorkDatabaseImplement.Models if (Model == null) return; - Id = Model.Id; - RepairId = Model.RepairId; - Sum = Model.Sum; Status = Model.Status; - DateCreate = Model.DateCreate; DateImplement = Model.DateImplement; } -- 2.25.1