Доделал

This commit is contained in:
[USERNAME] 2024-04-04 19:44:56 +04:00
parent 7f831ad75d
commit 95899c05ee
2 changed files with 16 additions and 13 deletions

View File

@ -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
};
}

View File

@ -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<OrderViewModel> 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<OrderViewModel> 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;
}