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

103 lines
3.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}
}