исправила orderstorage

This commit is contained in:
song2remaster 2024-05-01 19:51:53 +04:00
parent 93e910c83c
commit e49e1fbcb0
3 changed files with 70 additions and 39 deletions

View File

@ -1,4 +1,5 @@
using MotorPlantContracts.BindingModels; using Microsoft.EntityFrameworkCore;
using MotorPlantContracts.BindingModels;
using MotorPlantContracts.SearchModels; using MotorPlantContracts.SearchModels;
using MotorPlantContracts.StoragesContracts; using MotorPlantContracts.StoragesContracts;
using MotorPlantContracts.VeiwModels; using MotorPlantContracts.VeiwModels;
@ -33,12 +34,24 @@ namespace MotorPlantDatabaseImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue && !model.DateFrom.HasValue)
{ {
return new(); return new();
} }
using var context = new MotorPlantDatabase(); using var context = new MotorPlantDatabase();
return context.Orders.Where(x => x.Id == model.Id).Select(x => 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
.Include(x => x.Engine)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
} }
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()
{ {

View File

@ -43,20 +43,35 @@ namespace MotorPlantListImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{ {
var result = new List<OrderViewModel>(); var result = new List<OrderViewModel>();
if (model == null || !model.Id.HasValue)
if (!model.Id.HasValue) {
return result;
}
if (!model.DateFrom.HasValue || !model.DateTo.HasValue)
{ {
return result; return result;
} }
foreach (var order in _source.Orders) foreach (var order in _source.Orders)
{ {
if (model.Id.HasValue && order.Id == model.Id) if (order.Id == model.Id || (model.DateFrom <= order.DateCreate && order.DateCreate <= model.DateTo))
{ {
result.Add(order.GetViewModel); result.Add(AttachEngineName(order.GetViewModel));
} }
} }
return result; return result;
} }
private OrderViewModel AttachEngineName(OrderViewModel model)
{
foreach (var Engine in _source.Engines)
{
if (Engine.Id == model.EngineId)
{
model.EngineName = Engine.EngineName;
return model;
}
}
return model;
}
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()
{ {
var result = new List<OrderViewModel>(); var result = new List<OrderViewModel>();

View File

@ -12,23 +12,32 @@ namespace MotorPlantFileImplement.Implements
{ {
source = DataFileSingleton.GetInstance(); source = DataFileSingleton.GetInstance();
} }
public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachEngineName(x.GetViewModel)).ToList();
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return source.Orders.Where(x => x.Id == model.Id).Select(x => AttachEngineName(x.GetViewModel)).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model) public OrderViewModel? GetElement(OrderSearchModel model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue)
{ {
return new(); return null;
} }
return AttachEngineName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); return GetViewModel(source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id));
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
if (!model.Id.HasValue && !model.DateFrom.HasValue || !model.DateTo.HasValue)
{
return new();
}
return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList();
}
return source.Orders
.Where(x => x.Id == model.Id || (model.DateTo >= x.DateCreate && model.DateFrom <= x.DateCreate))
.Select(x => GetViewModel(x))
.ToList();
}
public List<OrderViewModel> GetFullList()
{
return source.Orders.Select(x => GetViewModel(x)).ToList();
} }
public OrderViewModel? Insert(OrderBindingModel model) public OrderViewModel? Insert(OrderBindingModel model)
{ {
@ -40,7 +49,7 @@ namespace MotorPlantFileImplement.Implements
} }
source.Orders.Add(newOrder); source.Orders.Add(newOrder);
source.SaveOrders(); source.SaveOrders();
return AttachEngineName(newOrder.GetViewModel); return GetViewModel(newOrder);
} }
public OrderViewModel? Update(OrderBindingModel model) public OrderViewModel? Update(OrderBindingModel model)
{ {
@ -51,31 +60,25 @@ namespace MotorPlantFileImplement.Implements
} }
order.Update(model); order.Update(model);
source.SaveOrders(); source.SaveOrders();
return AttachEngineName(order.GetViewModel); return GetViewModel(order);
} }
public OrderViewModel? Delete(OrderBindingModel model) public OrderViewModel? Delete(OrderBindingModel model)
{ {
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order != null) if (order == null)
{
source.Orders.Remove(order);
source.SaveOrders();
return AttachEngineName(order.GetViewModel);
}
return null;
}
private OrderViewModel? AttachEngineName(OrderViewModel? model)
{
if (model == null)
{ {
return null; return null;
} }
var engine = source.Engines.FirstOrDefault(x => x.Id == model.EngineId); order.Update(model);
if (engine != null) source.SaveOrders();
{ return GetViewModel(order);
model.EngineName = engine.EngineName; }
} private OrderViewModel GetViewModel(Order order)
return model; {
var viewModel = order.GetViewModel;
var engine = source.Engines.FirstOrDefault(x => x.Id == order.EngineId);
viewModel.EngineName = engine?.EngineName;
return viewModel;
} }
} }
} }