SoftwayWeb/Controllers/DestinazioniController.cs
2024-07-08 17:59:15 +02:00

69 lines
2.6 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using SoftwayWeb.Models;
using X.PagedList;
namespace SoftwayWeb.Controllers
{
public class DestinazioniController : 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 DestinazioniController(IConfiguration configuration)
{
_configuration = configuration;
client = new HttpClient();
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
public IActionResult Index(string id, string? codAutista, DateTime dataGiro, /*Destinazioni_Out giro,*/ string? codMezzo, int? page = 1)
{
helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzo";
//dataGiro = dataGiro.ToUniversalTime();
//urlBase = urlBase + "autista=" + giro.CodAutista + "datGiro=" /*+ giro.DataGiro*/;
urlBase = urlBase + "?autista=" + codAutista.Trim() + "&dataGiro=2023-05-30T00%3A00%3A00.000Z" + "&mezzo=" + codMezzo /*+ dataGiro*/;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Destinazioni_Out> modelList = new List<Destinazioni_Out>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Destinazioni_Out>>(dato);
modelList = modelList.Where(x => x.serialeGiro == id).ToList();
//if (page != null && page < 1)
//{
// page = 1;
//}
//var pageSize = 10;
var shortList = modelList.ToPagedList(/*page ?? 1, pageSize*/);
return View(/*modelList*/shortList);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
//var shortList = modelList.ToPagedList();
//return View(shortList);
}
}
}