VirtualTask/Controllers/ChiamateController.cs
2023-10-02 09:24:10 +02:00

71 lines
2.0 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using VirtualTask.Models;
using X.PagedList;
namespace VirtualTask.Controllers
{
public class ChiamateController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
HttpClient client;
public ChiamateController()
{
client = new HttpClient();
}
#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("Index", "Login");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiamateList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Chiamate> modelList = new List<Chiamate>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiamate>>(data);
}
if (!string.IsNullOrEmpty(searchString))
{
modelList = modelList.Where(s => s.chcodimp.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.chcodimp)
.ToPagedList(page ?? 1, pageSize);
return View(shortLinks);
}
#endregion INDEX
}
}