using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.StoragesContracts;
using AircraftPlantContracts.ViewModels;
using AircraftPlantFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AircraftPlantFileImplement.Implements
{
///
/// Реализация интерфейса хранилища для заказов
///
public class OrderStorage : IOrderStorage
{
///
/// Хранилище
///
private readonly DataFileSingleton _source;
///
/// Конструктор
///
public OrderStorage()
{
_source = DataFileSingleton.GetInstance();
}
///
/// Получение полного списка
///
///
public List GetFullList()
{
return _source.Orders
.Select(x => GetViewModel(x))
.ToList();
}
///
/// Получение фильтрованного списка
///
///
///
public List GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return _source.Orders
.Where(x => x.Id.Equals(model.Id))
.Select(x => GetViewModel(x))
.ToList();
}
///
/// Получение элемента
///
///
///
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return GetViewModel(_source.Orders
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)));
}
///
/// Добавление элемента
///
///
///
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 GetViewModel(newOrder);
}
///
/// Редактирование элемента
///
///
///
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 GetViewModel(order);
}
///
/// Удаление элемента
///
///
///
public OrderViewModel? Delete(OrderBindingModel model)
{
var element = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Orders.Remove(element);
_source.SaveOrders();
return GetViewModel(element);
}
return null;
}
///
/// Получение модели заказа
///
///
///
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
var plane = _source.Planes.FirstOrDefault(x => x.Id == order.PlaneId);
if (plane != null)
{
viewModel.PlaneName = plane.PlaneName;
}
return viewModel;
}
}
}