VirtualTask/Controllers/RegistrazioniController.cs

384 lines
13 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using VirtualTask.Models;
using X.PagedList;
namespace VirtualTask.Controllers
{
public class RegistrazioniController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string tenant = string.Empty;
string errMes = string.Empty;
string admin = string.Empty;
HttpClient client;
private readonly IConfiguration _configuration;
public RegistrazioniController(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);
urlBase = apiUrl + "RegistrazioniList";
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Registrazione> modelList = new List<Registrazione>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Registrazione>>(data);
if (!string.IsNullOrEmpty(searchString))
{
modelList = modelList.Where(s => s.cognome.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.id)
.ToPagedList(page ?? 1, pageSize);
return View(shortLinks);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
#endregion INDEX
#region CREATE
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Registrazione model)
{
SessionHelper helper = new SessionHelper(this);
if (ModelState.IsValid)
{
bool bAziPres = false;
bAziPres = checkAziendaPresente(model.azienda);
if (bAziPres)
{
ModelState.AddModelError("azienda", "Azienda presente in archivio. Inserire un valore diverso.");
}
bool bEmail = model.email.Equals(model.emailConf);
if (!bEmail)
{
ModelState.AddModelError("email", "I campi Email e Conferma Email devono essere uguali");
}
if (!bAziPres && bEmail)
{
urlBase = apiUrl + "registrazioni/add";
//urlBase = "http://10.0.0.187:8000/api/Polo/registrazioni/add";
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("RegistrazioneOk");
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
else
{
return View("Create", model);
}
}
else
{
foreach (var Elemento in ModelState.Values)
{
foreach (var Errore in Elemento.Errors)
{
string ErroreRilevato = Errore.ErrorMessage;
}
}
return View("Create", model);
}
}
public IActionResult RegistrazioneOk()
{
return View();
}
#endregion CREATE
#region DETAILS
public IActionResult Details(int id)
{
SessionHelper helper = new SessionHelper(this);
urlBase = apiUrl + "registrazioniList";
//urlBase = "http://10.0.0.187:8000/api/Polo/RegistrazioniList";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Registrazione reg = new Registrazione();
List<Registrazione> modelList = new List<Registrazione>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Registrazione>>(data);
reg = modelList.Where(x => x.id == id).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(reg);
}
#endregion DETAILS
#region EDIT
public IActionResult Edit(int id)
{
SessionHelper helper = new SessionHelper(this);
//token = helper.GetStringValue("tok");
//apiUrl = helper.GetStringValue("apiUrl");
//urlBase = apiUrl + "chiusureVtList";
urlBase = "http://10.0.0.187:8000/api/Polo/RegistrazioniList";
//urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Registrazione reg = new Registrazione();
List<Registrazione> modelList = new List<Registrazione>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Registrazione>>(data);
reg = modelList.Where(x => x.id == id).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(reg);
}
[HttpPost]
public IActionResult Edit(Registrazione model)
{
SessionHelper helper = new SessionHelper(this);
//token = helper.GetStringValue("tok");
//tenant = helper.GetStringValue("tenant");
//if (string.IsNullOrEmpty(token))
//{
// return RedirectToAction("Index", "Login");
//}
model.azienda = tenant;
//apiUrl = helper.GetStringValue("apiUrl");
//urlBase = apiUrl + "registrazioni/mod";
urlBase = apiUrl + "http://10.0.0.187:8000/api/Polo/registrazioni/mod";
//urlBase = urlBase + "?token=" + token;
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");
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
#endregion EDIT
#region DELETE
[HttpGet]
public IActionResult Delete(int id)
{
SessionHelper helper = new SessionHelper(this);
//token = helper.GetStringValue("tok");
//apiUrl = helper.GetStringValue("apiUrl");
//urlBase = apiUrl + "chiusureVtList";
urlBase = "http://10.0.0.187:8000/api/Polo/RegistrazioniList";
//urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Registrazione reg = new Registrazione();
List<Registrazione> modelList = new List<Registrazione>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Registrazione>>(data);
reg = modelList.Where(x => x.id.Equals(id)).First();
return View(reg);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
[HttpPost, ActionName("DeleteConfirmed")]
public IActionResult DeleteConfirmed(int id)
{
SessionHelper helper = new SessionHelper(this);
//token = helper.GetStringValue("tok");
//apiUrl = helper.GetStringValue("apiUrl");
urlBase = /*apiUrl + */"http://10.0.0.187:8000/api/Polo/Registrazioni/del?" + "id=" + 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");
}
}
#endregion DELETE
[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 bool checkAziendaPresente(string azienda)
{
bool trovato = false;
bool bAziPres = false;
//urlBase = "http://10.0.0.187:8000/api/Polo/AziendePresentiList";
urlBase = apiUrl + "AziendePresentiList";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<AziendaPres> modelList = new List<AziendaPres>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<AziendaPres>>(data);
foreach(AziendaPres a in modelList)
{
if(!string.IsNullOrEmpty(a.tccodazi) && a.tccodazi.Trim().Equals(azienda))
trovato = true;
}
bAziPres = trovato;
}
return bAziPres;
}
}
}