PIbd-23_Elatomtsev_L.K._Cou.../Bank/BankRestApi/Controllers/ClientController.cs

93 lines
2.5 KiB
C#

using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
namespace BankRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ClientController : Controller
{
private readonly ILogger _logger;
private readonly IClientLogic _logic;
public ClientController(ILogger<ClientController> logger,
IClientLogic logic)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public List<ClientViewModel>? GetClientList()
{
try
{
return _logic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка клиентов");
throw;
}
}
[HttpGet]
public ClientViewModel? GetClient(string ClientSnils)
{
try
{
return _logic.ReadElement(new ClientSearchModel
{
Snils = ClientSnils
});
}
catch (Exception ex)
{
_logger.LogError(
ex, "Ошибка получения клиента по Snils={Snils}",
ClientSnils);
throw;
}
}
[HttpPost]
public void CreateClient(ClientBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания клиента");
throw;
}
}
[HttpPut]
public void UpdateClient(ClientBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления клиента");
throw;
}
}
[HttpDelete]
public void DeleteClient(ClientBindingModel model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления клиента");
throw;
}
}
}
}