90 lines
3.5 KiB
C#
90 lines
3.5 KiB
C#
|
|
using GestionIntegral.Api.Dtos.Suscripciones;
|
||
|
|
using GestionIntegral.Api.Services.Suscripciones;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using System.Security.Claims;
|
||
|
|
|
||
|
|
namespace GestionIntegral.Api.Controllers.Suscripciones
|
||
|
|
{
|
||
|
|
[Route("api/promociones")]
|
||
|
|
[ApiController]
|
||
|
|
[Authorize]
|
||
|
|
public class PromocionesController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly IPromocionService _promocionService;
|
||
|
|
private readonly ILogger<PromocionesController> _logger;
|
||
|
|
|
||
|
|
// Permiso a crear en BD
|
||
|
|
private const string PermisoGestionarPromociones = "SU010";
|
||
|
|
|
||
|
|
public PromocionesController(IPromocionService promocionService, ILogger<PromocionesController> logger)
|
||
|
|
{
|
||
|
|
_promocionService = promocionService;
|
||
|
|
_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/promociones
|
||
|
|
[HttpGet]
|
||
|
|
public async Task<IActionResult> GetAll([FromQuery] bool soloActivas = true)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionarPromociones)) return Forbid();
|
||
|
|
var promociones = await _promocionService.ObtenerTodas(soloActivas);
|
||
|
|
return Ok(promociones);
|
||
|
|
}
|
||
|
|
|
||
|
|
// GET: api/promociones/{id}
|
||
|
|
[HttpGet("{id:int}", Name = "GetPromocionById")]
|
||
|
|
public async Task<IActionResult> GetById(int id)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionarPromociones)) return Forbid();
|
||
|
|
var promocion = await _promocionService.ObtenerPorId(id);
|
||
|
|
if (promocion == null) return NotFound();
|
||
|
|
return Ok(promocion);
|
||
|
|
}
|
||
|
|
|
||
|
|
// POST: api/promociones
|
||
|
|
[HttpPost]
|
||
|
|
public async Task<IActionResult> Create([FromBody] CreatePromocionDto createDto)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionarPromociones)) return Forbid();
|
||
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||
|
|
|
||
|
|
var userId = GetCurrentUserId();
|
||
|
|
if (userId == null) return Unauthorized();
|
||
|
|
|
||
|
|
var (dto, error) = await _promocionService.Crear(createDto, userId.Value);
|
||
|
|
|
||
|
|
if (error != null) return BadRequest(new { message = error });
|
||
|
|
if (dto == null) return StatusCode(500, "Error al crear la promoción.");
|
||
|
|
|
||
|
|
return CreatedAtRoute("GetPromocionById", new { id = dto.IdPromocion }, dto);
|
||
|
|
}
|
||
|
|
|
||
|
|
// PUT: api/promociones/{id}
|
||
|
|
[HttpPut("{id:int}")]
|
||
|
|
public async Task<IActionResult> Update(int id, [FromBody] UpdatePromocionDto updateDto)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionarPromociones)) return Forbid();
|
||
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||
|
|
|
||
|
|
var userId = GetCurrentUserId();
|
||
|
|
if (userId == null) return Unauthorized();
|
||
|
|
|
||
|
|
var (exito, error) = await _promocionService.Actualizar(id, updateDto, userId.Value);
|
||
|
|
|
||
|
|
if (!exito)
|
||
|
|
{
|
||
|
|
if (error != null && error.Contains("no encontrada")) return NotFound(new { message = error });
|
||
|
|
return BadRequest(new { message = error });
|
||
|
|
}
|
||
|
|
return NoContent();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|