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 modelList = new List(); SessionHelper helper = new SessionHelper(this); 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>(result); } string _filtro= helper.GetStringValue("filterData"); if (data == DateTime.MinValue && string.IsNullOrEmpty(_filtro)) { 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)) { DateTime dataFiltro=Convert.ToDateTime(_filtro); ViewBag.Filtro = Convert.ToString(_filtro); modelList = modelList.Where(x => x.DataGiro == dataFiltro).ToList(); } if (data != DateTime.MinValue ) { 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 lst) { SessionHelper helper = new SessionHelper(this); 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; 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 = response.Content.ReadAsStringAsync().Result; 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 }); } } }