using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System.Diagnostics; 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; HttpClient client; public RegistrazioniController() { client = new HttpClient(); } #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"); //urlBase = apiUrl + "RegistrazioniList"; 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; List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(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); //token = helper.GetStringValue("token"); //tenant = helper.GetStringValue("tenant"); //if (string.IsNullOrEmpty(token)) //{ // return RedirectToAction("Index", "Login"); //} //model.azienda = tenant; //apiUrl = helper.GetStringValue("apiUrl"); //urlBase = apiUrl + "registrazioni/add"; urlBase = "http://10.0.0.187:8000/api/Polo/registrazioni/add"; //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 CREATE #region DETAILS public IActionResult Details(int id) { SessionHelper helper = new SessionHelper(this); //token = helper.GetStringValue("tok"); //urlBase = apiUrl + "registrazioniList"; 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 modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); //reg = modelList.Where(x => x.id.Equals(id)).First(); 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 modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(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 modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(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 }); } } }