diff --git a/RenovationWork/RenovationWork.sln b/RenovationWork/RenovationWork.sln index aebfae6..f1518e2 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}") = "RenovationWorkView", "RenovationWorkView\RenovationWorkView.csproj", "{E3889A30-0232-4D10-9BBA-8BEE6D098008}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RenovationWorkFileImplement", "RenovationWorkFileImplement\RenovationWorkFileImplement.csproj", "{6E0F3EC0-B81C-442A-8511-F6D041C48576}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenovationWorkFileImplement", "RenovationWorkFileImplement\RenovationWorkFileImplement.csproj", "{6E0F3EC0-B81C-442A-8511-F6D041C48576}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RenovationWorkDatabaseImplement", "RenovationWorkDatabaseImplement\RenovationWorkDatabaseImplement.csproj", "{78A663E6-2611-449D-A33B-40348A47E28C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {6E0F3EC0-B81C-442A-8511-F6D041C48576}.Debug|Any CPU.Build.0 = Debug|Any CPU {6E0F3EC0-B81C-442A-8511-F6D041C48576}.Release|Any CPU.ActiveCfg = Release|Any CPU {6E0F3EC0-B81C-442A-8511-F6D041C48576}.Release|Any CPU.Build.0 = Release|Any CPU + {78A663E6-2611-449D-A33B-40348A47E28C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {78A663E6-2611-449D-A33B-40348A47E28C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {78A663E6-2611-449D-A33B-40348A47E28C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {78A663E6-2611-449D-A33B-40348A47E28C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/OrderLogic.cs b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/OrderLogic.cs index 0720cce..02eeaf4 100644 --- a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/OrderLogic.cs @@ -59,7 +59,7 @@ namespace RenovationWorkBusinessLogic.BusinessLogic model.Status = newStatus; if (model.Status == OrderStatus.Выдан) - model.DateImplement = DateTime.Now; + model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); if (_orderStorage.Update(model) == null) { diff --git a/RenovationWork/RenovationWorkContracts/BindingModels/OrderBindingModel.cs b/RenovationWork/RenovationWorkContracts/BindingModels/OrderBindingModel.cs index c035315..61edf13 100644 --- a/RenovationWork/RenovationWorkContracts/BindingModels/OrderBindingModel.cs +++ b/RenovationWork/RenovationWorkContracts/BindingModels/OrderBindingModel.cs @@ -16,7 +16,7 @@ namespace RenovationWorkContracts.BindingModels public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..f574e55 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,89 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SeatchModels; +using RenovationWorkContracts.StorageContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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..36fbcb0 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,105 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SeatchModels; +using RenovationWorkContracts.StorageContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new RenovationWorkDatabase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return GetViewModel(element); + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new RenovationWorkDatabase(); + + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new RenovationWorkDatabase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + using var context = new RenovationWorkDatabase(); + + return context.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + + using var context = new RenovationWorkDatabase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + return GetViewModel(newOrder); + } + + 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 GetViewModel(order); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new RenovationWorkDatabase(); + var element = context.Repairs.FirstOrDefault(x => x.Id == order.RepairId); + viewModel.RepairName = element.RepairName; + return viewModel; + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs new file mode 100644 index 0000000..c42fb73 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs @@ -0,0 +1,111 @@ +using Microsoft.EntityFrameworkCore; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SeatchModels; +using RenovationWorkContracts.StorageContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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/20230327210942_InitMigration.Designer.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230327210942_InitMigration.Designer.cs new file mode 100644 index 0000000..1fc1023 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230327210942_InitMigration.Designer.cs @@ -0,0 +1,156 @@ +// +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("20230327210942_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.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.Model.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.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.Model.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.Model.RepairComponent", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Model.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Model.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Repair", b => + { + b.Navigation("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230327210942_InitMigration.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230327210942_InitMigration.cs new file mode 100644 index 0000000..2be67c7 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230327210942_InitMigration.cs @@ -0,0 +1,115 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace RenovationWorkDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : 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: "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 with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", 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: "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_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..bfd0382 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs @@ -0,0 +1,153 @@ +// +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", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.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.Model.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.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.Model.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.Model.RepairComponent", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Model.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Model.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Repair", b => + { + b.Navigation("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Model/Component.cs b/RenovationWork/RenovationWorkDatabaseImplement/Model/Component.cs new file mode 100644 index 0000000..9173e22 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Model/Component.cs @@ -0,0 +1,68 @@ +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; + +namespace RenovationWorkDatabaseImplement.Model +{ + 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 RepairComponents { 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/Model/Order.cs b/RenovationWork/RenovationWorkDatabaseImplement/Model/Order.cs new file mode 100644 index 0000000..413f5ef --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Model/Order.cs @@ -0,0 +1,70 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Enums; +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement.Model +{ + public class Order : IOrderModel + { + [Required] + public int RepairId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public int Id { 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; + 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/Model/Repair.cs b/RenovationWork/RenovationWorkDatabaseImplement/Model/Repair.cs new file mode 100644 index 0000000..bdb6112 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Model/Repair.cs @@ -0,0 +1,101 @@ +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; + +namespace RenovationWorkDatabaseImplement.Model +{ + public class Repair : IRepairModel + { + 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 + { + if (_RepairComponents == null) + { + _RepairComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _RepairComponents; + } + } + + [ForeignKey("RepairId")] + public virtual List Components { get; set; } = new(); + + public static Repair Create(RenovationWorkDatabase context, RepairBindingModel model) + { + return new Repair() + { + Id = model.Id, + RepairName = model.RepairName, + Price = model.Price, + 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) + { + RepairName = model.RepairName; + Price = model.Price; + } + + public RepairViewModel GetViewModel => new() + { + Id = Id, + RepairName = RepairName, + 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/Model/RepairComponent.cs b/RenovationWork/RenovationWorkDatabaseImplement/Model/RepairComponent.cs new file mode 100644 index 0000000..c121ce0 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Model/RepairComponent.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement.Model +{ + 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..da2fdd4 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore; +using RenovationWorkDatabaseImplement.Model; +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=RenovationWorkDatabase; + Username=postgres; + Password=postgres"); + } + + base.OnConfiguring(optionsBuilder); + } + + 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 new file mode 100644 index 0000000..1011373 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/RenovationWork/RenovationWorkView/Program.cs b/RenovationWork/RenovationWorkView/Program.cs index c346395..6bfe4c1 100644 --- a/RenovationWork/RenovationWorkView/Program.cs +++ b/RenovationWork/RenovationWorkView/Program.cs @@ -3,7 +3,7 @@ using PrecastConcretePlantView; using RenovationWorkBusinessLogic.BusinessLogic; using RenovationWorkContracts.BusinessLogicsContracts; using RenovationWorkContracts.StorageContracts; -using RenovationWorkFileImplement.Implements; +using RenovationWorkDatabaseImplement.Implements; using System; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/RenovationWork/RenovationWorkView/RenovationWorkView.csproj b/RenovationWork/RenovationWorkView/RenovationWorkView.csproj index 46ca26d..223334c 100644 --- a/RenovationWork/RenovationWorkView/RenovationWorkView.csproj +++ b/RenovationWork/RenovationWorkView/RenovationWorkView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -28,6 +32,7 @@ +