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

96 lines
2.3 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 ClientController : Controller
{
private readonly ILogger _logger;
private readonly IClientLogic _logic;
public ClientController(ILogger<ClientController> logger, IClientLogic client)
{
_logger = logger;
_logic = client;
}
[HttpGet]
public List<ClientView>? GetClientList()
{
try
{
return _logic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Îøèáêà ïîëó÷åíèÿ ñïèñêà êëèåíòîâ");
throw;
}
}
[HttpGet]
public ClientView? GetClient(int ClientId)
{
try
{
return _logic.ReadElement(new ClientSearch
{
Id = ClientId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Îøèáêà ïîëó÷åíèÿ êëèåíòà ïî Id={Id}", ClientId);
throw;
}
}
[HttpPost]
public void CreateClient(ClientDto model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Îøèáêà ñîçäàíèÿ êëèåíòà");
throw;
}
}
[HttpPut]
public void UpdateClient(ClientDto model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Îøèáêà îáíîâëåíèÿ êëèåíòà");
throw;
}
}
[HttpDelete]
public void DeleteClient(ClientDto model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Îøèáêà óäàëåíèÿ êëèåíòà");
throw;
}
}
}
}