123 lines
6.2 KiB
C#
123 lines
6.2 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}/recargos")] // Anidado bajo publicaciones
|
||
|
|
[ApiController]
|
||
|
|
[Authorize]
|
||
|
|
public class RecargosZonaController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly IRecargoZonaService _recargoZonaService;
|
||
|
|
private readonly ILogger<RecargosZonaController> _logger;
|
||
|
|
|
||
|
|
// Permiso DP005 para gestionar recargos
|
||
|
|
private const string PermisoGestionarRecargos = "DP005";
|
||
|
|
|
||
|
|
public RecargosZonaController(IRecargoZonaService recargoZonaService, ILogger<RecargosZonaController> logger)
|
||
|
|
{
|
||
|
|
_recargoZonaService = recargoZonaService;
|
||
|
|
_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}/recargos
|
||
|
|
[HttpGet]
|
||
|
|
[ProducesResponseType(typeof(IEnumerable<RecargoZonaDto>), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
public async Task<IActionResult> GetRecargosPorPublicacion(int idPublicacion)
|
||
|
|
{
|
||
|
|
if (!TienePermiso("DP001") && !TienePermiso(PermisoGestionarRecargos)) return Forbid();
|
||
|
|
var recargos = await _recargoZonaService.ObtenerPorPublicacionIdAsync(idPublicacion);
|
||
|
|
return Ok(recargos);
|
||
|
|
}
|
||
|
|
|
||
|
|
// GET: api/publicaciones/{idPublicacion}/recargos/{idRecargo}
|
||
|
|
[HttpGet("{idRecargo:int}", Name = "GetRecargoZonaById")]
|
||
|
|
[ProducesResponseType(typeof(RecargoZonaDto), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetRecargoZonaById(int idPublicacion, int idRecargo)
|
||
|
|
{
|
||
|
|
if (!TienePermiso("DP001") && !TienePermiso(PermisoGestionarRecargos)) return Forbid();
|
||
|
|
var recargo = await _recargoZonaService.ObtenerPorIdAsync(idRecargo);
|
||
|
|
if (recargo == null || recargo.IdPublicacion != idPublicacion) return NotFound();
|
||
|
|
return Ok(recargo);
|
||
|
|
}
|
||
|
|
|
||
|
|
// POST: api/publicaciones/{idPublicacion}/recargos
|
||
|
|
[HttpPost]
|
||
|
|
[ProducesResponseType(typeof(RecargoZonaDto), StatusCodes.Status201Created)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
public async Task<IActionResult> CreateRecargoZona(int idPublicacion, [FromBody] CreateRecargoZonaDto createDto)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionarRecargos)) 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 _recargoZonaService.CrearAsync(createDto, userId.Value);
|
||
|
|
if (error != null) return BadRequest(new { message = error });
|
||
|
|
if (dto == null) return StatusCode(StatusCodes.Status500InternalServerError, "Error al crear recargo.");
|
||
|
|
return CreatedAtRoute("GetRecargoZonaById", new { idPublicacion = dto.IdPublicacion, idRecargo = dto.IdRecargo }, dto);
|
||
|
|
}
|
||
|
|
|
||
|
|
// PUT: api/publicaciones/{idPublicacion}/recargos/{idRecargo}
|
||
|
|
[HttpPut("{idRecargo:int}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> UpdateRecargoZona(int idPublicacion, int idRecargo, [FromBody] UpdateRecargoZonaDto updateDto)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionarRecargos)) return Forbid();
|
||
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||
|
|
var userId = GetCurrentUserId();
|
||
|
|
if (userId == null) return Unauthorized();
|
||
|
|
|
||
|
|
var recargoExistente = await _recargoZonaService.ObtenerPorIdAsync(idRecargo);
|
||
|
|
if (recargoExistente == null || recargoExistente.IdPublicacion != idPublicacion)
|
||
|
|
return NotFound(new { message = "Recargo no encontrado para esta publicación."});
|
||
|
|
|
||
|
|
var (exito, error) = await _recargoZonaService.ActualizarAsync(idRecargo, updateDto, userId.Value);
|
||
|
|
if (!exito) return BadRequest(new { message = error });
|
||
|
|
return NoContent();
|
||
|
|
}
|
||
|
|
|
||
|
|
// DELETE: api/publicaciones/{idPublicacion}/recargos/{idRecargo}
|
||
|
|
[HttpDelete("{idRecargo:int}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> DeleteRecargoZona(int idPublicacion, int idRecargo)
|
||
|
|
{
|
||
|
|
if (!TienePermiso(PermisoGestionarRecargos)) return Forbid();
|
||
|
|
var userId = GetCurrentUserId();
|
||
|
|
if (userId == null) return Unauthorized();
|
||
|
|
|
||
|
|
var recargoExistente = await _recargoZonaService.ObtenerPorIdAsync(idRecargo);
|
||
|
|
if (recargoExistente == null || recargoExistente.IdPublicacion != idPublicacion)
|
||
|
|
return NotFound(new { message = "Recargo no encontrado para esta publicación."});
|
||
|
|
|
||
|
|
var (exito, error) = await _recargoZonaService.EliminarAsync(idRecargo, userId.Value);
|
||
|
|
if (!exito) return BadRequest(new { message = error });
|
||
|
|
return NoContent();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|