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