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

103 lines
3.1 KiB
C#
Raw Normal View History

2024-05-27 07:56:21 +04:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
namespace BankRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ClientController : Controller
{
private readonly ILogger _logger;
2024-05-27 07:56:21 +04:00
private readonly IClientLogic client;
public ClientController(ILogger<ClientController> logger, IClientLogic visit)
{
_logger = logger;
2024-05-27 07:56:21 +04:00
client = visit;
}
2024-05-27 07:56:21 +04:00
[HttpGet]
2024-05-27 07:56:21 +04:00
public Tuple<ClientViewModel, List<Tuple<string, int>>>? GetClient(string ClientSnils)
{
try
{
2024-05-27 07:56:21 +04:00
var elem = client.ReadElement(new ClientSearchModel { Snils = ClientSnils });
if (elem == null)
return null;
var res = Tuple.Create(elem, elem.ClientPrograms.Select(x => Tuple.Create(x.Value.ProgramName, x.Value.Id)).ToList());
res.Item1.ClientPrograms = null;
return res;
}
catch (Exception ex)
{
2024-05-27 07:56:21 +04:00
_logger.LogError(ex, "Ошибка получения клиента по id={Id}", ClientSnils);
throw;
}
}
[HttpGet]
2024-05-27 07:56:21 +04:00
public List<ClientViewModel> GetClients(int? workerId = null)
{
try
{
2024-05-27 07:56:21 +04:00
List<ClientViewModel> res;
if (!workerId.HasValue)
res = client.ReadList(null);
else
res = client.ReadList(new ClientSearchModel { WorkerId = workerId });
foreach (var client in res)
client.ClientPrograms = null!;
return res;
}
catch (Exception ex)
{
2024-05-27 07:56:21 +04:00
_logger.LogError(ex, "Ошибка получения списка клиентов");
throw;
}
}
[HttpPost]
2024-05-27 07:56:21 +04:00
public bool CreateClient(ClientBindingModel model)
{
try
{
2024-05-27 07:56:21 +04:00
return client.Create(model);
}
catch (Exception ex)
{
2024-05-27 07:56:21 +04:00
_logger.LogError(ex, "Не удалось создать клиента");
throw;
}
}
2024-05-27 07:56:21 +04:00
[HttpPost]
public bool UpdateClient(bool isConnection, ClientBindingModel model)
{
try
{
2024-05-27 07:56:21 +04:00
return client.Update(model);
}
catch (Exception ex)
{
2024-05-27 07:56:21 +04:00
_logger.LogError(ex, "Не удалось обновить клиента");
throw;
}
}
2024-05-27 07:56:21 +04:00
[HttpPost]
public bool DeleteClient(ClientBindingModel model)
{
try
{
2024-05-27 07:56:21 +04:00
return client.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления клиента");
throw;
}
}
}
}