| | | 1 | | using FAU.Entidades; |
| | | 2 | | using FAU.Logica.DTOs; |
| | | 3 | | using FAU.Logica.Services; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | |
| | | 7 | | namespace FAU.API.Controllers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Gestión del catálogo de acreedores |
| | | 11 | | /// </summary> |
| | | 12 | | [ApiController] |
| | | 13 | | [Route("api/acreedores")] |
| | | 14 | | [Authorize] |
| | | 15 | | public class AcreedoresController : ControllerBase |
| | | 16 | | { |
| | | 17 | | private readonly IAcreedorService _acreedorService; |
| | | 18 | | private readonly ILogger<AcreedoresController> _logger; |
| | | 19 | | |
| | 9 | 20 | | public AcreedoresController( |
| | 9 | 21 | | IAcreedorService acreedorService, |
| | 9 | 22 | | ILogger<AcreedoresController> logger) |
| | | 23 | | { |
| | 9 | 24 | | _acreedorService = acreedorService; |
| | 9 | 25 | | _logger = logger; |
| | 9 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Listar acreedores con paginación y filtros |
| | | 30 | | /// GET /api/acreedores?page=1&pageSize=20&q=&activo=&categoria= |
| | | 31 | | /// </summary> |
| | | 32 | | [HttpGet] |
| | | 33 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerAcreedores)] |
| | | 34 | | public async Task<IActionResult> GetAcreedores( |
| | | 35 | | [FromQuery] int page = 1, |
| | | 36 | | [FromQuery] int pageSize = 20, |
| | | 37 | | [FromQuery] string? q = null, |
| | | 38 | | [FromQuery] bool? activo = null, |
| | | 39 | | [FromQuery] string? categoria = null) |
| | | 40 | | { |
| | | 41 | | try |
| | | 42 | | { |
| | 3 | 43 | | var resultado = await _acreedorService.GetPagedAsync(page, pageSize, q, activo, categoria); |
| | | 44 | | |
| | 3 | 45 | | var response = new |
| | 3 | 46 | | { |
| | 3 | 47 | | items = resultado.Items, |
| | 3 | 48 | | page = resultado.Page, |
| | 3 | 49 | | pageSize = resultado.PageSize, |
| | 3 | 50 | | totalCount = resultado.TotalCount, |
| | 3 | 51 | | totalPages = resultado.TotalPages, |
| | 3 | 52 | | hasPreviousPage = resultado.HasPreviousPage, |
| | 3 | 53 | | hasNextPage = resultado.HasNextPage, |
| | 3 | 54 | | }; |
| | | 55 | | |
| | 3 | 56 | | return Ok(ApiResponse<object>.SuccessResult(response)); |
| | | 57 | | } |
| | 0 | 58 | | catch (Exception ex) |
| | | 59 | | { |
| | 0 | 60 | | _logger.LogError(ex, "Error al obtener acreedores"); |
| | 0 | 61 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 62 | | } |
| | 3 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Obtener detalle de un acreedor por ID |
| | | 67 | | /// GET /api/acreedores/{id} |
| | | 68 | | /// </summary> |
| | | 69 | | [HttpGet("{id}")] |
| | | 70 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerAcreedores)] |
| | | 71 | | public async Task<IActionResult> GetAcreedor(long id) |
| | | 72 | | { |
| | | 73 | | try |
| | | 74 | | { |
| | 2 | 75 | | var acreedor = await _acreedorService.GetByIdAsync(id); |
| | | 76 | | |
| | 2 | 77 | | if (acreedor == null) |
| | 1 | 78 | | return NotFound(ApiResponse<string>.ErrorResult("Acreedor no encontrado")); |
| | | 79 | | |
| | 1 | 80 | | var response = new |
| | 1 | 81 | | { |
| | 1 | 82 | | id = acreedor.Id, |
| | 1 | 83 | | codigo = acreedor.Codigo, |
| | 1 | 84 | | nombre = acreedor.Nombre, |
| | 1 | 85 | | categoriaDeficit = acreedor.CategoriaDeficit, |
| | 1 | 86 | | ordenLegalDeficit = acreedor.OrdenLegalDeficit, |
| | 1 | 87 | | formatoArchivo = acreedor.FormatoArchivo, |
| | 1 | 88 | | contacto = acreedor.Contacto, |
| | 1 | 89 | | activo = acreedor.Activo, |
| | 1 | 90 | | }; |
| | | 91 | | |
| | 1 | 92 | | return Ok(ApiResponse<object>.SuccessResult(response)); |
| | | 93 | | } |
| | 0 | 94 | | catch (Exception ex) |
| | | 95 | | { |
| | 0 | 96 | | _logger.LogError(ex, "Error al obtener acreedor {Id}", id); |
| | 0 | 97 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 98 | | } |
| | 2 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Crear un nuevo acreedor |
| | | 103 | | /// POST /api/acreedores |
| | | 104 | | /// </summary> |
| | | 105 | | [HttpPost] |
| | | 106 | | [FAU.API.Security.RequirePermission(PermisoEnum.CrearAcreedor)] |
| | | 107 | | public async Task<IActionResult> CreateAcreedor([FromBody] CreateAcreedorRequest request) |
| | | 108 | | { |
| | | 109 | | try |
| | | 110 | | { |
| | 2 | 111 | | var (success, acreedor, error) = await _acreedorService.CreateAsync(request); |
| | | 112 | | |
| | 2 | 113 | | if (!success) |
| | | 114 | | { |
| | 1 | 115 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 116 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 117 | | |
| | 0 | 118 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear acreedor")); |
| | | 119 | | } |
| | | 120 | | |
| | 1 | 121 | | var response = new { id = acreedor!.Id, creado = true }; |
| | | 122 | | |
| | 1 | 123 | | return CreatedAtAction( |
| | 1 | 124 | | nameof(GetAcreedor), |
| | 1 | 125 | | new { id = acreedor.Id }, |
| | 1 | 126 | | ApiResponse<object>.SuccessResult(response, "Acreedor creado exitosamente")); |
| | | 127 | | } |
| | 0 | 128 | | catch (Exception ex) |
| | | 129 | | { |
| | 0 | 130 | | _logger.LogError(ex, "Error al crear acreedor"); |
| | 0 | 131 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 132 | | } |
| | 2 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Editar un acreedor existente (incluye activar/desactivar) |
| | | 137 | | /// PUT /api/acreedores/{id} |
| | | 138 | | /// </summary> |
| | | 139 | | [HttpPut("{id}")] |
| | | 140 | | [FAU.API.Security.RequirePermission(PermisoEnum.ActualizarAcreedor)] |
| | | 141 | | public async Task<IActionResult> UpdateAcreedor(long id, [FromBody] UpdateAcreedorRequest request) |
| | | 142 | | { |
| | | 143 | | try |
| | | 144 | | { |
| | 2 | 145 | | var (success, _, error) = await _acreedorService.UpdateAsync(id, request); |
| | | 146 | | |
| | 2 | 147 | | if (!success) |
| | | 148 | | { |
| | 1 | 149 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 150 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 151 | | |
| | 0 | 152 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar acreedor")); |
| | | 153 | | } |
| | | 154 | | |
| | 1 | 155 | | return Ok(ApiResponse<object>.SuccessResult(new { actualizado = true }, "Acreedor actualizado exitosamente") |
| | | 156 | | } |
| | 0 | 157 | | catch (Exception ex) |
| | | 158 | | { |
| | 0 | 159 | | _logger.LogError(ex, "Error al actualizar acreedor {Id}", id); |
| | 0 | 160 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 161 | | } |
| | 2 | 162 | | } |
| | | 163 | | } |