239 lines
8.8 KiB
C#
239 lines
8.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Newtonsoft.Json;
|
|
using SoftwayWeb.Models;
|
|
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, DateTime data, bool aperto = true, int? page = 1)
|
|
{
|
|
helper = new SessionHelper(this);
|
|
token = helper.GetStringValue("tok");
|
|
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<GiriConsegnaView> modelList = new List<GiriConsegnaView>();
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string dato = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<GiriConsegnaView>>(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;
|
|
}
|
|
|
|
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 = 1/*10*/;
|
|
|
|
var shortList = modelList.ToPagedList(page ?? 1, pageSize);
|
|
return View(shortList/*modelList*/);
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public IActionResult Create(bool sel)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
List<GiriConsegnaDaCreare> modelList = new List<GiriConsegnaDaCreare>();
|
|
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<List<GiriConsegnaDaCreare>>(data);
|
|
}
|
|
|
|
foreach(GiriConsegnaDaCreare giro in modelList)
|
|
{
|
|
giro.IsSelected= sel;
|
|
}
|
|
|
|
return View(modelList);
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult PostIndex(IList<GiriConsegnaDaCreare> lst)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
foreach(GiriConsegnaDaCreare g in lst)
|
|
{
|
|
if(g.IsSelected==true)
|
|
{
|
|
//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(g);
|
|
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");
|
|
}
|
|
[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 });
|
|
}
|
|
|
|
private List<SelectListItem> getAutisti()
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
urlBase = apiUrl + "Giri/listaAutisti";
|
|
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
List<SelectListItem> selectItems = new List<SelectListItem>();
|
|
List<Personale> modelList = new List<Personale>();
|
|
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string data = response.Content.ReadAsStringAsync().Result;
|
|
modelList = JsonConvert.DeserializeObject<List<Personale>>(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<GiriConsegnaDaCreare> getNuoviGiri()
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
List<GiriConsegnaDaCreare> modelList = new List<GiriConsegnaDaCreare> ();
|
|
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<List<GiriConsegnaDaCreare>>(data);
|
|
}
|
|
return modelList;
|
|
}
|
|
}
|
|
}
|