VirtualTask/Controllers/ProgressiviController.cs

177 lines
5.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
using VirtualTask.Models;
using X.PagedList;
namespace VirtualTask.Controllers
{
public class ProgressiviController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string tenant = string.Empty;
string errMes = string.Empty;
string admin = string.Empty;
HttpClient client;
private readonly IConfiguration _configuration;
public ProgressiviController(IConfiguration configuration)
{
client = new HttpClient();
_configuration = configuration;
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
#region INDEX
public IActionResult Index(string searchString, int? page = 1)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login2", "Login");
}
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "progressiviList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Progressivo> modelList = new List<Progressivo>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Progressivo>>(data);
if (!string.IsNullOrEmpty(searchString))
{
modelList = modelList.Where(s => s.tipo_prog.ToUpper().Contains(searchString.ToUpper())).ToList();
ViewData["CurrentFilter"] = searchString;
}
else
ViewData["CurrentFilter"] = null;
if (page != null && page < 1)
{
page = 1;
}
var pageSize = 10;
var shortLinks = modelList
.OrderByDescending(s => s.tipo_prog)
.ToPagedList(page ?? 1, pageSize);
return View(shortLinks);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
#endregion INDEX
#region EDIT
public IActionResult Edit(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "progressiviList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Progressivo prog = new Progressivo();
List<Progressivo> modelList = new List<Progressivo>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Progressivo>>(data);
prog = modelList.Where(x => x.tipo_prog.Equals(id)).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(prog);
}
[HttpPost]
public IActionResult Edit(Progressivo prog)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
tenant = helper.GetStringValue("tenant");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Index", "Login");
}
prog.azienda = tenant;
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "progressivo/mod";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(prog);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
return View(prog);
}
#endregion EDIT
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
SessionHelper helper = new SessionHelper(this);
string e = helper.GetStringValue("errMsg");
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrMsg = e });
}
}
}