95 lines
3.0 KiB
C#
95 lines
3.0 KiB
C#
|
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
|
|||
|
{
|
|||
|
public class OrderStorage : IOrderStorage
|
|||
|
{
|
|||
|
public List<OrderViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new AircraftPlantDatabase();
|
|||
|
return context.Orders.Select(x => GetViewModel(x)).ToList();
|
|||
|
}
|
|||
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
using var context = new AircraftPlantDatabase();
|
|||
|
return context.Orders
|
|||
|
.Where(x => x.Id.Equals(model.Id))
|
|||
|
.Select(x => GetViewModel(x))
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
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)));
|
|||
|
}
|
|||
|
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);
|
|||
|
}
|
|||
|
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);
|
|||
|
}
|
|||
|
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;
|
|||
|
}
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|