SUBD_PIbd-23_ZakharovRA/CarShowroom/CarShowroomRestApi/Controllers/ModelController.cs
2024-05-06 11:31:59 +04:00

96 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 ModelController : Controller
{
private readonly ILogger _logger;
private readonly IModelLogic _logic;
public ModelController(ILogger<ModelController> logger, IModelLogic model)
{
_logger = logger;
_logic = model;
}
[HttpGet]
public List<ModelView>? GetModelList()
{
try
{
return _logic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка моделей");
throw;
}
}
[HttpGet]
public ModelView? GetModel(int ModelId)
{
try
{
return _logic.ReadElement(new ModelSearch
{
Id = ModelId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения модели по Id={Id}", ModelId);
throw;
}
}
[HttpPost]
public void CreateModel(ModelDto model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания модели");
throw;
}
}
[HttpPut]
public void UpdateModel(ModelDto model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления модели");
throw;
}
}
[HttpDelete]
public void DeleteModel(ModelDto model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления модели");
throw;
}
}
}
}