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; } ///         /// Obtiene todos las roles vinculados a un usuario         ///         ///         /// Retorna todos roles vinculados a un usuario de la base de datos         /// [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpGet("{idUsuario}"), Authorize(Roles = "ADMINISTRADOR")] public async Task 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>(rolDeUsuario); return Ok(rolDeUsuarioDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Obtiene el rol de un usuario especificados por id         ///         ///         ///         /// Retorna los datos del rol de usuario específicado [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 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(rolDeUsuario); return Ok(rolDeUsuarioDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Agrega un rol vinculado a un usuario         ///         ///         [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 Post(RolDeUsuarioDTO rolDeUsuarioDto) { try { var rolDeUsuario = _mapper.Map(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; } } } ///         /// Elimina el rol vinculado a un usuario especificados por id         ///         ///         ///         [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 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; } } } } }