119 lines
6.0 KiB
C#
119 lines
6.0 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}/porcentajesmoncanilla")] // Anidado
|
||
|
|
[ApiController]
|
||
|
|
[Authorize]
|
||
|
|
public class PorcentajesMonCanillaController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly IPorcMonCanillaService _porcMonCanillaService;
|
||
|
|
private readonly ILogger<PorcentajesMonCanillaController> _logger;
|
||
|
|
|
||
|
|
// Permiso CG004 para porcentajes de pago de canillitas
|
||
|
|
private const string PermisoGestionar = "CG004";
|
||
|
|
|
||
|
|
public PorcentajesMonCanillaController(IPorcMonCanillaService porcMonCanillaService, ILogger<PorcentajesMonCanillaController> logger)
|
||
|
|
{
|
||
|
|
_porcMonCanillaService = porcMonCanillaService;
|
||
|
|
_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;
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet]
|
||
|
|
[ProducesResponseType(typeof(IEnumerable<PorcMonCanillaDto>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
public async Task<IActionResult> GetPorcMonCanillaPorPublicacion(int idPublicacion)
|
||
|
|
{
|
||
|
|
// DP001 para ver publicación, CG004 para gestionar específicamente esto
|
||
|
|
if (!TienePermiso("DP001") && !TienePermiso(PermisoGestionar)) return Forbid();
|
||
|
|
var items = await _porcMonCanillaService.ObtenerPorPublicacionIdAsync(idPublicacion);
|
||
|
|
return Ok(items);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("{idPorcMon:int}", Name = "GetPorcMonCanillaById")]
|
||
|
|
[ProducesResponseType(typeof(PorcMonCanillaDto), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetPorcMonCanillaById(int idPublicacion, int idPorcMon)
|
||
|
|
{
|
||
|
|
if (!TienePermiso("DP001") && !TienePermiso(PermisoGestionar)) return Forbid();
|
||
|
|
var item = await _porcMonCanillaService.ObtenerPorIdAsync(idPorcMon);
|
||
|
|
if (item == null || item.IdPublicacion != idPublicacion) return NotFound();
|
||
|
|
return Ok(item);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost]
|
||
|
|
[ProducesResponseType(typeof(PorcMonCanillaDto), StatusCodes.Status201Created)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
public async Task<IActionResult> CreatePorcMonCanilla(int idPublicacion, [FromBody] CreatePorcMonCanillaDto createDto)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionar)) 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 _porcMonCanillaService.CrearAsync(createDto, userId.Value);
|
||
|
|
if (error != null) return BadRequest(new { message = error });
|
||
|
|
if (dto == null) return StatusCode(StatusCodes.Status500InternalServerError, "Error al crear.");
|
||
|
|
return CreatedAtRoute("GetPorcMonCanillaById", new { idPublicacion = dto.IdPublicacion, idPorcMon = dto.IdPorcMon }, dto);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPut("{idPorcMon:int}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> UpdatePorcMonCanilla(int idPublicacion, int idPorcMon, [FromBody] UpdatePorcMonCanillaDto updateDto)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionar)) return Forbid();
|
||
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||
|
|
var userId = GetCurrentUserId();
|
||
|
|
if (userId == null) return Unauthorized();
|
||
|
|
|
||
|
|
var existente = await _porcMonCanillaService.ObtenerPorIdAsync(idPorcMon);
|
||
|
|
if (existente == null || existente.IdPublicacion != idPublicacion)
|
||
|
|
return NotFound(new { message = "Registro no encontrado para esta publicación."});
|
||
|
|
|
||
|
|
var (exito, error) = await _porcMonCanillaService.ActualizarAsync(idPorcMon, updateDto, userId.Value);
|
||
|
|
if (!exito) return BadRequest(new { message = error });
|
||
|
|
return NoContent();
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpDelete("{idPorcMon:int}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> DeletePorcMonCanilla(int idPublicacion, int idPorcMon)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionar)) return Forbid();
|
||
|
|
var userId = GetCurrentUserId();
|
||
|
|
if (userId == null) return Unauthorized();
|
||
|
|
|
||
|
|
var existente = await _porcMonCanillaService.ObtenerPorIdAsync(idPorcMon);
|
||
|
|
if (existente == null || existente.IdPublicacion != idPublicacion)
|
||
|
|
return NotFound(new { message = "Registro no encontrado para esta publicación."});
|
||
|
|
|
||
|
|
var (exito, error) = await _porcMonCanillaService.EliminarAsync(idPorcMon, userId.Value);
|
||
|
|
if (!exito) return BadRequest(new { message = error });
|
||
|
|
return NoContent();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|