SoftwayWeb/Controllers/GiriDaValidareController.cs
2025-04-02 15:48:42 +02:00

154 lines
5.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using NuGet.Common;
using SoftwayWeb.Models;
using System.Diagnostics;
using System.Text;
namespace SoftwayWeb.Controllers
{
public class GiriDaValidareController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string errMes = string.Empty;
private readonly IConfiguration _configuration;
HttpClient client;
public GiriDaValidareController(IConfiguration configuration)
{
client = new HttpClient();
_configuration = configuration;
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
public IActionResult Index(DateTime data, bool sel,bool clear)
{
//se la data non è valorizzata, prendo i giri inseriti per ultimi, altrimenti li cerco per data
List<GiriConsegnaDaCreare> modelList = new List<GiriConsegnaDaCreare>();
SessionHelper helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
if (clear)
{
helper.SetStringValue("filterData", string.Empty);
}
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/listaGiriDaCreare";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string result = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<GiriConsegnaDaCreare>>(result);
}
else
{
errMes = helper.getErrMsgFromResponse(response);
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
string _filtro= helper.GetStringValue("filterData");
if (data == DateTime.MinValue && string.IsNullOrEmpty(_filtro) && modelList.Count()>0)
{
var last = modelList.OrderByDescending(t => t.DataGiro).Take(1).First();
modelList = modelList.Where(x => x.DataGiro == last.DataGiro).ToList();
}
if (data == DateTime.MinValue && !string.IsNullOrEmpty(_filtro) && modelList.Count() > 0)
{
DateTime dataFiltro=Convert.ToDateTime(_filtro);
ViewBag.Filtro = Convert.ToString(_filtro);
modelList = modelList.Where(x => x.DataGiro == dataFiltro).ToList();
}
if (data != DateTime.MinValue && modelList.Count() > 0)
{
helper.SetStringValue("filterData", Convert.ToString( data));
ViewBag.Filtro = data.ToString("dd/MM/yyyy");
modelList = modelList.Where(x => x.DataGiro==data).ToList();
}
foreach (GiriConsegnaDaCreare giro in modelList)
{
giro.IsSelected = sel;
}
return View(modelList);
}
[HttpPost]
public IActionResult PostIndex(IList<GiriConsegnaDaCreare> lst, string valida, string valida0)
{
SessionHelper helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
foreach (GiriConsegnaDaCreare g in lst)
{
if (g.IsSelected == true)
{
//ViewBag.Autisti = getAutisti();
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/addGiro2";
GiriConsegnaView gcv = new GiriConsegnaView();
gcv.CodAutista = g.CodAutista;
gcv.DataGiro = g.DataGiro;
gcv.CodMezzo = g.CodMezzo;
gcv.DataChiusura = DateTime.MinValue;
if(!string.IsNullOrEmpty(valida0))
{
gcv.BancaliCaricati = 0;
}
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
string data = JsonConvert.SerializeObject(gcv);
StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
if (response.IsSuccessStatusCode)
{
}
else
{
errMes = helper.getErrMsgFromResponse(response);
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
}
return RedirectToAction("Index", "Giri");
}
[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 });
}
}
}