157 lines
4.7 KiB
C#
157 lines
4.7 KiB
C#
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;
|
|
|
|
namespace AircraftPlantFileImplement.Implements
|
|
{
|
|
/// <summary>
|
|
/// Реализация интерфейса хранилища для заказов
|
|
/// </summary>
|
|
public class OrderStorage : IOrderStorage
|
|
{
|
|
/// <summary>
|
|
/// Хранилище
|
|
/// </summary>
|
|
private readonly DataFileSingleton _source;
|
|
|
|
/// <summary>
|
|
/// Конструктор
|
|
/// </summary>
|
|
public OrderStorage()
|
|
{
|
|
_source = DataFileSingleton.GetInstance();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение полного списка
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<OrderViewModel> GetFullList()
|
|
{
|
|
return _source.Orders
|
|
.Select(x => GetViewModel(x))
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение фильтрованного списка
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue || !model.DateFrom.HasValue || !model.DateTo.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
|
|
if (model.DateFrom.HasValue)
|
|
{
|
|
return _source.Orders
|
|
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
|
|
.Select(x => GetViewModel(x))
|
|
.ToList();
|
|
}
|
|
|
|
return _source.Orders
|
|
.Where(x => x.Id.Equals(model.Id))
|
|
.Select(x => GetViewModel(x))
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return GetViewModel(_source.Orders
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Редактирование элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение модели заказа
|
|
/// </summary>
|
|
/// <param name="order"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|