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 RolController : ControllerBase { private readonly IRolRepo _RolRepo; private readonly IMapper _mapper; public RolController(IRolRepo rolRepo, IMapper mapper) { _RolRepo = rolRepo; _mapper = mapper; } ///         /// Obtiene todos los roles         ///         ///         /// Retorna todos los roles 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, Authorize(Roles = "ADMINISTRADOR")] public async Task Get() { try { var roles = await _RolRepo.Get(); if (roles == null) { throw new BadRequestException("Hubo un problema al obtener los roles"); } var rolesDto = _mapper.Map>(roles); return Ok(rolesDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Obtiene el rol especificada por su id         ///         ///         /// Retorna los datos del rol especificado [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(RolDTO))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpGet("{idRol}"), Authorize(Roles = "ADMINISTRADOR")] public async Task Get(int idRol) { try { var rol = await _RolRepo.Get(idRol); if (rol == null) { throw new BadRequestException("El rol no existe"); } var rolDto = _mapper.Map(rol); return Ok(rolDto); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Agrega un rol         ///         ///         [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(RolDTO rolDto) { try { var rol = _mapper.Map(rolDto); bool agregado = await _RolRepo.Post(rol); if (agregado == false) { throw new BadRequestException("Hubo un problema al agregar el rol"); } return Ok(agregado); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Actualiza el rol especificado por id         ///         ///         /// Accion         [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpPut("{idRol}"), Authorize(Roles = "ADMINISTRADOR")] public async Task Put(int idRol, RolDTO rolDto) { try { var rolTemp = await _RolRepo.Get(idRol); if (rolTemp == null) { throw new BadRequestException("El rol a modificar no existe"); } var rol = _mapper.Map(rolDto); rol.IdRol = idRol; bool modificado = await _RolRepo.Put(rol); if (modificado == false) { throw new BadRequestException("Hubo un problema al modificar el rol"); } return Ok(modificado); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } ///         /// Elimina el rol especificado por id         ///         ///         [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))] [ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))] [HttpDelete("{idRol}"), Authorize(Roles = "ADMINISTRADOR")] public async Task Delete(int idRol) { try { var rolTemp = await _RolRepo.Get(idRol); if (rolTemp == null) { throw new BadRequestException("El rol a eliminar no existe"); } bool eliminado = await _RolRepo.Delete(idRol); if (eliminado == false) { throw new BadRequestException("Hubo un problema al eliminar el rol"); } return Ok(eliminado); } catch (Exception ex) { if (ex.GetType() != typeof(BadRequestException)) { throw new InternalErrorException("Hubo un problema en el servidor"); } else { throw; } } } } }