| | | 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 | | [ApiController] |
| | | 10 | | [Route("api/dependientes")] |
| | | 11 | | public class DependienteController : ControllerBase |
| | | 12 | | { |
| | | 13 | | private readonly IDependienteService _dependienteService; |
| | | 14 | | private readonly ILogger<DependienteController> _logger; |
| | | 15 | | |
| | 18 | 16 | | public DependienteController(IDependienteService dependienteService, ILogger<DependienteController> logger) |
| | | 17 | | { |
| | 18 | 18 | | _dependienteService = dependienteService; |
| | 18 | 19 | | _logger = logger; |
| | 18 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Listar dependientes con filtros y paginación |
| | | 24 | | /// GET /api/dependientes?cedulaTitular=&tipo=&activo=&page=&pageSize= |
| | | 25 | | /// </summary> |
| | | 26 | | [HttpGet] |
| | | 27 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerDependientes)] |
| | | 28 | | public async Task<IActionResult> GetDependientes( |
| | | 29 | | [FromQuery] string? cedulaTitular = null, |
| | | 30 | | [FromQuery] string? tipo = null, |
| | | 31 | | [FromQuery] bool? activo = null, |
| | | 32 | | [FromQuery] int page = 1, |
| | | 33 | | [FromQuery] int pageSize = 20) |
| | | 34 | | { |
| | | 35 | | try |
| | | 36 | | { |
| | 2 | 37 | | if (page < 1) page = 1; |
| | 2 | 38 | | if (pageSize < 1 || pageSize > 100) pageSize = 20; |
| | | 39 | | |
| | 2 | 40 | | var resultado = await _dependienteService.GetDependientesPagedAsync(page, pageSize, cedulaTitular, tipo, act |
| | 1 | 41 | | return Ok(ApiResponse<object>.SuccessResult(resultado)); |
| | | 42 | | } |
| | 1 | 43 | | catch (Exception ex) |
| | | 44 | | { |
| | 1 | 45 | | _logger.LogError(ex, "Error al obtener dependientes"); |
| | 1 | 46 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 47 | | } |
| | 2 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Listar tipos de dependiente disponibles |
| | | 52 | | /// GET /api/dependientes/tipos |
| | | 53 | | /// </summary> |
| | | 54 | | [HttpGet("tipos")] |
| | | 55 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerDependientes)] |
| | | 56 | | public IActionResult GetTipos() |
| | | 57 | | { |
| | 1 | 58 | | var tipos = new[] |
| | 1 | 59 | | { |
| | 1 | 60 | | new { valor = "hijo", etiqueta = "Hijo" }, |
| | 1 | 61 | | new { valor = "conyuge_sin_ingresos", etiqueta = "Cónyuge sin ingresos" } |
| | 1 | 62 | | }; |
| | 1 | 63 | | return Ok(ApiResponse<object>.SuccessResult(tipos)); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Obtener dependiente por ID |
| | | 68 | | /// GET /api/dependientes/{id} |
| | | 69 | | /// </summary> |
| | | 70 | | [HttpGet("{id}")] |
| | | 71 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerDependientes)] |
| | | 72 | | public async Task<IActionResult> GetDependiente(long id) |
| | | 73 | | { |
| | | 74 | | try |
| | | 75 | | { |
| | 3 | 76 | | var dependiente = await _dependienteService.GetDependienteByIdAsync(id); |
| | 2 | 77 | | if (dependiente == null) |
| | 1 | 78 | | return NotFound(ApiResponse<string>.ErrorResult("Dependiente no encontrado")); |
| | | 79 | | |
| | 1 | 80 | | return Ok(ApiResponse<object>.SuccessResult(dependiente)); |
| | | 81 | | } |
| | 1 | 82 | | catch (Exception ex) |
| | | 83 | | { |
| | 1 | 84 | | _logger.LogError(ex, "Error al obtener dependiente {Id}", id); |
| | 1 | 85 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 86 | | } |
| | 3 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Listar dependientes de una persona por cédula |
| | | 91 | | /// GET /api/dependientes/persona/{cedula} |
| | | 92 | | /// </summary> |
| | | 93 | | [HttpGet("persona/{cedula}")] |
| | | 94 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerDependientes)] |
| | | 95 | | public async Task<IActionResult> GetDependientesByPersona(string cedula) |
| | | 96 | | { |
| | | 97 | | try |
| | | 98 | | { |
| | 2 | 99 | | var dependientes = await _dependienteService.GetDependientesByPersonaAsync(cedula); |
| | 1 | 100 | | return Ok(ApiResponse<object>.SuccessResult(dependientes)); |
| | | 101 | | } |
| | 1 | 102 | | catch (Exception ex) |
| | | 103 | | { |
| | 1 | 104 | | _logger.LogError(ex, "Error al obtener dependientes de persona {Cedula}", cedula); |
| | 1 | 105 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 106 | | } |
| | 2 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Crear dependiente |
| | | 111 | | /// POST /api/dependientes |
| | | 112 | | /// </summary> |
| | | 113 | | [HttpPost] |
| | | 114 | | [FAU.API.Security.RequirePermission(PermisoEnum.CrearDependientes)] |
| | | 115 | | public async Task<IActionResult> CreateDependiente([FromBody] CreateDependienteRequest request) |
| | | 116 | | { |
| | | 117 | | try |
| | | 118 | | { |
| | 5 | 119 | | var (success, dependiente, error) = await _dependienteService.CreateDependienteAsync(request); |
| | 4 | 120 | | if (!success) |
| | | 121 | | { |
| | 3 | 122 | | if (error?.Contains("Ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 123 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 2 | 124 | | if (error?.Contains("No se encontró", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 125 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 126 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear dependiente")); |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | return CreatedAtAction( |
| | 1 | 130 | | nameof(GetDependiente), |
| | 1 | 131 | | new { id = dependiente!.Id }, |
| | 1 | 132 | | ApiResponse<object>.SuccessResult(new { id = dependiente.Id, creado = true }, "Dependiente creado exitos |
| | | 133 | | } |
| | 1 | 134 | | catch (Exception ex) |
| | | 135 | | { |
| | 1 | 136 | | _logger.LogError(ex, "Error al crear dependiente"); |
| | 1 | 137 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 138 | | } |
| | 5 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Actualizar dependiente |
| | | 143 | | /// PUT /api/dependientes/{id} |
| | | 144 | | /// </summary> |
| | | 145 | | [HttpPut("{id}")] |
| | | 146 | | [FAU.API.Security.RequirePermission(PermisoEnum.ActualizarDependientes)] |
| | | 147 | | public async Task<IActionResult> UpdateDependiente(long id, [FromBody] UpdateDependienteRequest request) |
| | | 148 | | { |
| | | 149 | | try |
| | | 150 | | { |
| | 5 | 151 | | var (success, _, error) = await _dependienteService.UpdateDependienteAsync(id, request); |
| | 4 | 152 | | if (!success) |
| | | 153 | | { |
| | 3 | 154 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 155 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | 2 | 156 | | if (error?.Contains("Ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | 1 | 157 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | 1 | 158 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar dependiente")); |
| | | 159 | | } |
| | | 160 | | |
| | 1 | 161 | | return Ok(ApiResponse<object>.SuccessResult(new { actualizado = true }, "Dependiente actualizado exitosament |
| | | 162 | | } |
| | 1 | 163 | | catch (Exception ex) |
| | | 164 | | { |
| | 1 | 165 | | _logger.LogError(ex, "Error al actualizar dependiente {Id}", id); |
| | 1 | 166 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 167 | | } |
| | 5 | 168 | | } |
| | | 169 | | } |