From 991ea9ba5e30d5c4a612e94d8e44e945cdce8e1b Mon Sep 17 00:00:00 2001 From: michele Date: Tue, 14 Oct 2025 12:31:18 +0200 Subject: [PATCH] riparazione --- Controllers/ChiamateController.cs | 6 + Controllers/DatiAziendaController.cs | 285 +++++++++++++-------------- Views/Chiamate/Create.cshtml | 8 +- Views/DatiAzienda/Create.cshtml | 88 ++++++++- Views/DatiAzienda/Edit.cshtml | 94 +++++++-- appsettings.json | 10 +- 6 files changed, 302 insertions(+), 189 deletions(-) diff --git a/Controllers/ChiamateController.cs b/Controllers/ChiamateController.cs index 0b52816..73d3a45 100644 --- a/Controllers/ChiamateController.cs +++ b/Controllers/ChiamateController.cs @@ -245,6 +245,12 @@ namespace VirtualTask.Controllers //model.chdtapp = model.chdata; model.chdtass = model.chdata; model.chtipo = "A";//X=creato da app, A creato da adhoc. DEVO METTERE A perche altrimenti l'app lo tratta come una chiamata da commessa + + if (model.chtchiam != null) + { + model.chstato = "C"; + } + model.chmodrac = "EMAIL"; //int year=adesso.Year; //int ora = adesso.Hour; diff --git a/Controllers/DatiAziendaController.cs b/Controllers/DatiAziendaController.cs index 95277a6..b63c17e 100644 --- a/Controllers/DatiAziendaController.cs +++ b/Controllers/DatiAziendaController.cs @@ -91,101 +91,92 @@ namespace VirtualTask.Controllers } [HttpPost] - public IActionResult Create(DatiAzienda model) + public async Task Create(DatiAzienda model) { - DatiAziendaTable dat=new DatiAziendaTable(); - SessionHelper helper = new SessionHelper(this); - admin = helper.GetStringValue("admin"); - token = helper.GetStringValue("tok"); - tenant = helper.GetStringValue("tenant"); - tenant2 = helper.GetStringValue("tenant2"); + string token = helper.GetStringValue("tok"); + string tenant2 = helper.GetStringValue("tenant2"); // tenant = azienda + string apiUrl = helper.GetStringValue("apiUrl"); + string admin = helper.GetStringValue("admin"); + ViewBag.Admin = admin; - if (model.logo != null) + ViewBag.AllTecnici = getTecnici(); + + // ❌ Validazione fallita → restituisco errori in JSON + if (!ModelState.IsValid) { - string pic = System.IO.Path.GetFileName(model.logo.FileName); - //2025-05-05: gestione directory iommagine nel caso che l'azienda sia più corta di 5 caratteri - string dir = tenant2; - if (!string.IsNullOrEmpty(tenant2) && tenant2.Trim().Length < 5) - { - dir = tenant2.Trim(); - } - string path = string.Format("{0}{1}\\{2}",_pathLoghi, dir, pic); - //string projectRootPath = _hostingEnvironment.ContentRootPath; - - //// file is uploaded - using (Stream fileStream = new FileStream(path, FileMode.Create)) - { - model.logo.CopyToAsync(fileStream); - } - - //// save the image path path to the database or you can send image - //// directly to database - //// in-case if you want to store byte[] ie. for DB - using (MemoryStream ms = new MemoryStream()) - { - model.logo.CopyTo(ms); - byte[] array = ms.GetBuffer(); - dat.logo = array; - } - dat.azienda = tenant2; - dat.testo_buono = model.testo_buono; - dat.url_logo = string.Format("{0}{1}/{2}", _urlLoghi, dir, pic); - dat.ragsoc = model.ragsoc; - dat.tecnico = model.tecnico; + var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList(); + return BadRequest(string.Join("\n", errors)); } - - - apiUrl = helper.GetStringValue("apiUrl"); - admin = helper.GetStringValue("admin"); - ViewBag.Admin = admin; - urlBase = apiUrl + "datiazienda/add"; - urlBase = urlBase + "?token=" + token; - Uri baseAddress = new Uri(urlBase); - client = new HttpClient(); - client.BaseAddress = baseAddress; + string logoUrl = null; - string data = JsonConvert.SerializeObject(dat); - StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); - HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; - - if (response.IsSuccessStatusCode) + // 1️⃣ Upload del logo tramite API UploadLogo + if (model.logo != null && model.logo.Length > 0) { - //prima di andare alla pagina devo chiamare il metodo per - //salvare il file in locale in modo che sia visibile sul web dall'app - //C:\ZAPIPOLO\loghi - //https://api.poloinformatico.it:9000/api/Polo/datiazienda/saveFile?azienda=AZI02&tecnico=aaaaa%20%20%20%20%20%20%20%20%20%20&pathSrv=C%3A%5CZAPIPOLO%5Cloghi' - apiUrl = helper.GetStringValue("apiUrl"); - urlBase = apiUrl + "datiazienda/savefile"; - urlBase = urlBase + "?azienda=" + model.azienda; - urlBase = urlBase + "&tecnico=" + model.tecnico; - //urlBase = urlBase + "&pathSrv=" + "C:\\ZAPIPOLO\\loghi"; - urlBase = urlBase + "&pathSrv=" + _pathLoghi; - - baseAddress = new Uri(urlBase); - client = new HttpClient(); - client.BaseAddress = baseAddress; - HttpResponseMessage response2 = client.GetAsync(baseAddress).Result; - if (response2.IsSuccessStatusCode) + string uploadUrl = $"{apiUrl}datiazienda/upload_logo?token={token}"; + + using (var httpClient = new HttpClient()) + using (var form = new MultipartFormDataContent()) { - return RedirectToAction("Index"); - } - else - { - errMes = response.Content.ReadAsStringAsync().Result; - helper.SetStringValue("errMsg", errMes); - return RedirectToAction("Error"); + var fileContent = new StreamContent(model.logo.OpenReadStream()); + fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue( + string.IsNullOrWhiteSpace(model.logo.ContentType) ? "application/octet-stream" : model.logo.ContentType + ); + form.Add(fileContent, "file", model.logo.FileName); + + HttpResponseMessage response = await httpClient.PostAsync(uploadUrl, form); + + if (!response.IsSuccessStatusCode) + { + string err = await response.Content.ReadAsStringAsync(); + return BadRequest("Errore upload logo: " + err); + } + + dynamic result = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + logoUrl = result.url; } } - else + + // 2️⃣ Creo l'oggetto da inviare all'API + var dat = new DatiAziendaTable { - errMes = response.Content.ReadAsStringAsync().Result; - helper.SetStringValue("errMsg", errMes); - return RedirectToAction("Error"); + azienda = tenant2, + tecnico = model.tecnico, + ragsoc = model.ragsoc, + testo_buono = model.testo_buono, + url_logo = logoUrl, + logo = model.logo != null ? await ConvertToByteArrayAsync(model.logo) : null + }; + + // 3️⃣ Invio dati all'API datiazienda/add + string apiAddUrl = apiUrl + "datiazienda/add?token=" + token; + using (var httpClient = new HttpClient()) + { + string data = JsonConvert.SerializeObject(dat); + StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); + HttpResponseMessage saveResponse = await httpClient.PostAsync(apiAddUrl, content); + + if (!saveResponse.IsSuccessStatusCode) + { + string err = await saveResponse.Content.ReadAsStringAsync(); + return BadRequest("Errore salvataggio dati: " + err); + } } + + // ✅ Risposta JSON usata dalla view per aprire la finestra popup + return Json(new { success = true }); } + /// + /// Pagina popup mostrata dopo il salvataggio + /// + public IActionResult Dialog() + { + return View(); + } + + #endregion CREATE #region EDIT @@ -228,102 +219,73 @@ namespace VirtualTask.Controllers } [HttpPost] - public IActionResult Edit(DatiAziendaTable model) + public async Task Edit(DatiAziendaTable model) { SessionHelper helper = new SessionHelper(this); - token = helper.GetStringValue("tok"); - tenant = helper.GetStringValue("tenant"); - tenant2 = helper.GetStringValue("tenant2"); + string token = helper.GetStringValue("tok"); + string tenant2 = helper.GetStringValue("tenant2"); if (string.IsNullOrEmpty(token)) - { return RedirectToAction("Login2", "Login"); - } - //model.azienda = tenant; - if(model.logo2!=null) + + string apiUrl = helper.GetStringValue("apiUrl"); + + // 1️⃣ Upload del logo tramite API UploadLogo + if (model.logo2 != null && model.logo2.Length > 0) { - string pic = System.IO.Path.GetFileName(model.logo2.FileName); + string uploadUrl = $"{apiUrl}datiazienda/upload_logo?token={token}"; - //2025-05-05: gestione directory iommagine nel caso che l'azienda sia più corta di 5 caratteri - string dir = tenant2; - if (!string.IsNullOrEmpty(tenant2) && tenant2.Trim().Length < 5) + using (var httpClient = new HttpClient()) + using (var form = new MultipartFormDataContent()) { - dir = tenant2.Trim(); - } - string path = string.Format("{0}{1}\\{2}", _pathLoghi, dir, pic); - //string projectRootPath = _hostingEnvironment.ContentRootPath; - - //// file is uploaded - using (Stream fileStream = new FileStream(path, FileMode.Create)) - { - model.logo2.CopyToAsync(fileStream); + var fileContent = new StreamContent(model.logo2.OpenReadStream()); + fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue( + string.IsNullOrWhiteSpace(model.logo2.ContentType) ? "application/octet-stream" : model.logo2.ContentType + ); + form.Add(fileContent, "file", model.logo2.FileName); + + HttpResponseMessage response = await httpClient.PostAsync(uploadUrl, form); + if (!response.IsSuccessStatusCode) + { + string err = await response.Content.ReadAsStringAsync(); + ModelState.AddModelError("", "Errore upload logo: " + err); + return View(model); + } + + dynamic result = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + model.url_logo = result.url; } - //// save the image path path to the database or you can send image - //// directly to database - //// in-case if you want to store byte[] ie. for DB + // Salvo i byte del file nel modello se serve per il DB using (MemoryStream ms = new MemoryStream()) { - model.logo2.CopyTo(ms); - byte[] array = ms.GetBuffer(); - model.logo = array; + await model.logo2.CopyToAsync(ms); + model.logo = ms.ToArray(); } model.logo2 = null; - model.url_logo = string.Format("{0}{1}/{2}", _urlLoghi, dir, pic); } - - apiUrl = helper.GetStringValue("apiUrl"); - urlBase = apiUrl + "datiazienda/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) + // 2️⃣ Invio dati all'API datiazienda/mod + string modUrl = $"{apiUrl}datiazienda/mod?token={token}"; + using (var client = new HttpClient()) { - //prima di andare alla pagina devo chiamare il metodo per - //salvare il file in locale in modo che sia visibile sul web dall'app - //C:\ZAPIPOLO\loghi - //https://api.poloinformatico.it:9000/api/Polo/datiazienda/saveFile?azienda=AZI02&tecnico=aaaaa%20%20%20%20%20%20%20%20%20%20&pathSrv=C%3A%5CZAPIPOLO%5Cloghi' - apiUrl = helper.GetStringValue("apiUrl"); - urlBase = apiUrl + "datiazienda/savefile"; - if(!string.IsNullOrEmpty(model.azienda) && model.azienda.Length<5) - { - model.azienda= model.azienda.Trim(); - } - urlBase = urlBase + "?azienda=" + model.azienda; - urlBase = urlBase + "&tecnico=" + model.tecnico.Trim(); - urlBase = urlBase + "&pathSrv=" + _pathLoghi; - 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 = await client.PostAsync(modUrl, content); - data = JsonConvert.SerializeObject(model); - content = new StringContent(data, Encoding.UTF8, "application/json"); - HttpResponseMessage response2 = client.PostAsync(baseAddress, content).Result; - if (response2.IsSuccessStatusCode) + if (!response.IsSuccessStatusCode) { - return RedirectToAction("Index"); - } - else - { - errMes = response2.Content.ReadAsStringAsync().Result; + string errMes = await response.Content.ReadAsStringAsync(); helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } - else - { - errMes = response.Content.ReadAsStringAsync().Result; - helper.SetStringValue("errMsg", errMes); - return RedirectToAction("Error"); - } + + //return RedirectToAction("Index"); + // ✅ Risposta OK per far apparire la dialog nella view + return Ok(); + } #endregion @@ -480,5 +442,24 @@ namespace VirtualTask.Controllers } return selectItems; } + + // Metodo helper per convertire IFormFile in byte[] + private async Task ConvertToByteArrayAsync(IFormFile file) + { + using (var ms = new MemoryStream()) + { + await file.CopyToAsync(ms); + return ms.ToArray(); + } + } + + + // Classe per deserializzare la risposta JSON dell'API + private class UploadResponse + { + public string? message { get; set; } + public string? url { get; set; } + public string? fileName { get; set; } + } } } diff --git a/Views/Chiamate/Create.cshtml b/Views/Chiamate/Create.cshtml index aa0bc74..e6fb00a 100644 --- a/Views/Chiamate/Create.cshtml +++ b/Views/Chiamate/Create.cshtml @@ -13,7 +13,7 @@
- +
@Html.DropDownListFor(x => x.chcodimp, (IEnumerable)ViewBag.Impianti, new { @class = "agy-form-field require" }) @@ -25,19 +25,19 @@ @Html.DropDownListFor(x => x.chtchiam, (IEnumerable)ViewBag.Tecnici, new { @class = "agy-form-field require" })
-
 
+ @*
 
@Html.DropDownListFor(x => x.chstato, (IEnumerable)ViewBag.StatiChiamata, new { @class = "agy-form-field require" }) -
+
*@
 
@* *@ - +
 
diff --git a/Views/DatiAzienda/Create.cshtml b/Views/DatiAzienda/Create.cshtml index 5f693e7..09d59eb 100644 --- a/Views/DatiAzienda/Create.cshtml +++ b/Views/DatiAzienda/Create.cshtml @@ -4,61 +4,133 @@ ViewData["Title"] = "Nuova Intestazione Buoni Intervento"; Layout = "~/Views/Shared/_LayoutAreaRiservata.cshtml"; } +
- +
@Html.HiddenFor(x => x.azienda) @Html.HiddenFor(x => x.url_logo) +
- @Html.DropDownListFor(x =>x.tecnico,(IEnumerable)ViewBag.AllTecnici,new {@class = "form-control"}) + @Html.DropDownListFor(x => x.tecnico, (IEnumerable)ViewBag.AllTecnici, new { @class = "form-control" })
+
 
+
+
 
+
+
 
+
- @**@
+
 
+
-
- @* Torna alla lista *@ +
+
+
+ + + + + @section Scripts { - @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} + @{ + await Html.RenderPartialAsync("_ValidationScriptsPartial"); + } + } diff --git a/Views/DatiAzienda/Edit.cshtml b/Views/DatiAzienda/Edit.cshtml index 8ac4639..6621c86 100644 --- a/Views/DatiAzienda/Edit.cshtml +++ b/Views/DatiAzienda/Edit.cshtml @@ -10,61 +10,113 @@
-
+
@Html.HiddenFor(x => x.azienda) @Html.HiddenFor(x => x.url_logo) @Html.HiddenFor(x => x.logo)
-
- @Html.DropDownListFor(x => x.tecnico,(IEnumerable)ViewBag.AllTecnici, new {@class = "form-control"}) +
+ @Html.DropDownListFor(x => x.tecnico, (IEnumerable)ViewBag.AllTecnici, new { @class = "form-control" })
-
 
+
-
+
-
 
+
-
+
@{ - byte[] appo = Model.logo; - var base64 = Convert.ToBase64String(appo); - var imgSrc = String.Format("data:image/gif;base64,{0}", base64); + if (Model.logo != null && Model.logo.Length > 0) + { + var base64 = Convert.ToBase64String(Model.logo); + var imgSrc = $"data:image/png;base64,{base64}"; + + } } -
-
 
+
-
- +
+
-
-
- @* Back to List *@ +
+
+ + + + + + - @section Scripts { - @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} + @{ + await Html.RenderPartialAsync("_ValidationScriptsPartial"); + } + } diff --git a/appsettings.json b/appsettings.json index 2880891..d355d84 100644 --- a/appsettings.json +++ b/appsettings.json @@ -7,11 +7,12 @@ }, "ApplicationInsights": { - ////PRODUZIONE + //PRODUZIONE "rootUrlApi": "https://api-vt.poloinformatico.it/api/Polo/", "rootUrlApi2": "https://api-vt.poloinformatico.it/VIRTU/", //"rootWebLoghi": "C:\\ZAPIPOLO\\api_polo\\wwwroot\\VIRTU\\", - "rootWebLoghi": "/zucchetti/api/api-vt.poloinformatico.it/app/wwwroot/VIRTU/", + //"rootWebLoghi": "/zucchetti/api/api-vt.poloinformatico.it/app/wwwroot/VIRTU/", + "rootWebLoghi": "./wwwroot/VIRTU", "rootUrl": "https://virtualtask.it/", "rootPath": "/mnt/storagebox", @@ -21,10 +22,11 @@ //"rootWebLoghi": "C:\\Users\\audif\\source\\repos\\VirtualTask\\wwwroot\\VIRTU\\", //"rootUrl": "https://localhost:7140/", - //MICHELE: PUNTAMENTO A MIO PC PER FARE I TEST + ////MICHELE: PUNTAMENTO A MIO PC PER FARE I TEST //"rootUrlApi": "https://localhost:7068/api/Polo/", //"rootUrlApi2": "https://localhost:7068//VIRTU/", - //"rootWebLoghi": "C:\\Users\\audif\\source\\repos\\VirtualTask\\wwwroot\\VIRTU\\", + ////"rootWebLoghi": "C:\\Users\\audif\\source\\repos\\VirtualTask\\wwwroot\\VIRTU\\", + //"rootWebLoghi": "/zucchetti/api/api-vt.poloinformatico.it/app/wwwroot/VIRTU/", //"rootUrl": "https://localhost:7068/", "mittenteMail": "info@virtualtask.it",