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 Index () { List monedas = new List (); 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 ( 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 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ó 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 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ó 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 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 ( 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 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 ( 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 (); } } }