PIbd-21_Kouvshinoff_T._A._A.../AutomobilePlant/AutomobilePlantRestApi/Controllers/MainController.cs
2024-04-05 23:37:18 +04:00

83 lines
2.3 KiB
C#

using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.BusinessLogicsContracts;
using AutomobilePlantContracts.SearchModels;
using AutomobilePlantContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace AutomobilePlantRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly ICarLogic _car;
public MainController(ILogger<MainController> logger, IOrderLogic order, ICarLogic car)
{
_logger = logger;
_order = order;
_car = car;
}
[HttpGet]
public List<CarViewModel>? GetCarList()
{
try
{
return _car.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка авто");
throw;
}
}
[HttpGet]
public CarViewModel? GetCar(int carId)
{
try
{
return _car.ReadElement(new CarSearchModel
{
Id = carId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения авто по id={Id}", carId);
throw;
}
}
[HttpGet]
public List<OrderViewModel>? GetOrders(int clientId)
{
try
{
return _order.ReadList(new OrderSearchModel
{
ClientId = clientId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов клиента id ={ Id}", clientId);
throw;
}
}
[HttpPost]
public void CreateOrder(OrderBindingModel model)
{
try
{
_order.CreateOrder(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
}
}