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