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

150 lines
4.7 KiB
C#
Raw Normal View History

2024-03-10 01:33:27 +04:00
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.StoragesContracts;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantDatabaseImplement.Implements
{
/// <summary>
/// Реализация интерфейса хранилища для заказов
/// </summary>
public class OrderStorage : IOrderStorage
{
/// <summary>
/// Получение полного списка
/// </summary>
/// <returns></returns>
public List<OrderViewModel> GetFullList()
{
using var context = new AircraftPlantDatabase();
return context.Orders
.Select(x => GetViewModel(x))
.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-03-10 01:33:27 +04:00
{
return new();
}
using var context = new AircraftPlantDatabase();
2024-03-25 00:53:24 +04:00
if (model.DateFrom.HasValue)
{
return context.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => GetViewModel(x))
.ToList();
}
2024-03-10 01:33:27 +04:00
return context.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;
}
using var context = new AircraftPlantDatabase();
return GetViewModel(context.Orders
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)));
}
/// <summary>
/// Добавление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new AircraftPlantDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return GetViewModel(newOrder);
}
/// <summary>
/// Редактирование элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new AircraftPlantDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return GetViewModel(order);
}
/// <summary>
/// Удаление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new AircraftPlantDatabase();
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return GetViewModel(element);
}
return null;
}
/// <summary>
/// Получение модели заказа
/// </summary>
/// <param name="order"></param>
/// <returns></returns>
private static OrderViewModel GetViewModel(Order order)
{
using var context = new AircraftPlantDatabase();
var viewModel = order.GetViewModel;
var plane = context.Planes.FirstOrDefault(x => x.Id == order.PlaneId);
if (plane != null)
{
viewModel.PlaneName = plane.PlaneName;
}
return viewModel;
}
}
}