2024-05-27 07:56:21 +04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using BankContracts.BindingModels;
|
2024-05-02 00:46:24 +04:00
|
|
|
|
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)
|
2024-05-02 00:46:24 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2024-05-27 07:56:21 +04:00
|
|
|
|
client = visit;
|
2024-05-02 00:46:24 +04:00
|
|
|
|
}
|
2024-05-27 07:56:21 +04:00
|
|
|
|
|
2024-05-02 00:46:24 +04:00
|
|
|
|
[HttpGet]
|
2024-05-27 07:56:21 +04:00
|
|
|
|
public Tuple<ClientViewModel, List<Tuple<string, int>>>? GetClient(string ClientSnils)
|
2024-05-02 00:46:24 +04:00
|
|
|
|
{
|
|
|
|
|
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;
|
2024-05-02 00:46:24 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-05-27 07:56:21 +04:00
|
|
|
|
_logger.LogError(ex, "Ошибка получения клиента по id={Id}", ClientSnils);
|
2024-05-02 00:46:24 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[HttpGet]
|
2024-05-27 07:56:21 +04:00
|
|
|
|
public List<ClientViewModel> GetClients(int? workerId = null)
|
2024-05-02 00:46:24 +04:00
|
|
|
|
{
|
|
|
|
|
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;
|
2024-05-02 00:46:24 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-05-27 07:56:21 +04:00
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка клиентов");
|
2024-05-02 00:46:24 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[HttpPost]
|
2024-05-27 07:56:21 +04:00
|
|
|
|
public bool CreateClient(ClientBindingModel model)
|
2024-05-02 00:46:24 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-27 07:56:21 +04:00
|
|
|
|
return client.Create(model);
|
2024-05-02 00:46:24 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-05-27 07:56:21 +04:00
|
|
|
|
_logger.LogError(ex, "Не удалось создать клиента");
|
2024-05-02 00:46:24 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-27 07:56:21 +04:00
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public bool UpdateClient(bool isConnection, ClientBindingModel model)
|
2024-05-02 00:46:24 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-27 07:56:21 +04:00
|
|
|
|
return client.Update(model);
|
2024-05-02 00:46:24 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-05-27 07:56:21 +04:00
|
|
|
|
_logger.LogError(ex, "Не удалось обновить клиента");
|
2024-05-02 00:46:24 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-27 07:56:21 +04:00
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public bool DeleteClient(ClientBindingModel model)
|
2024-05-02 00:46:24 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-27 07:56:21 +04:00
|
|
|
|
return client.Delete(model);
|
2024-05-02 00:46:24 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка удаления клиента");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|