VirtualTask/Controllers/ChiamateController.cs

260 lines
8.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Text;
using VirtualTask.Models;
using X.PagedList;
namespace VirtualTask.Controllers
{
public class ChiamateController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string tenant = string.Empty;
HttpClient client;
public ChiamateController()
{
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 + "chiamate";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Chiamate> modelList = new List<Chiamate>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiamate>>(data);
}
if (!string.IsNullOrEmpty(searchString))
{
modelList = modelList.Where(s => s.chcodimp.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.chcodimp)
.ToPagedList(page ?? 1, pageSize);
return View(shortLinks);
}
#endregion INDEX
#region CREATE
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Chiamate model)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
tenant = helper.GetStringValue("tenant");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Index","Login");
}
model.chcodazi = tenant;
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiamata/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");
}
return View();
}
#endregion CREATE
#region DETAIL
public IActionResult Details(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiamate";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Chiamate chiamata = new Chiamate();
List<Chiamate> modelList = new List<Chiamate>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiamate>>(data);
chiamata = modelList.Where(x => x.chserial.Equals(id)).First();
}
return View(chiamata);
}
#endregion DETAIL
#region EDIT
public IActionResult Edit(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiamate";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Chiamate chiamata = new Chiamate();
List<Chiamate> modelList = new List<Chiamate>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiamate>>(data);
chiamata = modelList.Where(t => t.chserial.Equals(id)).First();
}
return View(chiamata);
}
[HttpPost]
public IActionResult Edit(Chiamate model)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
tenant = helper.GetStringValue("tenant");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Index", "Login");
}
model.chcodazi = tenant;
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiamata/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");
}
return View(model);
}
#endregion EDIT
#region DELETE
[HttpGet]
public IActionResult Delete(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiamate";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Chiamate chiamata = new Chiamate();
List<Chiamate> modelList = new List<Chiamate>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiamate>>(data);
chiamata = modelList.Where(x => x.chserial.Equals(id)).First();
}
return View(chiamata);
}
[HttpPost, ActionName("DeleteConfirmed")]
public IActionResult DeleteConfirmed(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiamata/del?" + "codice=" + 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");
}
return View();
}
#endregion DELETE
}
}