103 lines
3.1 KiB
C#
103 lines
3.1 KiB
C#
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;
|
||
private readonly IClientLogic client;
|
||
public ClientController(ILogger<ClientController> logger, IClientLogic visit)
|
||
{
|
||
_logger = logger;
|
||
client = visit;
|
||
}
|
||
|
||
[HttpGet]
|
||
public Tuple<ClientViewModel, List<Tuple<string, int>>>? GetClient(string ClientSnils)
|
||
{
|
||
try
|
||
{
|
||
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)
|
||
{
|
||
_logger.LogError(ex, "Ошибка получения клиента по id={Id}", ClientSnils);
|
||
throw;
|
||
}
|
||
}
|
||
[HttpGet]
|
||
public List<ClientViewModel> GetClients(int? workerId = null)
|
||
{
|
||
try
|
||
{
|
||
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)
|
||
{
|
||
_logger.LogError(ex, "Ошибка получения списка клиентов");
|
||
throw;
|
||
}
|
||
}
|
||
[HttpPost]
|
||
public bool CreateClient(ClientBindingModel model)
|
||
{
|
||
try
|
||
{
|
||
return client.Create(model);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Не удалось создать клиента");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
public bool UpdateClient(bool isConnection, ClientBindingModel model)
|
||
{
|
||
try
|
||
{
|
||
return client.Update(model);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Не удалось обновить клиента");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
public bool DeleteClient(ClientBindingModel model)
|
||
{
|
||
try
|
||
{
|
||
return client.Delete(model);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка удаления клиента");
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
} |