SoftwayWeb/Controllers/GiriController.cs
2024-06-25 08:58:24 +02:00

66 lines
2.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SoftwayWeb.Models;
using X.PagedList;
namespace SoftwayWeb.Controllers
{
public class GiriController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string errMes = string.Empty;
private readonly IConfiguration _configuration;
HttpClient client;
SessionHelper helper;
public GiriController(IConfiguration configuration)
{
_configuration = configuration;
client = new HttpClient();
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
public IActionResult Index(string? codAutista, DateTime? data, bool aperto = true)
{
helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
string url = apiUrl + "Giri/listaGiri";
urlBase = url + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<GiriConsegnaView> modelList = new List<GiriConsegnaView>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<GiriConsegnaView>>(dato);
if (!string.IsNullOrEmpty(codAutista))
{
modelList = modelList.Where(x => x.CodAutista.Contains(codAutista)).ToList();
ViewData["CurrentFilter"] = codAutista;
}
else
{
ViewData["CurrentFilter"] = null;
}
//var shortList = modelList.ToPagedList();
return View(modelList);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
}
}