diff --git a/MotorPlant/MotorPlantDatabaseImplement/Order.cs b/MotorPlant/MotorPlantDatabaseImplement/Order.cs index 811f955..5c7ac48 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Order.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Order.cs @@ -20,6 +20,7 @@ namespace MotorPlantDatabaseImplement.Models public DateTime? DateImplement { get; private set; } [Required] public int EngineId { get; private set; } + public virtual Engine Engine { get; private set; } public static Order? Create(OrderBindingModel model) { @@ -58,6 +59,7 @@ namespace MotorPlantDatabaseImplement.Models DateCreate = DateCreate, DateImplement = DateImplement, Id = Id, + EngineName = Engine.EngineName }; } diff --git a/MotorPlant/MotorPlantDatabaseImplement/OrderStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/OrderStorage.cs index 1f12ebe..b9213de 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/OrderStorage.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/OrderStorage.cs @@ -1,4 +1,5 @@ -using MotorPlantContracts.BindingModels; +using Microsoft.EntityFrameworkCore; +using MotorPlantContracts.BindingModels; using MotorPlantContracts.SearchModels; using MotorPlantContracts.StoragesContracts; using MotorPlantContracts.ViewModels; @@ -11,21 +12,21 @@ namespace MotorPlantDatabaseImplement.Implements public List GetFullList() { using var context = new MotorPlantDataBase(); - return context.Orders - .Select(x => AccessEngineStorage(x.GetViewModel)) - .ToList(); + return context.Orders.Include(x => x.Engine).Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue) + if (!model.DateFrom.HasValue || !model.DateTo.HasValue) { return new(); } using var context = new MotorPlantDataBase(); - return context.Orders - .Where(x => x.Id == model.Id) - .Select(x => AccessEngineStorage(x.GetViewModel)) - .ToList(); + if (model.DateFrom.HasValue) + { + return context.Orders.Include(x => x.Engine).Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList(); + } + return context.Orders.Where(x => x.Id == model.Id).Include(x => x.Engine) + .Select(x => x.GetViewModel).ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) { @@ -34,7 +35,7 @@ namespace MotorPlantDatabaseImplement.Implements return null; } using var context = new MotorPlantDataBase(); - return AccessEngineStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + return context.Orders.Include(x => x.Engine).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { @@ -46,7 +47,7 @@ namespace MotorPlantDatabaseImplement.Implements using var context = new MotorPlantDataBase(); context.Orders.Add(newOrder); context.SaveChanges(); - return AccessEngineStorage(newOrder.GetViewModel); + return context.Orders.Include(x => x.Engine).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { @@ -59,7 +60,7 @@ namespace MotorPlantDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - return AccessEngineStorage(order.GetViewModel); + return context.Orders.Include(x => x.Engine).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { @@ -70,7 +71,7 @@ namespace MotorPlantDatabaseImplement.Implements { context.Orders.Remove(element); context.SaveChanges(); - return AccessEngineStorage(element.GetViewModel); + return element.GetViewModel; } return null; }