diff --git a/Controllers/TecniciController.cs b/Controllers/TecniciController.cs index 8bf3d4e..96f727b 100644 --- a/Controllers/TecniciController.cs +++ b/Controllers/TecniciController.cs @@ -1,7 +1,9 @@ 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; @@ -104,39 +106,71 @@ namespace VirtualTask.Controllers [HttpPost] public IActionResult Create(Tecnici model) { + SessionHelper helper = new SessionHelper(this); - token = helper.GetStringValue("tok"); - tenant = helper.GetStringValue("tenant"); + if (ModelState.IsValid) + { + token = helper.GetStringValue("tok"); + tenant = helper.GetStringValue("tenant"); - if (string.IsNullOrEmpty(token)) - { - return RedirectToAction("Login2", "Login"); - } - model.tccodazi = 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"); + } - 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; + if(!c.isPresente) + { + model.tccodazi = tenant; - string data = JsonConvert.SerializeObject(model); - StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); - HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; + 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; - if (response.IsSuccessStatusCode) - { - return RedirectToAction("Index"); + 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 { - errMes = response.Content.ReadAsStringAsync().Result; - helper.SetStringValue("ErrMsg", errMes); - return RedirectToAction("Index"); + //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 @@ -347,5 +381,47 @@ namespace VirtualTask.Controllers 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 modelList = new List(); + + HttpResponseMessage response = client.GetAsync(baseAddress).Result; + + if (response.IsSuccessStatusCode) + { + string data = response.Content.ReadAsStringAsync().Result; + modelList = JsonConvert.DeserializeObject>(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 + { + /// + public string? errMsg { get; set; } + + /// + public bool isPresente { get; set; } } } + + diff --git a/Views/Anag/Delete.cshtml b/Views/Anag/Delete.cshtml index 5afc7d3..2007a2e 100644 --- a/Views/Anag/Delete.cshtml +++ b/Views/Anag/Delete.cshtml @@ -74,6 +74,8 @@
model.ancodice) name="id" /> + + model.ancodice) name="ancodice" />
Torna alla lista
diff --git a/appsettings.json b/appsettings.json index 4460f4a..41d46d1 100644 --- a/appsettings.json +++ b/appsettings.json @@ -6,8 +6,8 @@ } }, "ApplicationInsights": { - //"rootUrlApi": "https://api.poloinformatico.it:9000/api/Polo/", - "rootUrlApi": "http://testapi.poloinformatico.it:9001/api/Polo/", + "rootUrlApi": "https://api.poloinformatico.it:9000/api/Polo/", + //"rootUrlApi": "http://testapi.poloinformatico.it:9001/api/Polo/", "mittenteMail": "audiffredi@poloinformatico.it", "nomeMail": "Supporto Virtual Task", "pwdMail": "audicruz70", diff --git a/wwwroot/assets/images/Bild1.png b/wwwroot/assets/images/Bild1.png index 2b3e83b..7a78cd6 100644 Binary files a/wwwroot/assets/images/Bild1.png and b/wwwroot/assets/images/Bild1.png differ