224 lines
8.7 KiB
C#
224 lines
8.7 KiB
C#
using CuentasCobrar.CORE.Exceptions;
|
|
using CuentasCobrar.UI.Helpers;
|
|
using CuentasCobrar.UI.Models;
|
|
using CuentasCobrar.UI.Services.Interfaces;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using static CuentasCobrar.UI.Helpers.Enum;
|
|
|
|
namespace CuentasCobrar.UI.Controllers
|
|
{
|
|
public class RolController : BaseController
|
|
{
|
|
private readonly IRolAPI _rolAPI;
|
|
private readonly IUsuarioAPI _usuarioAPI;
|
|
|
|
public RolController ( IUsuarioAPI usuarioAPI, IRolAPI rolAPI )
|
|
{
|
|
_rolAPI = rolAPI;
|
|
_usuarioAPI = usuarioAPI;
|
|
}
|
|
public async Task<ActionResult> Index ()
|
|
{
|
|
List<RolViewModel>? roles = new List<RolViewModel> ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
roles = await _rolAPI.ObtenerRoles ( token );
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", "Home" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Rol" );
|
|
return RedirectToAction ( "Index", "Home" );
|
|
}
|
|
}
|
|
return View ( roles );
|
|
}
|
|
public async Task<ActionResult> ActualizarRol ( RolViewModel rol )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
bool respuesta = false;
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
respuesta = await _rolAPI.ActualizarRol ( token, rol );
|
|
if (respuesta)
|
|
{
|
|
CustomNotification ( "Se modificó el rol de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Rol" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Rol" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ViewBag.Roles = await _rolAPI.ObtenerRoles ( token );
|
|
return View ( "Editar", rol );
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Rol" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<ActionResult> AgregarRol ( RolViewModel rol )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
bool respuesta = false;
|
|
if (ModelState.IsValid)
|
|
{
|
|
respuesta = await _rolAPI.AgregarRol ( token, rol );
|
|
if (respuesta)
|
|
{
|
|
CustomNotification ( "Se agregó el rol de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Rol" );
|
|
return RedirectToAction ( "Index" );
|
|
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Rol" );
|
|
return RedirectToAction ( "Index" );
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ViewBag.Roles = await _rolAPI.ObtenerRoles ( token );
|
|
return View ( "Agregar", rol );
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Rol" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task EliminarRol ( int id )
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
await _rolAPI.EliminarRol ( token, id );
|
|
}
|
|
|
|
public async Task<ActionResult> Editar ( int id )
|
|
{
|
|
RolViewModel? rol = new RolViewModel ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
ViewBag.Roles = await _rolAPI.ObtenerRoles ( token );
|
|
rol = await _rolAPI.ObtenerRol ( token, id );
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Rol" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
}
|
|
return View ( rol );
|
|
}
|
|
|
|
public async Task<ActionResult> Agregar ()
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
ViewBag.Roles = await _rolAPI.ObtenerRoles ( token );
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Rol" );
|
|
return RedirectToAction ( "Index" );
|
|
}
|
|
}
|
|
return View ();
|
|
}
|
|
}
|
|
}
|
|
|