SUBD_PIbd-23_ZakharovRA/CarShowroom/CarShowroomRestApi/Controllers/CarController.cs

99 lines
2.4 KiB
C#

using CarShowroomContracts.BusinessLogic;
using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views;
using Microsoft.AspNetCore.Mvc;
namespace CarShowroomRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class CarController : Controller
{
private readonly ILogger _logger;
private readonly ICarLogic _logic;
public CarController(ILogger<CarController> logger, ICarLogic car)
{
_logger = logger;
_logic = car;
}
[HttpGet]
public List<CarView>? GetCarList()
{
try
{
return _logic.ReadList(new CarSearch
{
IsSaled = false,
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка машин");
throw;
}
}
[HttpGet]
public CarView? GetCar(int CarId)
{
try
{
return _logic.ReadElement(new CarSearch
{
Id = CarId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения машины по Id={Id}", CarId);
throw;
}
}
[HttpPost]
public void CreateCar(CarDto model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания машины");
throw;
}
}
[HttpPut]
public void UpdateCar(CarDto model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления машины");
throw;
}
}
[HttpDelete]
public void DeleteCar(CarDto model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления машины");
throw;
}
}
}
}