SoftwayWeb/Controllers/DestinazioniController.cs

175 lines
6.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using SoftwayWeb.Models;
using System.Diagnostics;
using System.Text;
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, string? codMezzo, int? page = 1)
{
helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
string autista = string.Empty;
string dataGi = string.Empty;
string mezzo = string.Empty;
if (!string.IsNullOrEmpty(codAutista))
{
helper.SetStringValue("codAutista", codAutista.TrimEnd());
}
else
{
string aut = helper.GetStringValue("codAutista");
codAutista = aut.TrimEnd();
}
if (!string.IsNullOrEmpty(codMezzo))
{
helper.SetStringValue("codMezzo", codMezzo.TrimEnd());
}
else
{
string mez = helper.GetStringValue("codMezzo").TrimEnd();
codMezzo = mez;
}
if (dataGiro.Date != DateTime.MinValue)
{
helper.SetStringValue("dataGiro", dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"));
dataGi = dataGiro.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
}
else
{
dataGi = helper.GetStringValue("dataGiro");
}
urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzo";
urlBase = urlBase + "?autista=" + codAutista + "&dataGiro=" + dataGi + "&mezzo=" + codMezzo;
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(shortList);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
public IActionResult EliminaDestinazione(string serial)
{
SessionHelper helper = new SessionHelper(this);
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaDestinazioneBySerial";
urlBase = urlBase + "?seriale=" + serial;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Destinazioni_Out model = new Destinazioni_Out();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string dato = response.Content.ReadAsStringAsync().Result;
model = JsonConvert.DeserializeObject<Destinazioni_Out>(dato);
//qui metodo post per scrivere su pimodgir
Modgir mg=new Modgir();
mg.Piserial = model.Brserial;
mg.Pidata = model.DataCarico;
mg.Picommit = model.CodCommittente;
mg.Pidesdiv = model.CodSede;
mg.Pitarga = model.CodAutomezzo;
mg.Piautist = model.CodAutista;
mg.Pitiprec = "D";
urlBase = apiUrl + "ModificaGiro/addModgir";
baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(mg);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response2 = client.PostAsync(baseAddress, content).Result;
if (response2.IsSuccessStatusCode)
{
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return RedirectToAction("Index", "Destinazioni", new
{
id = model.Brserial,
codAutista = model.CodAutista,
dataGiro = model.DataCarico,
codMezzo=model.CodAutomezzo
});
}
[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 });
}
}
}