using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Newtonsoft.Json; using SoftwayWeb.Models; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Runtime.Intrinsics.Arm; using System.Text; 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, string? codMezzo, DateTime data, bool aperto = true, int? page = 1) { helper = new SessionHelper(this); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) { return RedirectToAction("Login", "Login"); } urlBase = apiUrl + "Giri/listaGiri?aperto="+ aperto; //string url = apiUrl + "Giri/listaGiri"; //urlBase = url + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string dato = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(dato); ViewBag.CodAutista = getAutisti(); if (!string.IsNullOrEmpty(codAutista)) { modelList = modelList.Where(x => x.CodAutista.Contains(codAutista)).ToList(); ViewData["CurrentFilter"] = codAutista; ViewBag.Autista = codAutista; } else { ViewData["CurrentFilter"] = null; } ViewBag.CodMezzo = getMezzi(); if (!string.IsNullOrEmpty(codMezzo)) { modelList = modelList.Where(x => x.CodMezzo.Contains(codMezzo)).ToList(); ViewData["CurrentFilter"] = codMezzo; } else { ViewData["CurrentFilter"] = null; } if (data.Date != DateTime.MinValue) { modelList = modelList.Where(x => x.DataGiro.GetValueOrDefault().Date == data.Date).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 Create(bool sel) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) { return RedirectToAction("Login", "Login"); } List modelList = new List(); 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 data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); } 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"); } public IActionResult Bancali(string id, bool aperto = true) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) { return RedirectToAction("Login", "Login"); } apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "Giri/listaGiri?aperto=" + aperto; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; HttpResponseMessage response = client.GetAsync(baseAddress).Result; List modelList = new List(); GiriConsegnaView model = new GiriConsegnaView(); if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); model = modelList.Where(x => x.SerialeGiro.Equals(id)).First(); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } return View(model); } [HttpPost] public IActionResult Bancali(GiriConsegnaView model) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) { return RedirectToAction("Login", "Login"); } apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "Giri/modGiro"; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; string data = JsonConvert.SerializeObject(model); StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; if (response.IsSuccessStatusCode) { return RedirectToAction("Index", "Giri"); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } [HttpGet] public IActionResult ModificaTutteDestinazioni(string id, string? codAutista, string? nomeAutista, string? codMezzo, string? nomeMezzo, DateTime dataGiroCons, int? page = 1) { helper = new SessionHelper(this); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) { return RedirectToAction("Login", "Login"); } ViewBag.CodAutista = codAutista; ViewBag.Autista = nomeAutista; ViewBag.CodMezzo = codMezzo; ViewBag.Mezzo = nomeMezzo; ViewBag.DataGiroCons = dataGiroCons; string dataGiroString = dataGiroCons.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"); urlBase = apiUrl + "Giri/listaDestinazioniByAutistaDataMezzo"; urlBase = urlBase + "?autista=" + codAutista + "&dataGiro=" + dataGiroString + "&mezzo=" + codMezzo; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; List modelList = new List(); List autisti = new List(); autisti = getAutisti(); ViewBag.Autisti = autisti; List mezzi = new List(); mezzi = getMezzi(); ViewBag.Mezzi = mezzi; HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string dato = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(dato); modelList = modelList.Where(x => x.serialeGiro.Equals(id.TrimEnd())).ToList(); //if (page != null && page < 1) //{ // page = 1; //} //var pageSize = 10; //var shortList = modelList.ToPagedList(/*page ?? 1, pageSize*/); return View(modelList); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } //return View(modelList); } [HttpPost] public IActionResult ModificaTutteDestinazioniPost(List modelList, string? autisti, /*string? nomeAutista,*/ string? mezzi) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); if (string.IsNullOrEmpty(token)) { return RedirectToAction("Login", "Login"); } foreach (var destinazione in modelList) { Modgir mg = new Modgir(); mg.Piserial = destinazione.Brserial; mg.Pidata = destinazione.DataCarico; mg.Picommit = destinazione.CodCommittente; mg.Pidesdiv = destinazione.CodSede; mg.Pitarga = mezzi.TrimEnd(); mg.Piautist = autisti.TrimEnd(); mg.Pitiprec = "M"; mg.Pisergir = destinazione.Pisergir; apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "ModificaGiro/addModgir"; Uri 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) //{ // //Destinazioni_Out destMod = new Destinazioni_Out(); // //destMod.serialeGiro = destinazione.Brserial; // //destMod.CodAutista = destinazione.CodAutista; // //destMod.CodAutomezzo = destinazione.CodAutomezzo; // //new // //{ // // id = destinazione.serialeGiro, // // codAutista = destinazione.CodAutista, // // dataGiro = destinazione.DataCarico, // // codMezzo = destinazione.CodAutomezzo // //},; //} } return RedirectToAction("Index", "Giri"); } //[HttpPost] //public IActionResult Create(GiriConsegnaView model) //{ // SessionHelper helper = new SessionHelper(this); // ViewBag.Autisti = getAutisti(); // apiUrl = helper.GetStringValue("apiUrl"); // urlBase = apiUrl + "Giri/addGiro2"; // Uri baseAddress = new Uri(urlBase); // client = new HttpClient(); // client.BaseAddress = baseAddress; // string data = JsonConvert.SerializeObject(model); // StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); // HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; // if (response.IsSuccessStatusCode) // { // return RedirectToAction("Index", "Giri"); // } // else // { // errMes = response.Content.ReadAsStringAsync().Result; // helper.SetStringValue("errMsg", errMes); // return RedirectToAction("Error"); // } //} [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 }); } #region METODI PRIVATI private List getAutisti() { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "Giri/listaAutisti"; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; List selectItems = new List(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); //per gestire primo elemento tendina (deve essere vuoto) SelectListItem listItemFirt = new SelectListItem(); listItemFirt.Value = string.Empty; listItemFirt.Text = " - Autista"; selectItems.Add(listItemFirt); foreach (var role in modelList) { SelectListItem listItem = new SelectListItem(); string s = role.Catcodice + " - " + role.Catnome; listItem.Value = role.Catcodice; listItem.Text = s; selectItems.Add(listItem); } } return selectItems; } private List getMezzi() { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "Giri/listaAutomezzi"; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; List selectItems = new List(); List listMezzi = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; listMezzi = JsonConvert.DeserializeObject>(data); listMezzi = listMezzi.OrderBy(x => x.CauTarga).ToList(); //per gestire primo elemento tendina (deve essere vuoto) SelectListItem listItemFirt = new SelectListItem(); listItemFirt.Value = string.Empty; listItemFirt.Text = " - Automezzo"; selectItems.Add(listItemFirt); foreach (var mezzo in listMezzi) { SelectListItem listItem = new SelectListItem(); List listItemOrderby = new List(); string s = mezzo.CauTarga + " - " + mezzo.CauDesc; listItem.Value = mezzo.CauTarga; listItem.Text = s; selectItems.Add(listItem); } } return selectItems; } private List getNuoviGiri() { SessionHelper helper = new SessionHelper(this); List modelList = new List (); 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 data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); } return modelList; } #endregion METODI PRIVATI } }