PIbd-22_Safiulova_K.N._Airc.../AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs

96 lines
3.1 KiB
C#
Raw Normal View History

using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.StoragesContracts;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDatabaseImplement.Models;
2024-06-21 14:01:04 +04:00
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();
2024-03-26 19:43:48 +04:00
return context.Orders
2024-06-21 14:01:04 +04:00
.Select(x => GetViewModel(x))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
2024-06-21 14:01:04 +04:00
using var context = new AircraftPlantDatabase();
return context.Orders
2024-06-21 14:01:04 +04:00
.Where(x => x.Id.Equals(model.Id))
.Select(x => GetViewModel(x))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
2024-06-21 14:01:04 +04:00
using var context = new AircraftPlantDatabase();
2024-06-21 14:01:04 +04:00
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;
}
2024-06-21 14:01:04 +04:00
using var context = new AircraftPlantDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
2024-06-21 14:01:04 +04:00
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;
}
2024-06-21 14:01:04 +04:00
order.Update(model);
context.SaveChanges();
2024-06-21 14:01:04 +04:00
return GetViewModel(order);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new AircraftPlantDatabase();
2024-06-21 14:01:04 +04:00
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
2024-06-21 14:01:04 +04:00
return GetViewModel(element);
}
return null;
}
2024-06-21 14:01:04 +04:00
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;
}
}
2024-03-26 19:43:48 +04:00
}