118 lines
4.5 KiB
C#
118 lines
4.5 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
|
|
{
|
|
public class OrderStorage : IOrderStorage
|
|
{
|
|
private readonly DataFileSingleton _source;
|
|
public OrderStorage()
|
|
{
|
|
_source = DataFileSingleton.GetInstance();
|
|
}
|
|
public List<OrderViewModel> GetFullList()
|
|
{
|
|
return _source.Orders.Select(x => GetViewModel(x)).ToList();
|
|
}
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue || !model.DateFrom.HasValue || !model.DateTo.HasValue || !model.ClientId.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
else if (model.DateFrom.HasValue)
|
|
{
|
|
return _source.Orders
|
|
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
|
|
.Select(x => GetViewModel(x))
|
|
.ToList();
|
|
}
|
|
else if (model.ClientId.HasValue && !model.Id.HasValue)
|
|
{
|
|
return _source.Orders
|
|
.Where(x => x.ClientId == model.ClientId)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.ImplementerId.HasValue)
|
|
{
|
|
return _source.Orders
|
|
.Where(x => x.ImplementerId == model.ImplementerId)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.Status != null)
|
|
{
|
|
return _source.Orders
|
|
.Where(x => x.Status.Equals(model.Status))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
return _source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList();
|
|
}
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue && !model.ImplementerId.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
var order = _source.Orders.FirstOrDefault(x =>
|
|
model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId && model.Status != null && x.Status.Equals(model.Status)
|
|
|| model.Status == null && model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId
|
|
|| model.Id.HasValue && x.Id == model.Id);
|
|
return order?.GetViewModel != null ? order.GetViewModel : null;
|
|
}
|
|
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);
|
|
viewModel.PlaneName = plane?.PlaneName ?? string.Empty;
|
|
var client = _source.Clients.FirstOrDefault(x => x.Id == order.ClientId);
|
|
viewModel.ClientFIO = client?.ClientFIO ?? string.Empty;
|
|
return viewModel;
|
|
}
|
|
}
|
|
} |