From c5d252b9a0d68399f1f7d56647140935d7fe3507 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 17 May 2024 01:07:06 +0400 Subject: [PATCH] 100% --- CarRepairShop/CarRepairShop.sln | 6 + .../BusinessLogics/OrderLogic.cs | 2 +- .../BindingModels/OrderBindingModel.cs | 2 +- .../ViewModels/OrderViewModel.cs | 2 +- .../CarRepairShopDatabase.cs | 25 +++ .../CarRepairShopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 84 +++++++++ .../Implements/OrderStorage.cs | 97 ++++++++++ .../Implements/RepairStorage.cs | 106 +++++++++++ .../20240516210419_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240516210419_InitialCreate.cs | 126 +++++++++++++ .../CarRepairShopDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 63 +++++++ .../Models/Order.cs | 72 ++++++++ .../Models/Repair.cs | 99 ++++++++++ .../Models/RepairComponent.cs | 22 +++ .../CarRepairShopView.csproj | 6 +- CarRepairShop/CarRepairShopView/Program.cs | 2 +- 18 files changed, 1071 insertions(+), 5 deletions(-) create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabase.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Implements/RepairStorage.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240516210419_InitialCreate.Designer.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240516210419_InitialCreate.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Migrations/CarRepairShopDatabaseModelSnapshot.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Models/Component.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Models/RepairComponent.cs diff --git a/CarRepairShop/CarRepairShop.sln b/CarRepairShop/CarRepairShop.sln index f9d2f38..38586c7 100644 --- a/CarRepairShop/CarRepairShop.sln +++ b/CarRepairShop/CarRepairShop.sln @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopBusinessLogic" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{049AFC79-BBE4-4AFB-AF83-7D95D3BB0A04}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopDatabaseImplement", "CarRepairShopDatabaseImplement\CarRepairShopDatabaseImplement.csproj", "{A0043A9D-5C9A-4FC0-AAD2-E779D184D2D5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {049AFC79-BBE4-4AFB-AF83-7D95D3BB0A04}.Debug|Any CPU.Build.0 = Debug|Any CPU {049AFC79-BBE4-4AFB-AF83-7D95D3BB0A04}.Release|Any CPU.ActiveCfg = Release|Any CPU {049AFC79-BBE4-4AFB-AF83-7D95D3BB0A04}.Release|Any CPU.Build.0 = Release|Any CPU + {A0043A9D-5C9A-4FC0-AAD2-E779D184D2D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0043A9D-5C9A-4FC0-AAD2-E779D184D2D5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0043A9D-5C9A-4FC0-AAD2-E779D184D2D5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0043A9D-5C9A-4FC0-AAD2-E779D184D2D5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs index 5253fc5..5b1e47c 100644 --- a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -113,7 +113,7 @@ namespace CarRepairShopBusinessLogic.BusinessLogics model.Status = newStatus; if (model.Status == OrderStatus.Готов) { - model.DateImplement = DateTime.Now; + model.DateImplement = DateTime.Now.ToUniversalTime(); } else { model.DateImplement = order.DateImplement; diff --git a/CarRepairShop/CarRepairShopContracts/BindingModels/OrderBindingModel.cs b/CarRepairShop/CarRepairShopContracts/BindingModels/OrderBindingModel.cs index 314d4ab..1cc7e4b 100644 --- a/CarRepairShop/CarRepairShopContracts/BindingModels/OrderBindingModel.cs +++ b/CarRepairShop/CarRepairShopContracts/BindingModels/OrderBindingModel.cs @@ -15,7 +15,7 @@ namespace CarRepairShopContracts.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.Now.ToUniversalTime(); public DateTime? DateImplement { get; set; } } } diff --git a/CarRepairShop/CarRepairShopContracts/ViewModels/OrderViewModel.cs b/CarRepairShop/CarRepairShopContracts/ViewModels/OrderViewModel.cs index 5e39739..b632706 100644 --- a/CarRepairShop/CarRepairShopContracts/ViewModels/OrderViewModel.cs +++ b/CarRepairShop/CarRepairShopContracts/ViewModels/OrderViewModel.cs @@ -23,7 +23,7 @@ namespace CarRepairShopContracts.ViewModels [DisplayName("Статус")] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; [DisplayName("Дата создания")] - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.Now.ToUniversalTime(); [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } } diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabase.cs b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabase.cs new file mode 100644 index 0000000..8aae17e --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabase.cs @@ -0,0 +1,25 @@ +using CarRepairShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarRepairShopDatabaseImplement +{ + public class CarRepairShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@"Host=localhost;Database=mydb;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; } + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj new file mode 100644 index 0000000..e3b1a13 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net8.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ComponentStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..4b8d913 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,84 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDatabaseImplement.Models; + +namespace CarRepairShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new CarRepairShopDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new CarRepairShopDatabase(); + 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 CarRepairShopDatabase(); + 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 CarRepairShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new CarRepairShopDatabase(); + 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 CarRepairShopDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..59b5dd8 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,97 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarRepairShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new CarRepairShopDatabase(); + 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 CarRepairShopDatabase(); + 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 null; + } + using var context = new CarRepairShopDatabase(); + return context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new CarRepairShopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new CarRepairShopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new CarRepairShopDatabase(); + 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; + } + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/RepairStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/RepairStorage.cs new file mode 100644 index 0000000..dd9c37d --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/RepairStorage.cs @@ -0,0 +1,106 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarRepairShopDatabaseImplement.Implements +{ + public class RepairStorage : IRepairStorage + { + public List GetFullList() + { + using var context = new CarRepairShopDatabase(); + 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 CarRepairShopDatabase(); + 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 CarRepairShopDatabase(); + 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 CarRepairShopDatabase(); + 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 CarRepairShopDatabase(); + 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 CarRepairShopDatabase(); + 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; + } + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240516210419_InitialCreate.Designer.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240516210419_InitialCreate.Designer.cs new file mode 100644 index 0000000..e23a1bb --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240516210419_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using CarRepairShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace CarRepairShopDatabaseImplement.Migrations +{ + [DbContext(typeof(CarRepairShopDatabase))] + [Migration("20240516210419_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.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("CarRepairShopDatabaseImplement.Models.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.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.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("CarRepairShopDatabaseImplement.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("CarRepairShopDatabaseImplement.Models.Order", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240516210419_InitialCreate.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240516210419_InitialCreate.cs new file mode 100644 index 0000000..5b1e1b3 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240516210419_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace CarRepairShopDatabaseImplement.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 with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with 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/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/CarRepairShopDatabaseModelSnapshot.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/CarRepairShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..d08f553 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/CarRepairShopDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using CarRepairShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace CarRepairShopDatabaseImplement.Migrations +{ + [DbContext(typeof(CarRepairShopDatabase))] + partial class CarRepairShopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.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("CarRepairShopDatabaseImplement.Models.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.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.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("CarRepairShopDatabaseImplement.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("CarRepairShopDatabaseImplement.Models.Order", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Component.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..60ddee1 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace CarRepairShopDatabaseImplement.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 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 + }; + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..36a2d6b --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,72 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Enums; +using CarRepairShopDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace CarRepairShopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + + [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 virtual Repair Repair { 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, + RepairName = Repair.RepairName + }; + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs new file mode 100644 index 0000000..50495c0 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs @@ -0,0 +1,99 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace CarRepairShopDatabaseImplement.Models +{ + 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(); + + [ForeignKey("RepairId")] + public virtual List Orders { get; set; } = new(); + + public static Repair Create(CarRepairShopDatabase 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(CarRepairShopDatabase 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; + } + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/RepairComponent.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/RepairComponent.cs new file mode 100644 index 0000000..e064833 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/RepairComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace CarRepairShopDatabaseImplement.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(); + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopView/CarRepairShopView.csproj b/CarRepairShop/CarRepairShopView/CarRepairShopView.csproj index e32faaf..a008d26 100644 --- a/CarRepairShop/CarRepairShopView/CarRepairShopView.csproj +++ b/CarRepairShop/CarRepairShopView/CarRepairShopView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,7 +20,7 @@ - + \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopView/Program.cs b/CarRepairShop/CarRepairShopView/Program.cs index 2793834..051cbfc 100644 --- a/CarRepairShop/CarRepairShopView/Program.cs +++ b/CarRepairShop/CarRepairShopView/Program.cs @@ -1,6 +1,6 @@ using CarRepairShopContracts.BusinessLogicsContracts; using CarRepairShopContracts.StoragesContracts; -using CarRepairShopFileImplement.Implements; +using CarRepairShopDatabaseImplement.Implements; using CarRepairShopBusinessLogic.BusinessLogics; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -- 2.25.1