| | | 1 | | using FAU.Logica.DTOs; |
| | | 2 | | using FAU.Logica.Services; |
| | | 3 | | using FAU.Entidades; |
| | | 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 de roles del sistema |
| | | 12 | | /// </summary> |
| | | 13 | | [ApiController] |
| | | 14 | | [Route("api/[controller]")] |
| | | 15 | | [Produces("application/json")] |
| | | 16 | | public class RolesController : ControllerBase |
| | | 17 | | { |
| | | 18 | | private readonly IRolService _rolService; |
| | | 19 | | private readonly ILogger<RolesController> _logger; |
| | | 20 | | |
| | 11 | 21 | | public RolesController( |
| | 11 | 22 | | IRolService rolService, |
| | 11 | 23 | | ILogger<RolesController> logger) |
| | | 24 | | { |
| | 11 | 25 | | _rolService = rolService; |
| | 11 | 26 | | _logger = logger; |
| | 11 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Listar todos los roles |
| | | 31 | | /// GET /api/roles |
| | | 32 | | /// </summary> |
| | | 33 | | [HttpGet] |
| | | 34 | | [Authorize(Roles = "Administrador")] |
| | | 35 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 36 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 37 | | public async Task<IActionResult> GetRoles() |
| | | 38 | | { |
| | | 39 | | try |
| | | 40 | | { |
| | 2 | 41 | | var roles = await _rolService.GetAllAsync(); |
| | | 42 | | |
| | 3 | 43 | | var rolesDto = roles.Select(r => new |
| | 3 | 44 | | { |
| | 3 | 45 | | id = r.Id, |
| | 3 | 46 | | nombre = r.Nombre, |
| | 3 | 47 | | descripcion = r.Descripcion |
| | 3 | 48 | | }).ToList(); |
| | | 49 | | |
| | 1 | 50 | | return Ok(ApiResponse<object>.SuccessResult(rolesDto)); |
| | | 51 | | } |
| | 1 | 52 | | catch (Exception ex) |
| | | 53 | | { |
| | 1 | 54 | | _logger.LogError(ex, "Error al obtener roles"); |
| | 1 | 55 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 56 | | } |
| | 2 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Crear rol |
| | | 61 | | /// POST /api/roles |
| | | 62 | | /// </summary> |
| | | 63 | | [HttpPost] |
| | | 64 | | [Authorize(Roles = "Administrador")] |
| | | 65 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status201Created)] |
| | | 66 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 67 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 68 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 69 | | public async Task<IActionResult> CreateRol([FromBody] CreateRolRequest request) |
| | | 70 | | { |
| | | 71 | | try |
| | | 72 | | { |
| | 3 | 73 | | if (string.IsNullOrWhiteSpace(request.Nombre)) |
| | | 74 | | { |
| | 1 | 75 | | return BadRequest(ApiResponse<string>.ErrorResult("El nombre del rol es requerido")); |
| | | 76 | | } |
| | | 77 | | |
| | 2 | 78 | | var (success, rol, error) = await _rolService.CreateAsync( |
| | 2 | 79 | | request.Nombre, |
| | 2 | 80 | | request.Descripcion); |
| | | 81 | | |
| | 2 | 82 | | if (!success) |
| | | 83 | | { |
| | 1 | 84 | | if (error?.Contains("ya existe") == true) |
| | | 85 | | { |
| | 1 | 86 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 87 | | } |
| | 0 | 88 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear rol")); |
| | | 89 | | } |
| | | 90 | | |
| | 1 | 91 | | var response = new |
| | 1 | 92 | | { |
| | 1 | 93 | | id = rol!.Id, |
| | 1 | 94 | | creado = true |
| | 1 | 95 | | }; |
| | | 96 | | |
| | 1 | 97 | | return CreatedAtAction( |
| | 1 | 98 | | nameof(GetRol), |
| | 1 | 99 | | new { id = rol.Id }, |
| | 1 | 100 | | ApiResponse<object>.SuccessResult(response, "Rol creado exitosamente")); |
| | | 101 | | } |
| | 0 | 102 | | catch (Exception ex) |
| | | 103 | | { |
| | 0 | 104 | | _logger.LogError(ex, "Error al crear rol"); |
| | 0 | 105 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 106 | | } |
| | 3 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Obtener rol por ID |
| | | 111 | | /// GET /api/roles/{id} |
| | | 112 | | /// </summary> |
| | | 113 | | [HttpGet("{id}")] |
| | | 114 | | [Authorize(Roles = "Administrador")] |
| | | 115 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 116 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 117 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 118 | | public async Task<IActionResult> GetRol(long id) |
| | | 119 | | { |
| | | 120 | | try |
| | | 121 | | { |
| | 3 | 122 | | var rol = await _rolService.GetByIdAsync(id); |
| | | 123 | | |
| | 2 | 124 | | if (rol == null) |
| | | 125 | | { |
| | 1 | 126 | | return NotFound(ApiResponse<string>.ErrorResult("Rol no encontrado")); |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | var response = new |
| | 1 | 130 | | { |
| | 1 | 131 | | id = rol.Id, |
| | 1 | 132 | | nombre = rol.Nombre, |
| | 1 | 133 | | descripcion = rol.Descripcion |
| | 1 | 134 | | }; |
| | | 135 | | |
| | 1 | 136 | | return Ok(ApiResponse<object>.SuccessResult(response)); |
| | | 137 | | } |
| | 1 | 138 | | catch (Exception ex) |
| | | 139 | | { |
| | 1 | 140 | | _logger.LogError(ex, "Error al obtener rol {Id}", id); |
| | 1 | 141 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 142 | | } |
| | 3 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Editar rol |
| | | 147 | | /// PUT /api/roles/{id} |
| | | 148 | | /// </summary> |
| | | 149 | | [HttpPut("{id}")] |
| | | 150 | | [Authorize(Roles = "Administrador")] |
| | | 151 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 152 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 153 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 154 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 155 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 156 | | public async Task<IActionResult> UpdateRol(long id, [FromBody] UpdateRolRequest request) |
| | | 157 | | { |
| | | 158 | | try |
| | | 159 | | { |
| | 3 | 160 | | if (id != request.Id) |
| | | 161 | | { |
| | 1 | 162 | | return BadRequest(ApiResponse<string>.ErrorResult("El ID del rol no coincide")); |
| | | 163 | | } |
| | | 164 | | |
| | 2 | 165 | | var rolExistente = await _rolService.GetByIdAsync(id); |
| | 2 | 166 | | if (rolExistente == null) |
| | | 167 | | { |
| | 1 | 168 | | return NotFound(ApiResponse<string>.ErrorResult("Rol no encontrado")); |
| | | 169 | | } |
| | | 170 | | |
| | 1 | 171 | | if (!string.IsNullOrWhiteSpace(request.Nombre)) |
| | | 172 | | { |
| | 1 | 173 | | var (success, rol, error) = await _rolService.UpdateAsync( |
| | 1 | 174 | | request.Id, |
| | 1 | 175 | | request.Nombre, |
| | 1 | 176 | | request.Descripcion); |
| | | 177 | | |
| | 1 | 178 | | if (!success) |
| | | 179 | | { |
| | 0 | 180 | | if (error?.Contains("ya existe") == true) |
| | | 181 | | { |
| | 0 | 182 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 183 | | } |
| | 0 | 184 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar rol")); |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | 1 | 188 | | return Ok(ApiResponse<object>.SuccessResult(new { actualizado = true }, "Rol actualizado exitosamente")); |
| | | 189 | | } |
| | 0 | 190 | | catch (Exception ex) |
| | | 191 | | { |
| | 0 | 192 | | _logger.LogError(ex, "Error al actualizar rol {Id}", id); |
| | 0 | 193 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 194 | | } |
| | 3 | 195 | | } |
| | | 196 | | } |