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 logger, IClientLogic visit) { _logger = logger; client = visit; } [HttpGet] public Tuple>>? 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 GetClients(int? workerId = null) { try { List 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; } } } }