From 1cb89e24d726a3dce461a29a931a74412de9a57d Mon Sep 17 00:00:00 2001 From: Salikh Date: Thu, 11 Apr 2024 18:22:36 +0400 Subject: [PATCH] second commit --- .../Implements/OrderStorage.cs | 123 +++++++++--------- ...r.cs => 20240411140319_NewMig.Designer.cs} | 8 +- ...dMigration.cs => 20240411140319_NewMig.cs} | 2 +- .../MotorPlantDatabaseModelSnapshot.cs | 4 +- .../Models/Order.cs | 2 - 5 files changed, 69 insertions(+), 70 deletions(-) rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240411122818_InitHardMigration.Designer.cs => 20240411140319_NewMig.Designer.cs} (98%) rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240411122818_InitHardMigration.cs => 20240411140319_NewMig.cs} (99%) diff --git a/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs index 08c17f1..ae20ebb 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Implements/OrderStorage.cs @@ -13,8 +13,10 @@ namespace MotorPlantDatabaseImplement.Implements public List GetFullList() { using var context = new MotorPlantDatabase(); - return context.Orders.Include(x => x.Engine).Select(x => x.GetViewModel).ToList(); - } + return context.Orders + .Select(x => AccessEngineStorage(x.GetViewModel)) + .ToList(); + } public List GetFilteredList(OrderSearchModel model) { if (!model.Id.HasValue) @@ -22,67 +24,70 @@ namespace MotorPlantDatabaseImplement.Implements return new(); } using var context = new MotorPlantDatabase(); - return context.Orders - .Include(x => x.Engine) - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); - } + return context.Orders.Where(x => x.Id == model.Id).Select(x => AccessEngineStorage(x.GetViewModel)).ToList(); + } public OrderViewModel? GetElement(OrderSearchModel model) { - if (!model.Id.HasValue) - { - return null; - } - using var context = new MotorPlantDatabase(); - return context.Orders.Include(x => x.Engine) - .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; - } + if (!model.Id.HasValue) + { + return null; + } + using var context = new MotorPlantDatabase(); + return AccessEngineStorage(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 MotorPlantDatabase(); - context.Orders.Add(newOrder); - context.SaveChanges(); - return context.Orders - .Include(x => x.Engine) - .FirstOrDefault(x => x.Id == newOrder.Id) - ?.GetViewModel; - } + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new MotorPlantDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessEngineStorage(newOrder.GetViewModel); + } public OrderViewModel? Update(OrderBindingModel model) { - using var context = new MotorPlantDatabase(); - 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.Engine) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } - public OrderViewModel? Delete(OrderBindingModel model) - { - using var context = new MotorPlantDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - var deletedElement = context.Orders - .Include(x => x.Engine) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - context.Orders.Remove(element); - context.SaveChanges(); - return deletedElement; - } - return null; - } - } + using var context = new MotorPlantDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessEngineStorage(order.GetViewModel); + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new MotorPlantDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessEngineStorage(element.GetViewModel); + } + return null; + } + + public static OrderViewModel AccessEngineStorage(OrderViewModel model) + { + if (model == null) + return null; + using var context = new MotorPlantDatabase(); + foreach (var Engine in context.Engines) + { + if (Engine.Id == model.EngineId) + { + model.EngineName = Engine.EngineName; + break; + } + } + return model; + } + } } diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.Designer.cs similarity index 98% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.Designer.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.Designer.cs index 8b2c5f9..1151db6 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.Designer.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.Designer.cs @@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace MotorPlantDatabaseImplement.Migrations { [DbContext(typeof(MotorPlantDatabase))] - [Migration("20240411122818_InitHardMigration")] - partial class InitHardMigration + [Migration("20240411140319_NewMig")] + partial class NewMig { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -198,13 +198,11 @@ namespace MotorPlantDatabaseImplement.Migrations modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => { - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") + b.HasOne("MotorPlantDatabaseImplement.Models.Engine", null) .WithMany("Orders") .HasForeignKey("EngineId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - - b.Navigation("Engine"); }); modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b => diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.cs similarity index 99% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.cs index 46dc8e1..5a17410 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411122818_InitHardMigration.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240411140319_NewMig.cs @@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace MotorPlantDatabaseImplement.Migrations { /// - public partial class InitHardMigration : Migration + public partial class NewMig : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs index 51fdedd..e59cde0 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDatabaseModelSnapshot.cs @@ -195,13 +195,11 @@ namespace MotorPlantDatabaseImplement.Migrations modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => { - b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") + b.HasOne("MotorPlantDatabaseImplement.Models.Engine", null) .WithMany("Orders") .HasForeignKey("EngineId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - - b.Navigation("Engine"); }); modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b => diff --git a/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs index 91450c6..7355218 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Models/Order.cs @@ -27,8 +27,6 @@ namespace MotorPlantDatabaseImplement.Models public DateTime? DateImplement { get; private set; } - public virtual Engine Engine { get; set; } - public static Order? Create(OrderBindingModel? model) { if (model == null)