251 lines
9.9 KiB
C#
251 lines
9.9 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 MonedaController : BaseController
|
|||
|
|
{
|
|||
|
|
private readonly IMonedaAPI _monedaAPI;
|
|||
|
|
private readonly IUsuarioAPI _usuarioAPI;
|
|||
|
|
|
|||
|
|
|
|||
|
|
public MonedaController ( IUsuarioAPI usuarioAPI, IMonedaAPI monedaAPI )
|
|||
|
|
{
|
|||
|
|
_monedaAPI = monedaAPI;
|
|||
|
|
_usuarioAPI = usuarioAPI;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
public async Task<ActionResult> Index ()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
List<MonedaViewModel> monedas = new List<MonedaViewModel> ();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var token = Request.Cookies["CookieToken"];
|
|||
|
|
if (token == null)
|
|||
|
|
{
|
|||
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|||
|
|
token = tokenUsuario;
|
|||
|
|
CrearCookies ( tokenUsuario );
|
|||
|
|
}
|
|||
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|||
|
|
var listaRoles = new List<string> ( rolesCookie.Split ( "," ) );
|
|||
|
|
if (!listaRoles.Exists ( r => r == "ADMINISTRADOR" || r == "OPERATIVO" ))
|
|||
|
|
{
|
|||
|
|
throw new AuthorizeException ( 403, "Usuario no autorizado" );
|
|||
|
|
}
|
|||
|
|
monedas = await _monedaAPI.ObtenerMonedas ( 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, "Moneda" );
|
|||
|
|
return RedirectToAction ( "Index", "Home" );
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return View ( monedas );
|
|||
|
|
}
|
|||
|
|
public async Task<ActionResult> ActualizarMoneda ( MonedaViewModel moneda )
|
|||
|
|
{
|
|||
|
|
bool respuesta = false;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var token = Request.Cookies["CookieToken"];
|
|||
|
|
if (token == null)
|
|||
|
|
{
|
|||
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|||
|
|
token = tokenUsuario;
|
|||
|
|
CrearCookies ( tokenUsuario );
|
|||
|
|
}
|
|||
|
|
if (ModelState.IsValid)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
respuesta = await _monedaAPI.ActualizarMoneda ( token, moneda );
|
|||
|
|
if (respuesta)
|
|||
|
|
{
|
|||
|
|
CustomNotification ( "Se modific<69> la moneda de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Monedas" );
|
|||
|
|
return RedirectToAction ( "Index" );
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Monedas" );
|
|||
|
|
return RedirectToAction ( "Index" );
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|||
|
|
return View ( "Editar", moneda );
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
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, "Moneda" );
|
|||
|
|
return RedirectToAction ( "Index" );
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<ActionResult> AgregarMoneda ( MonedaViewModel moneda )
|
|||
|
|
{
|
|||
|
|
bool respuesta = false;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var token = Request.Cookies["CookieToken"];
|
|||
|
|
if (token == null)
|
|||
|
|
{
|
|||
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|||
|
|
token = tokenUsuario;
|
|||
|
|
CrearCookies ( tokenUsuario );
|
|||
|
|
}
|
|||
|
|
if (ModelState.IsValid)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
respuesta = await _monedaAPI.AgregarMoneda ( token, moneda );
|
|||
|
|
if (respuesta)
|
|||
|
|
{
|
|||
|
|
CustomNotification ( "Se agreg<65> la moneda de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Monedas" );
|
|||
|
|
return RedirectToAction ( "Index" );
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Monedas" );
|
|||
|
|
return RedirectToAction ( "Index" );
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|||
|
|
return View ( "Agregar", moneda );
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
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, "Moneda" );
|
|||
|
|
return RedirectToAction ( "Index" );
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task EliminarMoneda ( int id )
|
|||
|
|
{
|
|||
|
|
var token = Request.Cookies["CookieToken"];
|
|||
|
|
if (token == null)
|
|||
|
|
{
|
|||
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|||
|
|
token = tokenUsuario;
|
|||
|
|
CrearCookies ( tokenUsuario );
|
|||
|
|
}
|
|||
|
|
await _monedaAPI.EliminarMoneda ( token, id );
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<ActionResult> Editar ( int id )
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
MonedaViewModel moneda = new MonedaViewModel ();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var token = Request.Cookies["CookieToken"];
|
|||
|
|
if (token == null)
|
|||
|
|
{
|
|||
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|||
|
|
token = tokenUsuario;
|
|||
|
|
CrearCookies ( tokenUsuario );
|
|||
|
|
}
|
|||
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|||
|
|
var listaRoles = new List<string> ( rolesCookie.Split ( "," ) );
|
|||
|
|
if (!listaRoles.Exists ( r => r == "ADMINISTRADOR" || r == "OPERATIVO" ))
|
|||
|
|
{
|
|||
|
|
throw new AuthorizeException ( 403, "Usuario no autorizado" );
|
|||
|
|
}
|
|||
|
|
moneda = await _monedaAPI.ObtenerMoneda ( token, id );
|
|||
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( 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, "Moneda" );
|
|||
|
|
return RedirectToAction ( "Index" );
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return View ( moneda );
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<ActionResult> Agregar ()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var token = Request.Cookies["CookieToken"];
|
|||
|
|
if (token == null)
|
|||
|
|
{
|
|||
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|||
|
|
token = tokenUsuario;
|
|||
|
|
CrearCookies ( tokenUsuario );
|
|||
|
|
}
|
|||
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|||
|
|
var listaRoles = new List<string> ( rolesCookie.Split ( "," ) );
|
|||
|
|
if (!listaRoles.Exists ( r => r == "ADMINISTRADOR" || r == "OPERATIVO" ))
|
|||
|
|
{
|
|||
|
|
throw new AuthorizeException ( 403, "Usuario no autorizado" );
|
|||
|
|
}
|
|||
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( 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, "Moneda" );
|
|||
|
|
return RedirectToAction ( "Index" );
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return View ();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|