diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs index 8a23f6d..450324f 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs @@ -3,6 +3,7 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; using IceCreamShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -16,8 +17,8 @@ namespace IceCreamShopDatabaseImplement.Implements public List GetFullList() { using var context = new IceCreamShopDataBase(); - return context.Orders - .Select(x => AccessIceCreamStorage(x.GetViewModel)) + return context.Orders.Include(x => x.IceCream) + .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -29,7 +30,7 @@ namespace IceCreamShopDatabaseImplement.Implements using var context = new IceCreamShopDataBase(); return context.Orders .Where(x => x.Id == model.Id) - .Select(x => AccessIceCreamStorage(x.GetViewModel)) + .Select(x => x.GetViewModel) .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -39,19 +40,19 @@ namespace IceCreamShopDatabaseImplement.Implements return null; } using var context = new IceCreamShopDataBase(); - return AccessIceCreamStorage(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 IceCreamShopDataBase(); + var newOrder = Order.Create(context, model); if (newOrder == null) { return null; } - using var context = new IceCreamShopDataBase(); context.Orders.Add(newOrder); context.SaveChanges(); - return AccessIceCreamStorage(newOrder.GetViewModel); + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { @@ -64,7 +65,7 @@ namespace IceCreamShopDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - return AccessIceCreamStorage(order.GetViewModel); + return order.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { @@ -75,21 +76,9 @@ namespace IceCreamShopDatabaseImplement.Implements { context.Orders.Remove(element); context.SaveChanges(); - return AccessIceCreamStorage(element.GetViewModel); + return element.GetViewModel; } return null; } - - public static OrderViewModel AccessIceCreamStorage(OrderViewModel model) - { - if (model == null) - return null; - using var context = new IceCreamShopDataBase(); - var iceCream = context.IceCreams.FirstOrDefault(x => (x.Id == model.IceCreamId)); - if (iceCream == null) - return model; - model.IceCreamName = iceCream.IceCreamName; - return model; - } } } diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.Designer.cs similarity index 97% rename from IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs rename to IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.Designer.cs index 655fb59..38becf5 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace IceCreamShopDatabaseImplement.Migrations { [DbContext(typeof(IceCreamShopDataBase))] - [Migration("20240304143043_InitialCreate")] + [Migration("20240318125446_InitialCreate")] partial class InitialCreate { /// @@ -145,11 +145,13 @@ namespace IceCreamShopDatabaseImplement.Migrations modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => { - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") .WithMany("Orders") .HasForeignKey("IceCreamId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("IceCream"); }); modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.cs similarity index 100% rename from IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.cs rename to IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.cs diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs index 447b981..8400a67 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs @@ -142,11 +142,13 @@ namespace IceCreamShopDatabaseImplement.Migrations modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => { - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") .WithMany("Orders") .HasForeignKey("IceCreamId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("IceCream"); }); modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs index a80e766..6bce4fd 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs @@ -25,8 +25,8 @@ namespace IceCreamShopDatabaseImplement.Models public DateTime? DateImplement { get; private set; } [Required] public int IceCreamId { get; private set; } - - public static Order? Create(OrderBindingModel model) + public virtual IceCream IceCream { get; private set; } + public static Order? Create(IceCreamShopDataBase context, OrderBindingModel model) { if (model == null) { @@ -41,6 +41,7 @@ namespace IceCreamShopDatabaseImplement.Models DateCreate = model.DateCreate, DateImplement = model.DateImplement, IceCreamId = model.IceCreamId, + IceCream = context.IceCreams.FirstOrDefault(x => x.Id == model.IceCreamId) }; } @@ -62,6 +63,7 @@ namespace IceCreamShopDatabaseImplement.Models Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, + IceCreamName = IceCream.IceCreamName, Id = Id, };