PIbd22_NikiforovaMV_Automob.../AutomobilePlantRestApi/Controllers/MainController.cs
2024-06-22 22:23:42 +04:00

85 lines
2.4 KiB
C#

using DocumentFormat.OpenXml.Office2010.Excel;
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.BusinessLogicContracts;
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;
}
}
}
}