124 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			124 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
|  | using GestionIntegral.Api.Dtos.Distribucion; | ||
|  | using GestionIntegral.Api.Services.Distribucion; | ||
|  | using Microsoft.AspNetCore.Authorization; | ||
|  | using Microsoft.AspNetCore.Mvc; | ||
|  | using Microsoft.Extensions.Logging; | ||
|  | using System.Collections.Generic; | ||
|  | using System.Security.Claims; | ||
|  | using System.Threading.Tasks; | ||
|  | 
 | ||
|  | namespace GestionIntegral.Api.Controllers.Distribucion | ||
|  | { | ||
|  |     [Route("api/publicaciones/{idPublicacion}/secciones")] // Anidado | ||
|  |     [ApiController] | ||
|  |     [Authorize] | ||
|  |     public class PubliSeccionesController : ControllerBase | ||
|  |     { | ||
|  |         private readonly IPubliSeccionService _publiSeccionService; | ||
|  |         private readonly ILogger<PubliSeccionesController> _logger; | ||
|  | 
 | ||
|  |         // Permiso DP007 para gestionar secciones de publicaciones | ||
|  |         private const string PermisoGestionarSecciones = "DP007"; | ||
|  | 
 | ||
|  |         public PubliSeccionesController(IPubliSeccionService publiSeccionService, ILogger<PubliSeccionesController> logger) | ||
|  |         { | ||
|  |             _publiSeccionService = publiSeccionService; | ||
|  |             _logger = logger; | ||
|  |         } | ||
|  | 
 | ||
|  |         private bool TienePermiso(string codAcc) => User.IsInRole("SuperAdmin") || User.HasClaim(c => c.Type == "permission" && c.Value == codAcc); | ||
|  |         private int? GetCurrentUserId() | ||
|  |         { | ||
|  |             if (int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub"), out int userId)) return userId; | ||
|  |             return null; | ||
|  |         } | ||
|  | 
 | ||
|  |         // GET: api/publicaciones/{idPublicacion}/secciones | ||
|  |         [HttpGet] | ||
|  |         [ProducesResponseType(typeof(IEnumerable<PubliSeccionDto>), StatusCodes.Status200OK)] | ||
|  |         [ProducesResponseType(StatusCodes.Status403Forbidden)] | ||
|  |         public async Task<IActionResult> GetSeccionesPorPublicacion(int idPublicacion, [FromQuery] bool? soloActivas) | ||
|  |         { | ||
|  |             // DP001 para ver publicación, DP007 para gestionar específicamente secciones | ||
|  |             if (!TienePermiso("DP001") && !TienePermiso(PermisoGestionarSecciones)) return Forbid(); | ||
|  |             var secciones = await _publiSeccionService.ObtenerPorPublicacionIdAsync(idPublicacion, soloActivas); | ||
|  |             return Ok(secciones); | ||
|  |         } | ||
|  | 
 | ||
|  |         // GET: api/publicaciones/{idPublicacion}/secciones/{idSeccion} | ||
|  |         [HttpGet("{idSeccion:int}", Name = "GetPubliSeccionById")] | ||
|  |         [ProducesResponseType(typeof(PubliSeccionDto), StatusCodes.Status200OK)] | ||
|  |         [ProducesResponseType(StatusCodes.Status403Forbidden)] | ||
|  |         [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
|  |         public async Task<IActionResult> GetPubliSeccionById(int idPublicacion, int idSeccion) | ||
|  |         { | ||
|  |             if (!TienePermiso("DP001") && !TienePermiso(PermisoGestionarSecciones)) return Forbid(); | ||
|  |             var seccion = await _publiSeccionService.ObtenerPorIdAsync(idSeccion); | ||
|  |             if (seccion == null || seccion.IdPublicacion != idPublicacion) return NotFound(); | ||
|  |             return Ok(seccion); | ||
|  |         } | ||
|  | 
 | ||
|  |         // POST: api/publicaciones/{idPublicacion}/secciones | ||
|  |         [HttpPost] | ||
|  |         [ProducesResponseType(typeof(PubliSeccionDto), StatusCodes.Status201Created)] | ||
|  |         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
|  |         [ProducesResponseType(StatusCodes.Status403Forbidden)] | ||
|  |         public async Task<IActionResult> CreatePubliSeccion(int idPublicacion, [FromBody] CreatePubliSeccionDto createDto) | ||
|  |         { | ||
|  |             if (!TienePermiso(PermisoGestionarSecciones)) return Forbid(); | ||
|  |             if (idPublicacion != createDto.IdPublicacion) | ||
|  |                 return BadRequest(new { message = "ID de publicación en ruta no coincide con el del cuerpo." }); | ||
|  |             if (!ModelState.IsValid) return BadRequest(ModelState); | ||
|  |             var userId = GetCurrentUserId(); | ||
|  |             if (userId == null) return Unauthorized(); | ||
|  | 
 | ||
|  |             var (dto, error) = await _publiSeccionService.CrearAsync(createDto, userId.Value); | ||
|  |             if (error != null) return BadRequest(new { message = error }); | ||
|  |             if (dto == null) return StatusCode(StatusCodes.Status500InternalServerError, "Error al crear sección."); | ||
|  |             return CreatedAtRoute("GetPubliSeccionById", new { idPublicacion = dto.IdPublicacion, idSeccion = dto.IdSeccion }, dto); | ||
|  |         } | ||
|  | 
 | ||
|  |         // PUT: api/publicaciones/{idPublicacion}/secciones/{idSeccion} | ||
|  |         [HttpPut("{idSeccion:int}")] | ||
|  |         [ProducesResponseType(StatusCodes.Status204NoContent)] | ||
|  |         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
|  |         [ProducesResponseType(StatusCodes.Status403Forbidden)] | ||
|  |         [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
|  |         public async Task<IActionResult> UpdatePubliSeccion(int idPublicacion, int idSeccion, [FromBody] UpdatePubliSeccionDto updateDto) | ||
|  |         { | ||
|  |             if (!TienePermiso(PermisoGestionarSecciones)) return Forbid(); | ||
|  |             if (!ModelState.IsValid) return BadRequest(ModelState); | ||
|  |             var userId = GetCurrentUserId(); | ||
|  |             if (userId == null) return Unauthorized(); | ||
|  | 
 | ||
|  |             var existente = await _publiSeccionService.ObtenerPorIdAsync(idSeccion); | ||
|  |             if (existente == null || existente.IdPublicacion != idPublicacion) | ||
|  |                 return NotFound(new { message = "Sección no encontrada para esta publicación."}); | ||
|  | 
 | ||
|  |             var (exito, error) = await _publiSeccionService.ActualizarAsync(idSeccion, updateDto, userId.Value); | ||
|  |             if (!exito) return BadRequest(new { message = error }); | ||
|  |             return NoContent(); | ||
|  |         } | ||
|  | 
 | ||
|  |         // DELETE: api/publicaciones/{idPublicacion}/secciones/{idSeccion} | ||
|  |         [HttpDelete("{idSeccion:int}")] | ||
|  |         [ProducesResponseType(StatusCodes.Status204NoContent)] | ||
|  |         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
|  |         [ProducesResponseType(StatusCodes.Status403Forbidden)] | ||
|  |         [ProducesResponseType(StatusCodes.Status404NotFound)] | ||
|  |         public async Task<IActionResult> DeletePubliSeccion(int idPublicacion, int idSeccion) | ||
|  |         { | ||
|  |             if (!TienePermiso(PermisoGestionarSecciones)) return Forbid(); | ||
|  |             var userId = GetCurrentUserId(); | ||
|  |             if (userId == null) return Unauthorized(); | ||
|  | 
 | ||
|  |              var existente = await _publiSeccionService.ObtenerPorIdAsync(idSeccion); | ||
|  |             if (existente == null || existente.IdPublicacion != idPublicacion) | ||
|  |                 return NotFound(new { message = "Sección no encontrada para esta publicación."}); | ||
|  | 
 | ||
|  |             var (exito, error) = await _publiSeccionService.EliminarAsync(idSeccion, userId.Value); | ||
|  |             if (!exito) return BadRequest(new { message = error }); | ||
|  |             return NoContent(); | ||
|  |         } | ||
|  |     } | ||
|  | } |