| | | 1 | | using FAU.Logica.DTOs; |
| | | 2 | | using FAU.Logica.Services; |
| | | 3 | | using FAU.Entidades; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | |
| | | 7 | | namespace FAU.API.Controllers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Gestión de bancos del sistema |
| | | 11 | | /// </summary> |
| | | 12 | | [ApiController] |
| | | 13 | | [Route("api/[controller]")] |
| | | 14 | | public class BancosController : ControllerBase |
| | | 15 | | { |
| | | 16 | | private readonly IBancoService _bancoService; |
| | | 17 | | private readonly ILogger<BancosController> _logger; |
| | | 18 | | |
| | 19 | 19 | | public BancosController( |
| | 19 | 20 | | IBancoService bancoService, |
| | 19 | 21 | | ILogger<BancosController> logger) |
| | | 22 | | { |
| | 19 | 23 | | _bancoService = bancoService; |
| | 19 | 24 | | _logger = logger; |
| | 19 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Listar bancos con paginación y filtros |
| | | 29 | | /// GET /api/bancos?page=1&pageSize=10&q=&vigente= |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="page">Número de página (default: 1)</param> |
| | | 32 | | /// <param name="pageSize">Tamaño de página (default: 10, max: 100)</param> |
| | | 33 | | /// <param name="q">Búsqueda por nombre o código</param> |
| | | 34 | | /// <param name="vigente">Filtrar por estado vigente</param> |
| | | 35 | | [HttpGet] |
| | | 36 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerBancos)] |
| | | 37 | | public async Task<IActionResult> GetBancos( |
| | | 38 | | [FromQuery] int page = 1, |
| | | 39 | | [FromQuery] int pageSize = 10, |
| | | 40 | | [FromQuery] string? q = null, |
| | | 41 | | [FromQuery] bool? vigente = null) |
| | | 42 | | { |
| | | 43 | | try |
| | | 44 | | { |
| | 3 | 45 | | var resultado = await _bancoService.GetPagedAsync(page, pageSize, q, vigente); |
| | | 46 | | |
| | 2 | 47 | | var response = new |
| | 2 | 48 | | { |
| | 0 | 49 | | items = resultado.Items.Select(b => new |
| | 0 | 50 | | { |
| | 0 | 51 | | id = b.Id, |
| | 0 | 52 | | nombre = b.Nombre, |
| | 0 | 53 | | codigo = b.Codigo, |
| | 0 | 54 | | vigente = b.Vigente |
| | 0 | 55 | | }), |
| | 2 | 56 | | page = resultado.Page, |
| | 2 | 57 | | pageSize = resultado.PageSize, |
| | 2 | 58 | | totalCount = resultado.TotalCount, |
| | 2 | 59 | | totalPages = resultado.TotalPages, |
| | 2 | 60 | | hasPreviousPage = resultado.HasPreviousPage, |
| | 2 | 61 | | hasNextPage = resultado.HasNextPage |
| | 2 | 62 | | }; |
| | | 63 | | |
| | 2 | 64 | | return Ok(ApiResponse<object>.SuccessResult(response)); |
| | | 65 | | } |
| | 1 | 66 | | catch (Exception ex) |
| | | 67 | | { |
| | 1 | 68 | | _logger.LogError(ex, "Error al obtener bancos"); |
| | 1 | 69 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 70 | | } |
| | 3 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Obtener detalle de un banco por ID |
| | | 75 | | /// GET /api/bancos/{id} |
| | | 76 | | /// </summary> |
| | | 77 | | [HttpGet("{id}")] |
| | | 78 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerBancos)] |
| | | 79 | | public async Task<IActionResult> GetBanco(long id) |
| | | 80 | | { |
| | | 81 | | try |
| | | 82 | | { |
| | 3 | 83 | | var banco = await _bancoService.GetByIdAsync(id); |
| | | 84 | | |
| | 2 | 85 | | if (banco == null) |
| | | 86 | | { |
| | 1 | 87 | | return NotFound(ApiResponse<string>.ErrorResult("Banco no encontrado")); |
| | | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | var response = new |
| | 1 | 91 | | { |
| | 1 | 92 | | id = banco.Id, |
| | 1 | 93 | | nombre = banco.Nombre, |
| | 1 | 94 | | codigo = banco.Codigo, |
| | 1 | 95 | | vigente = banco.Vigente |
| | 1 | 96 | | }; |
| | | 97 | | |
| | 1 | 98 | | return Ok(ApiResponse<object>.SuccessResult(response)); |
| | | 99 | | } |
| | 1 | 100 | | catch (Exception ex) |
| | | 101 | | { |
| | 1 | 102 | | _logger.LogError(ex, "Error al obtener banco {Id}", id); |
| | 1 | 103 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 104 | | } |
| | 3 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Crear un nuevo banco |
| | | 109 | | /// POST /api/bancos |
| | | 110 | | /// </summary> |
| | | 111 | | [HttpPost] |
| | | 112 | | [FAU.API.Security.RequirePermission(PermisoEnum.CrearBanco)] |
| | | 113 | | public async Task<IActionResult> CreateBanco([FromBody] CreateBancoRequest request) |
| | | 114 | | { |
| | | 115 | | try |
| | | 116 | | { |
| | | 117 | | // Validaciones |
| | 5 | 118 | | if (string.IsNullOrWhiteSpace(request.Nombre)) |
| | | 119 | | { |
| | 1 | 120 | | return BadRequest(ApiResponse<string>.ErrorResult("El nombre del banco es requerido")); |
| | | 121 | | } |
| | | 122 | | |
| | 4 | 123 | | if (string.IsNullOrWhiteSpace(request.Codigo)) |
| | | 124 | | { |
| | 1 | 125 | | return BadRequest(ApiResponse<string>.ErrorResult("El código del banco es requerido")); |
| | | 126 | | } |
| | | 127 | | |
| | 3 | 128 | | var (success, banco, error) = await _bancoService.CreateAsync( |
| | 3 | 129 | | request.Nombre, |
| | 3 | 130 | | request.Codigo, |
| | 3 | 131 | | request.Vigente); |
| | | 132 | | |
| | 2 | 133 | | if (!success) |
| | | 134 | | { |
| | 1 | 135 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | | 136 | | { |
| | 1 | 137 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 138 | | } |
| | 0 | 139 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear banco")); |
| | | 140 | | } |
| | | 141 | | |
| | 1 | 142 | | var response = new |
| | 1 | 143 | | { |
| | 1 | 144 | | id = banco!.Id, |
| | 1 | 145 | | creado = true |
| | 1 | 146 | | }; |
| | | 147 | | |
| | 1 | 148 | | return CreatedAtAction( |
| | 1 | 149 | | nameof(GetBanco), |
| | 1 | 150 | | new { id = banco.Id }, |
| | 1 | 151 | | ApiResponse<object>.SuccessResult(response, "Banco creado exitosamente")); |
| | | 152 | | } |
| | 1 | 153 | | catch (Exception ex) |
| | | 154 | | { |
| | 1 | 155 | | _logger.LogError(ex, "Error al crear banco"); |
| | 1 | 156 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 157 | | } |
| | 5 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Editar un banco existente (incluye activar/desactivar) |
| | | 162 | | /// PUT /api/bancos/{id} |
| | | 163 | | /// </summary> |
| | | 164 | | [HttpPut("{id}")] |
| | | 165 | | [FAU.API.Security.RequirePermission(PermisoEnum.ActualizarBanco)] |
| | | 166 | | public async Task<IActionResult> UpdateBanco(long id, [FromBody] UpdateBancoRequest request) |
| | | 167 | | { |
| | | 168 | | try |
| | | 169 | | { |
| | | 170 | | // Validar que al menos un campo se está actualizando |
| | 8 | 171 | | if (string.IsNullOrWhiteSpace(request.Nombre) && |
| | 8 | 172 | | string.IsNullOrWhiteSpace(request.Codigo) && |
| | 8 | 173 | | !request.Vigente.HasValue) |
| | | 174 | | { |
| | 1 | 175 | | return BadRequest(ApiResponse<string>.ErrorResult("Debe proporcionar al menos un campo para actualizar") |
| | | 176 | | } |
| | | 177 | | |
| | 7 | 178 | | var (success, banco, error) = await _bancoService.UpdateAsync( |
| | 7 | 179 | | id, |
| | 7 | 180 | | request.Nombre, |
| | 7 | 181 | | request.Codigo, |
| | 7 | 182 | | request.Vigente); |
| | | 183 | | |
| | 6 | 184 | | if (!success) |
| | | 185 | | { |
| | 2 | 186 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | | 187 | | { |
| | 1 | 188 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 189 | | } |
| | 1 | 190 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | | 191 | | { |
| | 1 | 192 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 193 | | } |
| | 0 | 194 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar banco")); |
| | | 195 | | } |
| | | 196 | | |
| | 4 | 197 | | var response = new |
| | 4 | 198 | | { |
| | 4 | 199 | | actualizado = true |
| | 4 | 200 | | }; |
| | | 201 | | |
| | 4 | 202 | | return Ok(ApiResponse<object>.SuccessResult(response, "Banco actualizado exitosamente")); |
| | | 203 | | } |
| | 1 | 204 | | catch (Exception ex) |
| | | 205 | | { |
| | 1 | 206 | | _logger.LogError(ex, "Error al actualizar banco {Id}", id); |
| | 1 | 207 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 208 | | } |
| | 8 | 209 | | } |
| | | 210 | | } |
| | | 211 | | |