PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantFileImplement/Implements/OrderStorage.cs

157 lines
4.7 KiB
C#
Raw Normal View History

2024-02-25 01:22:22 +04:00
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
2024-02-26 10:39:39 +04:00
.Select(x => GetViewModel(x))
2024-02-25 01:22:22 +04:00
.ToList();
}
/// <summary>
/// Получение фильтрованного списка
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2024-03-25 00:53:24 +04:00
if (!model.Id.HasValue || !model.DateFrom.HasValue || !model.DateTo.HasValue)
2024-02-25 01:22:22 +04:00
{
return new();
}
2024-03-25 00:53:24 +04:00
if (model.DateFrom.HasValue)
{
return _source.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => GetViewModel(x))
.ToList();
}
2024-02-25 01:22:22 +04:00
return _source.Orders
.Where(x => x.Id.Equals(model.Id))
2024-02-26 10:39:39 +04:00
.Select(x => GetViewModel(x))
2024-02-25 01:22:22 +04:00
.ToList();
}
/// <summary>
/// Получение элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
2024-02-26 10:39:39 +04:00
return GetViewModel(_source.Orders
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)));
2024-02-25 01:22:22 +04:00
}
/// <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();
2024-02-26 10:39:39 +04:00
return GetViewModel(newOrder);
2024-02-25 01:22:22 +04:00
}
/// <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();
2024-02-26 10:39:39 +04:00
return GetViewModel(order);
2024-02-25 01:22:22 +04:00
}
/// <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();
2024-02-26 10:39:39 +04:00
return GetViewModel(element);
2024-02-25 01:22:22 +04:00
}
return null;
}
2024-02-26 10:39:39 +04:00
/// <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;
}
2024-02-25 01:22:22 +04:00
}
}