2024-05-06 11:31:59 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
2024-05-13 15:26:31 +04:00
|
|
|
|
return _logic.ReadList(new CarSearch
|
|
|
|
|
{
|
|
|
|
|
IsSaled = false,
|
|
|
|
|
});
|
2024-05-06 11:31:59 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка машин");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-05-13 15:26:31 +04:00
|
|
|
|
}
|
2024-05-06 11:31:59 +04:00
|
|
|
|
|
2024-05-13 15:26:31 +04:00
|
|
|
|
[HttpGet]
|
2024-05-06 11:31:59 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|