448 lines
15 KiB
C#
448 lines
15 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
using System.Xml.Linq;
|
|
using VirtualTask.Models;
|
|
using X.PagedList;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
|
|
namespace VirtualTask.Controllers
|
|
{
|
|
public class TecniciController : 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;
|
|
string time_sheet=string.Empty;
|
|
HttpClient client;
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public TecniciController(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("Login2", "Login");
|
|
}
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
time_sheet = helper.GetStringValue("time_sheet");
|
|
ViewBag.TimeSheet = time_sheet;
|
|
urlBase = apiUrl + "tecniciList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
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);
|
|
modelList=modelList.Where(x=>x.tcdatobs==null).ToList();
|
|
|
|
if (!string.IsNullOrEmpty(searchString))
|
|
{
|
|
modelList = modelList.Where(s => s.tcdescri.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.tccodice)
|
|
.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()
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
|
|
return View();
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult Create(Tecnici model)
|
|
{
|
|
|
|
SessionHelper helper = new SessionHelper(this);
|
|
if (ModelState.IsValid)
|
|
{
|
|
token = helper.GetStringValue("tok");
|
|
tenant = helper.GetStringValue("tenant");
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return RedirectToAction("Login2", "Login");
|
|
}
|
|
//step 1 prima controllo che le credenziali di login non siano già presenti.
|
|
//per fare questo chiamo il metodo api tecniciListAll
|
|
Controllo c = isLoginPresente(model.tcuser, model.tcpwd, helper);
|
|
if (c.isPresente)
|
|
{
|
|
ModelState.AddModelError("tcuser", "Login già utilizzata. Scegliere un nome utente differente");
|
|
}
|
|
|
|
if(!c.isPresente)
|
|
{
|
|
model.tccodazi = tenant;
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "tecnici/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("Index");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return View("Create", model);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//foreach (var Elemento in ModelState.Values)
|
|
//{
|
|
// foreach (var Errore in Elemento.Errors)
|
|
// {
|
|
// string ErroreRilevato = Errore.ErrorMessage;
|
|
// }
|
|
//}
|
|
//return RedirectToAction("Create");
|
|
return View("Create", model);
|
|
}
|
|
|
|
}
|
|
|
|
#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 + "tecniciList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
Tecnici tecnico = new Tecnici();
|
|
|
|
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);
|
|
tecnico = modelList.Where(x => x.tccodice.Equals(id)).First();
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
|
|
return View(tecnico);
|
|
}
|
|
|
|
#endregion DETAIL
|
|
|
|
#region EDIT
|
|
|
|
public IActionResult Edit(string id)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
|
|
token = helper.GetStringValue("tok");
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "tecniciList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
Tecnici tecnico = new Tecnici();
|
|
|
|
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);
|
|
tecnico = modelList.Where(x => x.tccodice.Equals(id)).First();
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
|
|
return View(tecnico);
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult Edit(Tecnici tecnico)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
|
|
token = helper.GetStringValue("tok");
|
|
tenant = helper.GetStringValue("tenant");
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
{
|
|
return RedirectToAction("Login2", "Login");
|
|
}
|
|
|
|
//step 1 prima controllo che le credenziali di login non siano già presenti.
|
|
//per fare questo chiamo il metodo api tecniciListAll
|
|
//Controllo c = isLoginPresente(tecnico.tcuser, tecnico.tcpwd, helper);
|
|
//if (c.isPresente)
|
|
//{
|
|
// ModelState.AddModelError("tcuser", "Login già utilizzata. Scegliere un nome utente differente");
|
|
//}
|
|
|
|
//if (!c.isPresente)
|
|
//{
|
|
tecnico.tccodazi = tenant;
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "tecnici/mod";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
string data = JsonConvert.SerializeObject(tecnico);
|
|
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(tecnico);
|
|
//}
|
|
//else
|
|
//{
|
|
// return View(tecnico);
|
|
//}
|
|
}
|
|
|
|
#endregion EDIT
|
|
|
|
#region DELETE
|
|
|
|
[HttpGet]
|
|
public IActionResult Delete(string id)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
|
|
token = helper.GetStringValue("tok");
|
|
tenant = helper.GetStringValue("tenant");
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "tecniciList";
|
|
urlBase = urlBase + "?token=" + token;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
Tecnici tecnico = new Tecnici();
|
|
|
|
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);
|
|
tecnico = modelList.Where(x => x.tccodice.Equals(id)).First();
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
return RedirectToAction("Error");
|
|
}
|
|
|
|
return View(tecnico);
|
|
}
|
|
|
|
[HttpPost, ActionName("DeleteConfirmed")]
|
|
public IActionResult DeleteConfirmed(string id)
|
|
{
|
|
SessionHelper helper = new SessionHelper(this);
|
|
|
|
token = helper.GetStringValue("tok");
|
|
|
|
apiUrl = helper.GetStringValue("apiUrl");
|
|
admin = helper.GetStringValue("admin");
|
|
ViewBag.Admin = admin;
|
|
urlBase = apiUrl + "tecnici/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");
|
|
}
|
|
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 });
|
|
}
|
|
|
|
public Controllo isLoginPresente(string user, string pwd, SessionHelper helper)
|
|
{
|
|
Controllo c=new Controllo();
|
|
c.isPresente = false;
|
|
|
|
urlBase = apiUrl + "tecniciListAll";
|
|
urlBase = urlBase + "?usr=" + user;
|
|
urlBase = urlBase + "&pwd=" + pwd;
|
|
Uri baseAddress = new Uri(urlBase);
|
|
client = new HttpClient();
|
|
client.BaseAddress = baseAddress;
|
|
|
|
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);
|
|
if(modelList!=null && modelList.Count>0)
|
|
c.isPresente=true;
|
|
}
|
|
else
|
|
{
|
|
errMes = response.Content.ReadAsStringAsync().Result;
|
|
helper.SetStringValue("errMsg", errMes);
|
|
c.errMsg = errMes;
|
|
}
|
|
return c;
|
|
}
|
|
|
|
}
|
|
public class Controllo
|
|
{
|
|
/// <summary></summary>
|
|
public string? errMsg { get; set; }
|
|
|
|
/// <summary></summary>
|
|
public bool isPresente { get; set; }
|
|
}
|
|
}
|
|
|
|
|