VirtualTask/Controllers/Rapp_NewController.cs
2023-11-20 11:18:41 +01:00

404 lines
14 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
using VirtualTask.Models;
using X.PagedList;
namespace VirtualTask.Controllers
{
public class Rapp_NewController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string errMes = string.Empty;
string admin = string.Empty;
HttpClient client;
private readonly IConfiguration _configuration;
public Rapp_NewController(IConfiguration configuration)
{
client = new HttpClient();
_configuration = configuration;
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
#region INDEX
public IActionResult Index(string searchString, int? page = 1)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
return RedirectToAction("Index", "Login");
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "rappnewList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Rapp_New> modelList = new List<Rapp_New>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Rapp_New>>(data);
if (!string.IsNullOrEmpty(searchString))
{
modelList = modelList.Where(s => s.descrizione_intervento.ToUpper().Contains(searchString.ToUpper())).ToList();
ViewData["CurrentFilter"] = searchString;
}
else
ViewData["CurrentFilter"] = null;
if (page != null && page < 1)
{
page = 1;
}
var pageSize = 10;
var shortLinks = modelList
.OrderByDescending(s => s.seriale_rapportino)
.ToPagedList(page ?? 1, pageSize);
return View(shortLinks);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
#endregion INDEX
#region CREATE
// 08/11/2023 TOLTI METODI CREATE/EDIT/DELETE PERCHE' IL RAPPORTINO VIENE TUTTO GESTITO DALL'APP.
//public IActionResult Create()
//{
// ViewBag.Tecnici = GetTecnici();
// ViewBag.Chiusure = GetCodChiusura();
// return View();
//}
//[HttpPost]
//public IActionResult Create(Rapp_New model)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// if (string.IsNullOrEmpty(token))
// return RedirectToAction("Index", "Login");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnew/add";
// urlBase = urlBase + "?token=" + token;
// Uri baseAddress = new Uri(urlBase);
// 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");
// }
// else
// {
// errMes = response.Content.ReadAsStringAsync().Result;
// helper.SetStringValue("errMsg", errMes);
// return RedirectToAction("Error");
// }
// return View();
//}
#endregion CREATE
#region DETAIL
public IActionResult Details(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "rappnewList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Rapp_New rapp = new Rapp_New();
List<Rapp_New> modelList = new List<Rapp_New>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Rapp_New>>(data);
rapp = modelList.Where(x => x.seriale_rapportino.Equals(id)).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(rapp);
}
#endregion DETAIL
#region EDIT
// 08/11/2023 TOLTI METODI CREATE/EDIT/DELETE PERCHE' IL RAPPORTINO VIENE TUTTO GESTITO DALL'APP.
//public IActionResult Edit(string id)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnewList";
// urlBase = urlBase + "?token=" + token;
// Uri baseAddress = new Uri(urlBase);
// client = new HttpClient();
// client.BaseAddress = baseAddress;
// Rapp_New rapp = new Rapp_New();
// List<Rapp_New> modelList = new List<Rapp_New>();
// HttpResponseMessage response = client.GetAsync(baseAddress).Result;
// if (response.IsSuccessStatusCode)
// {
// string data = response.Content.ReadAsStringAsync().Result;
// modelList = JsonConvert.DeserializeObject<List<Rapp_New>>(data);
// rapp = modelList.Where(x => x.seriale_rapportino.Equals(id)).First();
// }
// else
// {
// errMes = response.Content.ReadAsStringAsync().Result;
// helper.SetStringValue("errMsg", errMes);
// return RedirectToAction("Error");
// }
// ViewBag.Chiusure = GetCodChiusura();
// return View(rapp);
//}
//[HttpPost]
//public IActionResult Edit(Rapp_New rapp)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnew/mod";
// urlBase = urlBase + "?token=" + token;
// Uri baseAddress = new Uri(urlBase);
// client = new HttpClient();
// client.BaseAddress = baseAddress;
// string data = JsonConvert.SerializeObject(rapp);
// StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
// HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
// if (response.IsSuccessStatusCode)
// {
// return RedirectToAction("Index");
// }
// else
// {
// errMes = response.Content.ReadAsStringAsync().Result;
// helper.SetStringValue("errMsg", errMes);
// return RedirectToAction("Error");
// }
// return View(rapp);
//}
#endregion EDIT
#region DELETE
// 08/11/2023 TOLTI METODI CREATE/EDIT/DELETE PERCHE' IL RAPPORTINO VIENE TUTTO GESTITO DALL'APP.
//[HttpGet]
//public IActionResult Delete(string id)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnewList";
// urlBase = urlBase + "?token=" + token;
// Uri baseAddress = new Uri(urlBase);
// client = new HttpClient();
// client.BaseAddress = baseAddress;
// Rapp_New rapp = new Rapp_New();
// List<Rapp_New> modelList = new List<Rapp_New>();
// HttpResponseMessage response = client.GetAsync(baseAddress).Result;
// if (response.IsSuccessStatusCode)
// {
// string data = response.Content.ReadAsStringAsync().Result;
// modelList = JsonConvert.DeserializeObject<List<Rapp_New>>(data);
// rapp = modelList.Where(x => x.seriale_rapportino.Equals(id)).First();
// }
// else
// {
// errMes = response.Content.ReadAsStringAsync().Result;
// helper.SetStringValue("errMsg", errMes);
// return RedirectToAction("Error");
// }
// return View(rapp);
//}
//[HttpPost, ActionName("DeleteConfirmed")]
//public IActionResult DeleteConfirmed(string id)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnew/del?" + "serialeRapportino=" + id + "&";
// urlBase = urlBase + "token=" + token;
// Uri baseAddress = new Uri(urlBase);
// client = new HttpClient();
// client.BaseAddress = baseAddress;
// string data = JsonConvert.SerializeObject(id);
// StringContent content = new StringContent(data, Encoding.UTF8, "application/json");
// HttpResponseMessage response = client.PostAsync(baseAddress, content).Result;
// if (response.IsSuccessStatusCode)
// {
// return RedirectToAction("Index");
// }
// else
// {
// errMes = response.Content.ReadAsStringAsync().Result;
// helper.SetStringValue("errMsg", errMes);
// return RedirectToAction("Error");
// }
// return View();
//}
#endregion DELETE
//metodo per avere riempire combobox con la tabella delle chiusure
private List<SelectListItem> GetTecnici()
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "tecniciList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<SelectListItem> selectItems = new List<SelectListItem>();
List<Tecnici> modelList = new List<Tecnici>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Tecnici>>(data);
foreach (var role in modelList)
{
SelectListItem listItem = new SelectListItem();
listItem.Value = role.tccodice;
listItem.Text = role.tcdescri;
selectItems.Add(listItem);
}
}
return selectItems;
}
private List<SelectListItem> GetCodChiusura()
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiusureVtList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<SelectListItem> selectItems = new List<SelectListItem>();
List<Chiusure> modelList = new List<Chiusure>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiusure>>(data);
foreach (var role in modelList)
{
SelectListItem listItem = new SelectListItem();
if (role == modelList.First())
{
listItem.Value = string.Empty;
listItem.Text = "";
selectItems.Add(listItem);
}
else
{
listItem.Value = role.cccodice;
listItem.Text = role.ccdescr;
selectItems.Add(listItem);
}
}
}
return selectItems;
}
[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 });
}
}
}