82 lines
2.7 KiB
C#
82 lines
2.7 KiB
C#
using MotorPlantContracts.BindingModels;
|
|
using MotorPlantContracts.SearchModels;
|
|
using MotorPlantContracts.StoragesContracts;
|
|
using MotorPlantContracts.VeiwModels;
|
|
using MotorPlantFileImplement.Models;
|
|
namespace MotorPlantFileImplement.Implements
|
|
{
|
|
public class OrderStorage : IOrderStorage
|
|
{
|
|
private readonly DataFileSingleton source;
|
|
public OrderStorage()
|
|
{
|
|
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)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
return AttachEngineName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
|
|
}
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
{
|
|
model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1;
|
|
var newOrder = Order.Create(model);
|
|
if (newOrder == null)
|
|
{
|
|
return null;
|
|
}
|
|
source.Orders.Add(newOrder);
|
|
source.SaveOrders();
|
|
return AttachEngineName(newOrder.GetViewModel);
|
|
}
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
{
|
|
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
if (order == null)
|
|
{
|
|
return null;
|
|
}
|
|
order.Update(model);
|
|
source.SaveOrders();
|
|
return AttachEngineName(order.GetViewModel);
|
|
}
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
{
|
|
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
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;
|
|
}
|
|
var engine = source.Engines.FirstOrDefault(x => x.Id == model.EngineId);
|
|
if (engine != null)
|
|
{
|
|
model.EngineName = engine.EngineName;
|
|
}
|
|
return model;
|
|
}
|
|
}
|
|
}
|