From eebaf97e7848cae380eb56e050f3c33cd000e762 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Thu, 14 Mar 2024 18:53:30 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CarRepairShop/CarRepairShop.sln | 8 +- .../CarRepairShop/CarRepairShop.csproj | 5 + .../CarRepairShop/FormMain.Designer.cs | 4 +- CarRepairShop/CarRepairShop/Program.cs | 2 +- .../CarRepairShopDatabaseImplement.csproj | 27 +++ .../Implements/ComponentStorage.cs | 84 +++++++++ .../Implements/OrderStorage.cs | 94 ++++++++++ .../Implements/RepairStorage.cs | 105 +++++++++++ .../20240312202857_InitialCreate.Designer.cs | 169 ++++++++++++++++++ .../20240312202857_InitialCreate.cs | 125 +++++++++++++ .../RepairsShopDatabaseModelSnapshot.cs | 162 +++++++++++++++++ .../Models/Component.cs | 56 ++++++ .../Models/Order.cs | 62 +++++++ .../Models/Repair.cs | 89 +++++++++ .../Models/RepairComponent.cs | 18 ++ .../RepairsShopDatabase.cs | 22 +++ 16 files changed, 1028 insertions(+), 4 deletions(-) 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/20240312202857_InitialCreate.Designer.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.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 create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/RepairsShopDatabase.cs diff --git a/CarRepairShop/CarRepairShop.sln b/CarRepairShop/CarRepairShop.sln index ce40891..a02c51f 100644 --- a/CarRepairShop/CarRepairShop.sln +++ b/CarRepairShop/CarRepairShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopBusinessLogic" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopListImplement", "CarRepairShopListImplement\CarRepairShopListImplement.csproj", "{687C5C03-1B68-494D-8006-12862232D229}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{53D685FB-4609-4562-A930-49FAAB77BC9B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{53D685FB-4609-4562-A930-49FAAB77BC9B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopDatabaseImplement", "CarRepairShopDatabaseImplement\CarRepairShopDatabaseImplement.csproj", "{F1BAC88A-6D0D-4276-92C4-757F48D0D80F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {53D685FB-4609-4562-A930-49FAAB77BC9B}.Debug|Any CPU.Build.0 = Debug|Any CPU {53D685FB-4609-4562-A930-49FAAB77BC9B}.Release|Any CPU.ActiveCfg = Release|Any CPU {53D685FB-4609-4562-A930-49FAAB77BC9B}.Release|Any CPU.Build.0 = Release|Any CPU + {F1BAC88A-6D0D-4276-92C4-757F48D0D80F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F1BAC88A-6D0D-4276-92C4-757F48D0D80F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F1BAC88A-6D0D-4276-92C4-757F48D0D80F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F1BAC88A-6D0D-4276-92C4-757F48D0D80F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CarRepairShop/CarRepairShop/CarRepairShop.csproj b/CarRepairShop/CarRepairShop/CarRepairShop.csproj index 20eefb7..cc9a359 100644 --- a/CarRepairShop/CarRepairShop/CarRepairShop.csproj +++ b/CarRepairShop/CarRepairShop/CarRepairShop.csproj @@ -9,11 +9,16 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/CarRepairShop/CarRepairShop/FormMain.Designer.cs b/CarRepairShop/CarRepairShop/FormMain.Designer.cs index eb2ee1b..457ce9f 100644 --- a/CarRepairShop/CarRepairShop/FormMain.Designer.cs +++ b/CarRepairShop/CarRepairShop/FormMain.Designer.cs @@ -62,14 +62,14 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(182, 26); + компонентыToolStripMenuItem.Size = new Size(224, 26); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += ComponentsToolStripMenuItem_Click; // // ремонтToolStripMenuItem // ремонтToolStripMenuItem.Name = "ремонтToolStripMenuItem"; - ремонтToolStripMenuItem.Size = new Size(182, 26); + ремонтToolStripMenuItem.Size = new Size(224, 26); ремонтToolStripMenuItem.Text = "Ремонт"; ремонтToolStripMenuItem.Click += RepairToolStripMenuItem_Click; // diff --git a/CarRepairShop/CarRepairShop/Program.cs b/CarRepairShop/CarRepairShop/Program.cs index 5f77e56..2149f23 100644 --- a/CarRepairShop/CarRepairShop/Program.cs +++ b/CarRepairShop/CarRepairShop/Program.cs @@ -1,7 +1,7 @@ using CarRepairShopBusinessLogic; using CarRepairShopContracts.BusinessLogicsContracts; using CarRepairShopContracts.StoragesContracts; -using CarRepairShopFileImplement.Implements; +using CarRepairShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj new file mode 100644 index 0000000..82f4fe9 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj @@ -0,0 +1,27 @@ + + + + net6.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..76ba22b --- /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 RepairsShopDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new RepairsShopDatabase(); + 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 RepairsShopDatabase(); + 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 RepairsShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new RepairsShopDatabase(); + 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 RepairsShopDatabase(); + 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..e4fdbf1 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,94 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDatabaseImplement.Models; + +namespace CarRepairShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new RepairsShopDatabase(); + return context.Orders + .Select(x => AccessRepairsStorage(x.GetViewModel)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new RepairsShopDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessRepairsStorage(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new RepairsShopDatabase(); + return AccessRepairsStorage(context.Orders.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 RepairsShopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessRepairsStorage(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new RepairsShopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessRepairsStorage(order.GetViewModel); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new RepairsShopDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessRepairsStorage(element.GetViewModel); + } + return null; + } + + public static OrderViewModel AccessRepairsStorage(OrderViewModel model) + { + if (model == null) + return null; + using var context = new RepairsShopDatabase(); + foreach (var Repairs in context.Repairs) + { + if (Repairs.Id == model.RepairId) + { + model.RepairName = Repairs.RepairName; + break; + } + } + return model; + } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/RepairStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/RepairStorage.cs new file mode 100644 index 0000000..efca0e8 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/RepairStorage.cs @@ -0,0 +1,105 @@ +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 RepairsShopDatabase(); + 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 RepairsShopDatabase(); + 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 RepairsShopDatabase(); + 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 RepairsShopDatabase(); + 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 RepairsShopDatabase(); + 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 RepairsShopDatabase(); + 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/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.Designer.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.Designer.cs new file mode 100644 index 0000000..9fe4b77 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using CarRepairShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace CarRepairShopDatabaseImplement.Migrations +{ + [DbContext(typeof(RepairsShopDatabase))] + [Migration("20240312202857_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", null) + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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/20240312202857_InitialCreate.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.cs new file mode 100644 index 0000000..fe70c1e --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#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: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Repairs", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + RepairName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Repairs", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true), + RepairId = table.Column(type: "int", nullable: false) + }, + 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: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + RepairId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_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/RepairsShopDatabaseModelSnapshot.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..6b0390e --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs @@ -0,0 +1,162 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +#nullable disable + +namespace CarRepairShopDatabaseImplement.Migrations +{ + [DbContext(typeof(RepairsShopDatabase))] + partial class CarRepairShopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", null) + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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..fdf1ef2 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +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 + }; + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..361f0d4 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,62 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Enums; +using System.ComponentModel.DataAnnotations; + +namespace CarRepairShopDatabaseImplement.Models +{ + public class Order + { + public int Id { get; private set; } + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } + [Required] + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + [Required] + public int RepairId { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + RepairId = model.RepairId, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + RepairId = RepairId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs new file mode 100644 index 0000000..e979943 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs @@ -0,0 +1,89 @@ +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(RepairsShopDatabase 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(RepairsShopDatabase 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/CarRepairShop/CarRepairShopDatabaseImplement/Models/RepairComponent.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/RepairComponent.cs new file mode 100644 index 0000000..c328712 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/RepairComponent.cs @@ -0,0 +1,18 @@ + +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(); + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/RepairsShopDatabase.cs b/CarRepairShop/CarRepairShopDatabaseImplement/RepairsShopDatabase.cs new file mode 100644 index 0000000..089eea5 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/RepairsShopDatabase.cs @@ -0,0 +1,22 @@ +using CarRepairShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarRepairShopDatabaseImplement +{ + public class RepairsShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=RepairsShopDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True" + ); + } + 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; } + } +} -- 2.25.1 From fc20462d0a2c2f6a40235f5408a404221fc2db3f Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sun, 24 Mar 2024 20:29:38 +0400 Subject: [PATCH 2/5] Correct --- .../Implements/OrderStorage.cs | 32 +++++-------------- .../RepairsShopDatabaseModelSnapshot.cs | 3 +- .../Models/Order.cs | 11 +++---- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs index e4fdbf1..15d2e6a 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs @@ -12,7 +12,7 @@ namespace CarRepairShopDatabaseImplement.Implements { using var context = new RepairsShopDatabase(); return context.Orders - .Select(x => AccessRepairsStorage(x.GetViewModel)) + .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -24,7 +24,7 @@ namespace CarRepairShopDatabaseImplement.Implements using var context = new RepairsShopDatabase(); return context.Orders .Where(x => x.Id == model.Id) - .Select(x => AccessRepairsStorage(x.GetViewModel)) + .Select(x => x.GetViewModel) .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -34,19 +34,19 @@ namespace CarRepairShopDatabaseImplement.Implements return null; } using var context = new RepairsShopDatabase(); - return AccessRepairsStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { - var newOrder = Order.Create(model); + using var context = new RepairsShopDatabase(); + var newOrder = Order.Create(context, model); if (newOrder == null) { return null; } - using var context = new RepairsShopDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return AccessRepairsStorage(newOrder.GetViewModel); + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { @@ -59,7 +59,7 @@ namespace CarRepairShopDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - return AccessRepairsStorage(order.GetViewModel); + return order.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { @@ -70,25 +70,9 @@ namespace CarRepairShopDatabaseImplement.Implements { context.Orders.Remove(element); context.SaveChanges(); - return AccessRepairsStorage(element.GetViewModel); + return element.GetViewModel; } return null; } - - public static OrderViewModel AccessRepairsStorage(OrderViewModel model) - { - if (model == null) - return null; - using var context = new RepairsShopDatabase(); - foreach (var Repairs in context.Repairs) - { - if (Repairs.Id == model.RepairId) - { - model.RepairName = Repairs.RepairName; - break; - } - } - return model; - } } } diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs index 6b0390e..f0531b6 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs @@ -119,11 +119,12 @@ namespace CarRepairShopDatabaseImplement.Migrations modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b => { - b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", null) + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") .WithMany("Orders") .HasForeignKey("RepairId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.Navigation("Repair"); }); modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b => diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs index 361f0d4..6fca62a 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs @@ -19,13 +19,10 @@ namespace CarRepairShopDatabaseImplement.Models public DateTime? DateImplement { get; private set; } [Required] public int RepairId { get; private set; } + public virtual Repair Repair { get; set; } = new(); - public static Order? Create(OrderBindingModel model) + public static Order? Create(RepairsShopDatabase context, OrderBindingModel model) { - if (model == null) - { - return null; - } return new Order() { Id = model.Id, @@ -35,6 +32,7 @@ namespace CarRepairShopDatabaseImplement.Models DateCreate = model.DateCreate, DateImplement = model.DateImplement, RepairId = model.RepairId, + Repair = context.Repairs.FirstOrDefault(x => x.Id == model.RepairId) }; } @@ -51,12 +49,13 @@ namespace CarRepairShopDatabaseImplement.Models public OrderViewModel GetViewModel => new() { RepairId = RepairId, + RepairName = Repair.RepairName, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - Id = Id, + Id = Id }; } } -- 2.25.1 From 818f6791d904e88560ada09d5b6a0a9f7f5d1517 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sun, 24 Mar 2024 21:23:30 +0400 Subject: [PATCH 3/5] Correct2 --- .../CarRepairShopDatabaseImplement.csproj | 4 ---- .../Implements/OrderStorage.cs | 3 ++- ...esigner.cs => 20240324163100_InitialCreate.Designer.cs} | 6 ++++-- ...57_InitialCreate.cs => 20240324163100_InitialCreate.cs} | 0 .../Migrations/RepairsShopDatabaseModelSnapshot.cs | 7 ++++++- .../CarRepairShopDatabaseImplement/Models/Order.cs | 3 +-- 6 files changed, 13 insertions(+), 10 deletions(-) rename CarRepairShop/CarRepairShopDatabaseImplement/Migrations/{20240312202857_InitialCreate.Designer.cs => 20240324163100_InitialCreate.Designer.cs} (98%) rename CarRepairShop/CarRepairShopDatabaseImplement/Migrations/{20240312202857_InitialCreate.cs => 20240324163100_InitialCreate.cs} (100%) diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj index 82f4fe9..792b545 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj +++ b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj @@ -20,8 +20,4 @@ - - - - diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs index 15d2e6a..f6fc350 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs @@ -3,6 +3,7 @@ using CarRepairShopContracts.SearchModels; using CarRepairShopContracts.StoragesContracts; using CarRepairShopContracts.ViewModels; using CarRepairShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace CarRepairShopDatabaseImplement.Implements { @@ -11,7 +12,7 @@ namespace CarRepairShopDatabaseImplement.Implements public List GetFullList() { using var context = new RepairsShopDatabase(); - return context.Orders + return context.Orders.Include(x => x.Repair) .Select(x => x.GetViewModel) .ToList(); } diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.Designer.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.Designer.cs similarity index 98% rename from CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.Designer.cs rename to CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.Designer.cs index 9fe4b77..b8e7c4d 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.Designer.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace CarRepairShopDatabaseImplement.Migrations { [DbContext(typeof(RepairsShopDatabase))] - [Migration("20240312202857_InitialCreate")] + [Migration("20240324163100_InitialCreate")] partial class InitialCreate { /// @@ -126,11 +126,13 @@ namespace CarRepairShopDatabaseImplement.Migrations modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b => { - b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", null) + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") .WithMany("Orders") .HasForeignKey("RepairId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Repair"); }); modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b => diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.cs similarity index 100% rename from CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240312202857_InitialCreate.cs rename to CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.cs diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs index f0531b6..e88cd6a 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/RepairsShopDatabaseModelSnapshot.cs @@ -1,13 +1,17 @@ // +using System; +using CarRepairShopDatabaseImplement; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; #nullable disable namespace CarRepairShopDatabaseImplement.Migrations { [DbContext(typeof(RepairsShopDatabase))] - partial class CarRepairShopDatabaseModelSnapshot : ModelSnapshot + partial class RepairsShopDatabaseModelSnapshot : ModelSnapshot { protected override void BuildModel(ModelBuilder modelBuilder) { @@ -124,6 +128,7 @@ namespace CarRepairShopDatabaseImplement.Migrations .HasForeignKey("RepairId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.Navigation("Repair"); }); diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs index 6fca62a..8a0e069 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs @@ -21,7 +21,7 @@ namespace CarRepairShopDatabaseImplement.Models public int RepairId { get; private set; } public virtual Repair Repair { get; set; } = new(); - public static Order? Create(RepairsShopDatabase context, OrderBindingModel model) + public static Order? Create(OrderBindingModel model) { return new Order() { @@ -32,7 +32,6 @@ namespace CarRepairShopDatabaseImplement.Models DateCreate = model.DateCreate, DateImplement = model.DateImplement, RepairId = model.RepairId, - Repair = context.Repairs.FirstOrDefault(x => x.Id == model.RepairId) }; } -- 2.25.1 From 0d6af8477a5a060b51882ad384b6f8430c8443ee Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Mon, 25 Mar 2024 20:17:43 +0400 Subject: [PATCH 4/5] Correct3 --- .../CarRepairShopDatabaseImplement/Implements/OrderStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs index f6fc350..23156b4 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs @@ -40,7 +40,7 @@ namespace CarRepairShopDatabaseImplement.Implements public OrderViewModel? Insert(OrderBindingModel model) { using var context = new RepairsShopDatabase(); - var newOrder = Order.Create(context, model); + var newOrder = Order.Create(model); if (newOrder == null) { return null; -- 2.25.1 From 97f1a44c76804caaa36262e20bba6bb63d732628 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Mon, 25 Mar 2024 21:45:56 +0400 Subject: [PATCH 5/5] Correct4 --- ...e.Designer.cs => 20240325173603_InitialCreate.Designer.cs} | 2 +- ...63100_InitialCreate.cs => 20240325173603_InitialCreate.cs} | 0 CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) rename CarRepairShop/CarRepairShopDatabaseImplement/Migrations/{20240324163100_InitialCreate.Designer.cs => 20240325173603_InitialCreate.Designer.cs} (99%) rename CarRepairShop/CarRepairShopDatabaseImplement/Migrations/{20240324163100_InitialCreate.cs => 20240325173603_InitialCreate.cs} (100%) diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.Designer.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240325173603_InitialCreate.Designer.cs similarity index 99% rename from CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.Designer.cs rename to CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240325173603_InitialCreate.Designer.cs index b8e7c4d..3a809f9 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.Designer.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240325173603_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace CarRepairShopDatabaseImplement.Migrations { [DbContext(typeof(RepairsShopDatabase))] - [Migration("20240324163100_InitialCreate")] + [Migration("20240325173603_InitialCreate")] partial class InitialCreate { /// diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240325173603_InitialCreate.cs similarity index 100% rename from CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240324163100_InitialCreate.cs rename to CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240325173603_InitialCreate.cs diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs index 8a0e069..86c3047 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Order.cs @@ -19,7 +19,7 @@ namespace CarRepairShopDatabaseImplement.Models public DateTime? DateImplement { get; private set; } [Required] public int RepairId { get; private set; } - public virtual Repair Repair { get; set; } = new(); + public virtual Repair? Repair { get; set; } public static Order? Create(OrderBindingModel model) { @@ -48,7 +48,7 @@ namespace CarRepairShopDatabaseImplement.Models public OrderViewModel GetViewModel => new() { RepairId = RepairId, - RepairName = Repair.RepairName, + RepairName = Repair?.RepairName ?? string.Empty, Count = Count, Sum = Sum, Status = Status, -- 2.25.1