TFS_ANTIGUO/TPAtesa_CuentasCobrar/CuentasCobrar.API/Controllers/RolController.cs

206 lines
7.4 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
/// <summary>
        /// Obtiene todos los roles
        /// </summary>
        /// <returns>
        /// Retorna todos los roles de la base de datos
        /// </returns>
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable<RolDTO>))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(ExceptionResponse))]
[ProducesResponseType((int)HttpStatusCode.InternalServerError, Type = typeof(ExceptionResponse))]
[HttpGet, Authorize(Roles = "ADMINISTRADOR")]
public async Task<IActionResult> Get()
{
try
{
var roles = await _RolRepo.Get();
if (roles == null)
{
throw new BadRequestException("Hubo un problema al obtener los roles");
}
var rolesDto = _mapper.Map<IEnumerable<RolDTO>>(roles);
return Ok(rolesDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
/// <summary>
        /// Obtiene el rol especificada por su id
        /// </summary>
        /// <param name="idRol"></param>
        /// <returns>Retorna los datos del rol especificado</returns>
[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<IActionResult> Get(int idRol)
{
try
{
var rol = await _RolRepo.Get(idRol);
if (rol == null)
{
throw new BadRequestException("El rol no existe");
}
var rolDto = _mapper.Map<RolDTO>(rol);
return Ok(rolDto);
}
catch (Exception ex)
{
if (ex.GetType() != typeof(BadRequestException))
{
throw new InternalErrorException("Hubo un problema en el servidor");
}
else
{
throw;
}
}
}
/// <summary>
        /// Agrega un rol
        /// </summary>
        /// <param name="rolDto"></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(RolDTO rolDto)
{
try
{
var rol = _mapper.Map<Rol>(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;
}
}
}
/// <summary>
        /// Actualiza el rol especificado por id
        /// </summary>
        /// <param name="idRol"></param>
        /// <param name="rolDto">Accion</param>
        [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<IActionResult> 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<Rol>(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;
}
}
}
/// <summary>
        /// Elimina el rol especificado por id
        /// </summary>
        /// <param name="idRol"></param>
        [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<IActionResult> 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;
}
}
}
}
}