| | | 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>Listar acreedores con paginación y filtros</summary> |
| | | 31 | | /// <remarks>Roles: Administrador, Supervisor, Usuario</remarks> |
| | | 32 | | [HttpGet] |
| | | 33 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 34 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 35 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 36 | | public async Task<IActionResult> GetAcreedores( |
| | | 37 | | [FromQuery] int page = 1, |
| | | 38 | | [FromQuery] int pageSize = 20, |
| | | 39 | | [FromQuery] string? q = null, |
| | | 40 | | [FromQuery] bool? activo = null, |
| | | 41 | | [FromQuery] string? categoria = null) |
| | | 42 | | { |
| | | 43 | | try |
| | | 44 | | { |
| | 3 | 45 | | var resultado = await _acreedorService.GetPagedAsync(page, pageSize, q, activo, categoria); |
| | | 46 | | |
| | 3 | 47 | | var response = new |
| | 3 | 48 | | { |
| | 3 | 49 | | items = resultado.Items, |
| | 3 | 50 | | page = resultado.Page, |
| | 3 | 51 | | pageSize = resultado.PageSize, |
| | 3 | 52 | | totalCount = resultado.TotalCount, |
| | 3 | 53 | | totalPages = resultado.TotalPages, |
| | 3 | 54 | | hasPreviousPage = resultado.HasPreviousPage, |
| | 3 | 55 | | hasNextPage = resultado.HasNextPage, |
| | 3 | 56 | | }; |
| | | 57 | | |
| | 3 | 58 | | return Ok(ApiResponse<object>.SuccessResult(response)); |
| | | 59 | | } |
| | 0 | 60 | | catch (Exception ex) |
| | | 61 | | { |
| | 0 | 62 | | _logger.LogError(ex, "Error al obtener acreedores"); |
| | 0 | 63 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 64 | | } |
| | 3 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary>Obtener detalle de un acreedor por ID</summary> |
| | | 68 | | /// <remarks>Roles: Administrador, Supervisor, Usuario</remarks> |
| | | 69 | | [HttpGet("{id}")] |
| | | 70 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 71 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 72 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 73 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 74 | | public async Task<IActionResult> GetAcreedor(long id) |
| | | 75 | | { |
| | | 76 | | try |
| | | 77 | | { |
| | 2 | 78 | | var acreedor = await _acreedorService.GetByIdAsync(id); |
| | | 79 | | |
| | 2 | 80 | | if (acreedor == null) |
| | 1 | 81 | | return NotFound(ApiResponse<string>.ErrorResult("Acreedor no encontrado")); |
| | | 82 | | |
| | 1 | 83 | | var response = new |
| | 1 | 84 | | { |
| | 1 | 85 | | id = acreedor.Id, |
| | 1 | 86 | | codigo = acreedor.Codigo, |
| | 1 | 87 | | nombre = acreedor.Nombre, |
| | 1 | 88 | | categoriaDeficit = acreedor.CategoriaDeficit, |
| | 1 | 89 | | ordenLegalDeficit = acreedor.OrdenLegalDeficit, |
| | 1 | 90 | | formatoArchivo = acreedor.FormatoArchivo, |
| | 1 | 91 | | contacto = acreedor.Contacto, |
| | 1 | 92 | | activo = acreedor.Activo, |
| | 1 | 93 | | }; |
| | | 94 | | |
| | 1 | 95 | | return Ok(ApiResponse<object>.SuccessResult(response)); |
| | | 96 | | } |
| | 0 | 97 | | catch (Exception ex) |
| | | 98 | | { |
| | 0 | 99 | | _logger.LogError(ex, "Error al obtener acreedor {Id}", id); |
| | 0 | 100 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 101 | | } |
| | 2 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary>Crear un nuevo acreedor</summary> |
| | | 105 | | /// <remarks>Roles: Administrador</remarks> |
| | | 106 | | [HttpPost] |
| | | 107 | | [Authorize(Roles = "Administrador")] |
| | | 108 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status201Created)] |
| | | 109 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 110 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 111 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 112 | | public async Task<IActionResult> CreateAcreedor([FromBody] CreateAcreedorRequest request) |
| | | 113 | | { |
| | | 114 | | try |
| | | 115 | | { |
| | 2 | 116 | | var (success, acreedor, error) = await _acreedorService.CreateAsync(request); |
| | | 117 | | |
| | 2 | 118 | | if (!success) |
| | | 119 | | { |
| | 1 | 120 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 121 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 122 | | |
| | 0 | 123 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear acreedor")); |
| | | 124 | | } |
| | | 125 | | |
| | 1 | 126 | | var response = new { id = acreedor!.Id, creado = true }; |
| | | 127 | | |
| | 1 | 128 | | return CreatedAtAction( |
| | 1 | 129 | | nameof(GetAcreedor), |
| | 1 | 130 | | new { id = acreedor.Id }, |
| | 1 | 131 | | ApiResponse<object>.SuccessResult(response, "Acreedor creado exitosamente")); |
| | | 132 | | } |
| | 0 | 133 | | catch (Exception ex) |
| | | 134 | | { |
| | 0 | 135 | | _logger.LogError(ex, "Error al crear acreedor"); |
| | 0 | 136 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 137 | | } |
| | 2 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary>Editar un acreedor existente (incluye activar/desactivar)</summary> |
| | | 141 | | /// <remarks>Roles: Administrador</remarks> |
| | | 142 | | [HttpPut("{id}")] |
| | | 143 | | [Authorize(Roles = "Administrador")] |
| | | 144 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 145 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 146 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 147 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 148 | | public async Task<IActionResult> UpdateAcreedor(long id, [FromBody] UpdateAcreedorRequest request) |
| | | 149 | | { |
| | | 150 | | try |
| | | 151 | | { |
| | 2 | 152 | | var (success, _, error) = await _acreedorService.UpdateAsync(id, request); |
| | | 153 | | |
| | 2 | 154 | | if (!success) |
| | | 155 | | { |
| | 1 | 156 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 157 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 158 | | |
| | 0 | 159 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar acreedor")); |
| | | 160 | | } |
| | | 161 | | |
| | 1 | 162 | | return Ok(ApiResponse<object>.SuccessResult(new { actualizado = true }, "Acreedor actualizado exitosamente") |
| | | 163 | | } |
| | 0 | 164 | | catch (Exception ex) |
| | | 165 | | { |
| | 0 | 166 | | _logger.LogError(ex, "Error al actualizar acreedor {Id}", id); |
| | 0 | 167 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 168 | | } |
| | 2 | 169 | | } |
| | | 170 | | } |