PIbd-23_Minhasapov_R.H._Aut.../AutomobilePlant/AutomobilePlantRestApi/Controllers/MainController.cs

85 lines
2.4 KiB
C#
Raw Normal View History

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;
}
}
}
}