PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantRestApi/Controllers/MainController.cs

119 lines
3.5 KiB
C#
Raw Permalink Normal View History

2024-04-07 00:02:29 +04:00
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.BusinessLogicsContracts;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace AircraftPlantRestApi.Controllers
{
/// <summary>
/// Контроллер для работы с заказами
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Бизнес-логика для заказов
/// </summary>
private readonly IOrderLogic _orderLogic;
/// <summary>
/// Бизнес-логика для изделий
/// </summary>
private readonly IPlaneLogic _planeLogic;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="orderLogic"></param>
/// <param name="planeLogic"></param>
public MainController(ILogger<MainController> logger, IOrderLogic orderLogic, IPlaneLogic planeLogic)
{
_logger = logger;
_orderLogic = orderLogic;
_planeLogic = planeLogic;
}
/// <summary>
/// Получение списка изделий
/// </summary>
/// <returns></returns>
[HttpGet]
public List<PlaneViewModel>? GetPlaneList()
{
try
{
return _planeLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка изделий");
throw;
}
}
/// <summary>
/// Получение изделия
/// </summary>
/// <param name="planeId"></param>
/// <returns></returns>
[HttpGet]
public PlaneViewModel? GetPlane(int planeId)
{
try
{
return _planeLogic.ReadElement(new PlaneSearchModel { Id = planeId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения изделия с Id: {Id}", planeId);
throw;
}
}
/// <summary>
/// Получение списка заказов клиента
/// </summary>
/// <param name="clientId"></param>
/// <returns></returns>
[HttpGet]
public List<OrderViewModel>? GetOrders(int clientId)
{
try
{
return _orderLogic.ReadList(new OrderSearchModel { ClientId = clientId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов клиента с Id: {Id}", clientId);
throw;
}
}
/// <summary>
/// Создание заказа
/// </summary>
/// <param name="model"></param>
[HttpPost]
public void CreateOrder(OrderBindingModel model)
{
try
{
_orderLogic.CreateOrder(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
}
}