140 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			140 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/[controller]")]
 | |
|     [ApiController]
 | |
|     [Authorize]
 | |
|     public class DistribuidoresController : ControllerBase
 | |
|     {
 | |
|         private readonly IDistribuidorService _distribuidorService;
 | |
|         private readonly ILogger<DistribuidoresController> _logger;
 | |
| 
 | |
|         // Permisos para Distribuidores (DG001 a DG005)
 | |
|         private const string PermisoVer = "DG001";
 | |
|         private const string PermisoCrear = "DG002";
 | |
|         private const string PermisoModificar = "DG003";
 | |
|         // DG004 es para Porcentajes, se manejará en un endpoint/controlador dedicado
 | |
|         private const string PermisoEliminar = "DG005";
 | |
| 
 | |
|         public DistribuidoresController(IDistribuidorService distribuidorService, ILogger<DistribuidoresController> logger)
 | |
|         {
 | |
|             _distribuidorService = distribuidorService;
 | |
|             _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<DistribuidorDto>), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         public async Task<IActionResult> GetAllDistribuidores([FromQuery] string? nombre, [FromQuery] string? nroDoc)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoVer)) return Forbid();
 | |
|             var distribuidores = await _distribuidorService.ObtenerTodosAsync(nombre, nroDoc);
 | |
|             return Ok(distribuidores);
 | |
|         }
 | |
| 
 | |
|         [HttpGet("dropdown")]
 | |
|         [ProducesResponseType(typeof(IEnumerable<DistribuidorDropdownDto>), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         public async Task<IActionResult> GetAllDropdownDistribuidores()
 | |
|         {
 | |
|             var distribuidores = await _distribuidorService.GetAllDropdownAsync();
 | |
|             return Ok(distribuidores);
 | |
|         }
 | |
| 
 | |
|         [HttpGet("{id:int}", Name = "GetDistribuidorById")]
 | |
|         [ProducesResponseType(typeof(DistribuidorDto), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         public async Task<IActionResult> GetDistribuidorById(int id)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoVer)) return Forbid();
 | |
|             var distribuidor = await _distribuidorService.ObtenerPorIdAsync(id);
 | |
|             if (distribuidor == null) return NotFound();
 | |
|             return Ok(distribuidor);
 | |
|         }
 | |
| 
 | |
|         [HttpGet("{id:int}/lookup", Name = "GetDistribuidorLookupById")]
 | |
|         [ProducesResponseType(typeof(DistribuidorLookupDto), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         public async Task<IActionResult> ObtenerLookupPorIdAsync(int id)
 | |
|         {
 | |
|             var distribuidor = await _distribuidorService.ObtenerLookupPorIdAsync(id);
 | |
|             if (distribuidor == null) return NotFound();
 | |
|             return Ok(distribuidor);
 | |
|         }
 | |
| 
 | |
|         [HttpPost]
 | |
|         [ProducesResponseType(typeof(DistribuidorDto), StatusCodes.Status201Created)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         public async Task<IActionResult> CreateDistribuidor([FromBody] CreateDistribuidorDto createDto)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoCrear)) return Forbid();
 | |
|             if (!ModelState.IsValid) return BadRequest(ModelState);
 | |
|             var userId = GetCurrentUserId();
 | |
|             if (userId == null) return Unauthorized();
 | |
| 
 | |
|             var (dto, error) = await _distribuidorService.CrearAsync(createDto, userId.Value);
 | |
|             if (error != null) return BadRequest(new { message = error });
 | |
|             if (dto == null) return StatusCode(StatusCodes.Status500InternalServerError, "Error al crear.");
 | |
|             return CreatedAtRoute("GetDistribuidorById", new { id = dto.IdDistribuidor }, dto);
 | |
|         }
 | |
| 
 | |
|         [HttpPut("{id:int}")]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         public async Task<IActionResult> UpdateDistribuidor(int id, [FromBody] UpdateDistribuidorDto updateDto)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoModificar)) return Forbid();
 | |
|             if (!ModelState.IsValid) return BadRequest(ModelState);
 | |
|             var userId = GetCurrentUserId();
 | |
|             if (userId == null) return Unauthorized();
 | |
| 
 | |
|             var (exito, error) = await _distribuidorService.ActualizarAsync(id, updateDto, userId.Value);
 | |
|             if (!exito)
 | |
|             {
 | |
|                 if (error == "Distribuidor no encontrado.") return NotFound(new { message = error });
 | |
|                 return BadRequest(new { message = error });
 | |
|             }
 | |
|             return NoContent();
 | |
|         }
 | |
| 
 | |
|         [HttpDelete("{id:int}")]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         public async Task<IActionResult> DeleteDistribuidor(int id)
 | |
|         {
 | |
|             if (!TienePermiso(PermisoEliminar)) return Forbid();
 | |
|             var userId = GetCurrentUserId();
 | |
|             if (userId == null) return Unauthorized();
 | |
| 
 | |
|             var (exito, error) = await _distribuidorService.EliminarAsync(id, userId.Value);
 | |
|             if (!exito)
 | |
|             {
 | |
|                 if (error == "Distribuidor no encontrado.") return NotFound(new { message = error });
 | |
|                 return BadRequest(new { message = error });
 | |
|             }
 | |
|             return NoContent();
 | |
|         }
 | |
|     }
 | |
| } |