172 lines
6.9 KiB
C#
172 lines
6.9 KiB
C#
using AutoMapper;
|
||
using CuentasCobrar.CORE.DTOs;
|
||
using CuentasCobrar.CORE.Entities;
|
||
using CuentasCobrar.CORE.Exceptions;
|
||
using CuentasCobrar.CORE.Interfaces;
|
||
using CuentasCobrar.CORE.ResponseObjects;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System.Net;
|
||
|
||
namespace CuentasCobrar.API.Controllers
|
||
{
|
||
[Route("api/[controller]")]
|
||
[Produces("application/json")]
|
||
[ApiController]
|
||
public class RolDeUsuarioController : ControllerBase
|
||
{
|
||
private readonly IRolDeUsuarioRepo _rolDeUsuarioRepo;
|
||
private readonly IMapper _mapper;
|
||
public RolDeUsuarioController(IRolDeUsuarioRepo rolDeUsuarioRepo, IMapper mapper)
|
||
{
|
||
_rolDeUsuarioRepo = rolDeUsuarioRepo;
|
||
_mapper = mapper;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Obtiene todos las roles vinculados a un usuario
|
||
/// </summary>
|
||
/// <returns>
|
||
/// Retorna todos roles vinculados a un usuario de la base de datos
|
||
/// </returns>
|
||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<RolDeUsuarioDTO>))]
|
||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
||
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
||
[HttpGet("{idUsuario}"), Authorize(Roles = "ADMINISTRADOR")]
|
||
public async Task<IActionResult> Get(string idUsuario)
|
||
{
|
||
try
|
||
{
|
||
var rolDeUsuario = await _rolDeUsuarioRepo.Get(idUsuario);
|
||
if (rolDeUsuario == null)
|
||
{
|
||
throw new BadRequestException("Hubo un problema al obtener los roles de usuario");
|
||
}
|
||
var rolDeUsuarioDto = _mapper.Map<IEnumerable<RolDeUsuarioDTO>>(rolDeUsuario);
|
||
return Ok(rolDeUsuarioDto);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Obtiene el rol de un usuario especificados por id
|
||
/// </summary>
|
||
/// <param name="idUsuario"></param>
|
||
/// <param name="idRol"></param>
|
||
/// <returns>Retorna los datos del rol de usuario específicado</returns>
|
||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(RolDeUsuarioDTO))]
|
||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
||
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
||
[HttpGet("{idUsuario}/{idRol}"), Authorize(Roles = "ADMINISTRADOR")]
|
||
public async Task<IActionResult> Get(string idUsuario, int idRol)
|
||
{
|
||
try
|
||
{
|
||
var rolDeUsuario = await _rolDeUsuarioRepo.Get(idUsuario, idRol);
|
||
if (rolDeUsuario == null)
|
||
{
|
||
throw new BadRequestException("El usuario no se ecuentra asociada a dicho rol");
|
||
}
|
||
var rolDeUsuarioDto = _mapper.Map<RolDeUsuarioDTO>(rolDeUsuario);
|
||
return Ok(rolDeUsuarioDto);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Agrega un rol vinculado a un usuario
|
||
/// </summary>
|
||
/// <param name="rolDeUsuarioDto"></param>
|
||
[ProducesResponseType((int)HttpStatusCode.OK)]
|
||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
||
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
||
[HttpPost, Authorize(Roles = "ADMINISTRADOR")]
|
||
public async Task<IActionResult> Post(RolDeUsuarioDTO rolDeUsuarioDto)
|
||
{
|
||
try
|
||
{
|
||
var rolDeUsuario = _mapper.Map<RolDeUsuario>(rolDeUsuarioDto);
|
||
var rolDeUsuarioTemp = await _rolDeUsuarioRepo.Get(rolDeUsuario.IdUsuario, rolDeUsuario.IdRol);
|
||
if (rolDeUsuarioTemp != null)
|
||
{
|
||
throw new BadRequestException("El rol de usuario ya existe");
|
||
}
|
||
bool agregado = await _rolDeUsuarioRepo.Post(rolDeUsuario);
|
||
if (agregado == false)
|
||
{
|
||
throw new BadRequestException("Hubo un problema al agregar el rol de usuario");
|
||
}
|
||
return Ok(agregado);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Elimina el rol vinculado a un usuario especificados por id
|
||
/// </summary>
|
||
/// <param name="idUsuario"></param>
|
||
/// <param name="idRol"></param>
|
||
[ProducesResponseType((int)HttpStatusCode.OK)]
|
||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
|
||
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
|
||
[HttpDelete("{idUsuario}/{idRol}"), Authorize(Roles = "ADMINISTRADOR")]
|
||
public async Task<IActionResult> Delete(string idUsuario, int idRol)
|
||
{
|
||
try
|
||
{
|
||
var rolDeUsuarioTemp = await _rolDeUsuarioRepo.Get(idUsuario, idRol);
|
||
if (rolDeUsuarioTemp == null)
|
||
{
|
||
throw new BadRequestException("El rol de usuario a eliminar no existe");
|
||
}
|
||
bool eliminado = await _rolDeUsuarioRepo.Delete(idUsuario, idRol);
|
||
if (eliminado == false)
|
||
{
|
||
throw new BadRequestException("Hubo un problema al eliminar el rol de usuario");
|
||
}
|
||
return Ok(eliminado);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.GetType() != typeof(BadRequestException))
|
||
{
|
||
throw new InternalErrorException("Hubo un problema en el servidor");
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|