PIbd-22_Safiulova_K.N._Airc.../AircraftPlant/AircraftPlantFileImplement/OrderStorage.cs

97 lines
2.9 KiB
C#
Raw Normal View History

2024-03-13 00:15:40 +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;
2024-05-05 14:10:57 +04:00
using System.Xml.Linq;
2024-03-13 00:15:40 +04:00
namespace AircraftPlantFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton _source;
public OrderStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
2024-05-05 14:10:57 +04:00
return _source.Orders
.Select(x => GetViewModel(x))
.ToList();
2024-03-13 00:15:40 +04:00
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
2024-05-05 14:10:57 +04:00
return _source.Orders
.Where(x => x.Id.Equals(model.Id))
.Select(x => GetViewModel(x))
.ToList();
2024-03-13 00:15:40 +04:00
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
2024-05-05 14:10:57 +04:00
2024-03-13 00:15:40 +04:00
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;
}
2024-05-05 14:10:57 +04:00
2024-03-13 00:15:40 +04:00
_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;
}
2024-05-05 14:10:57 +04:00
2024-03-13 00:15:40 +04:00
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);
2024-05-05 14:10:57 +04:00
if (plane != null)
{
viewModel.PlaneName = plane.PlaneName;
}
2024-03-13 00:15:40 +04:00
return viewModel;
}
}
}