using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.DotNet.Scaffolding.Shared.CodeModifier.CodeChange; using Microsoft.Extensions.Hosting.Internal; using Newtonsoft.Json; using System; using System.Diagnostics; using System.Reflection; using System.Text; using System.Web; using VirtualTask.Models; namespace VirtualTask.Controllers { public class DatiAziendaController : Controller { string apiUrl = string.Empty; string urlBase = string.Empty; string token = string.Empty; string tenant = string.Empty; string tenant2 = string.Empty; string errMes = string.Empty; string _pathLoghi = string.Empty; string _urlLoghi = string.Empty; HttpClient client; string admin = string.Empty; private readonly IConfiguration _configuration; //private readonly Microsoft.Extensions.Hosting.IHostingEnvironment _hostingEnvironment; string time_sheet=string.Empty; public DatiAziendaController(IConfiguration configuration /*,Microsoft.Extensions.Hosting.IHostingEnvironment hostingEnvironment*/) { client = new HttpClient(); _configuration = configuration; var key = _configuration["ApplicationInsights:rootUrlApi"]; apiUrl = key; //_hostingEnvironment = hostingEnvironment; _pathLoghi = _configuration["ApplicationInsights:rootWebLoghi"]; _urlLoghi = _configuration["ApplicationInsights:rootUrlApi2"]; } public IActionResult Index() { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); tenant = helper.GetStringValue("tenant"); tenant2= helper.GetStringValue("tenant2"); time_sheet = helper.GetStringValue("time_sheet"); ViewBag.TimeSheet = time_sheet; if (string.IsNullOrEmpty(token)) { return RedirectToAction("Login2", "Login"); } apiUrl = helper.GetStringValue("apiUrl"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; urlBase = apiUrl + "datiaziendaList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); 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); return View(modelList); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } #region CREATE public IActionResult Create() { SessionHelper helper = new SessionHelper(this); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; ViewBag.AllTecnici = getTecnici(); return View(); } [HttpPost] public IActionResult 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"); ViewBag.Admin = admin; if (model.logo != null) { string pic = System.IO.Path.GetFileName(model.logo.FileName); string path = string.Format("{0}{1}\\{2}",_pathLoghi,tenant2,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, tenant2, pic); dat.ragsoc = model.ragsoc; dat.tecnico = model.tecnico; } 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 data = JsonConvert.SerializeObject(dat); StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; if (response.IsSuccessStatusCode) { //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) { return RedirectToAction("Index"); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } #endregion CREATE #region EDIT public IActionResult Edit(string azienda,string tecnico) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); apiUrl = helper.GetStringValue("apiUrl"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; urlBase = apiUrl + "datiaziendaList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; DatiAziendaTable dat = new DatiAziendaTable(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); dat = modelList.Where(t => t.azienda.Equals(azienda) && t.tecnico.Equals(tecnico)).First(); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } ViewBag.AllTecnici = getTecnici(); return View(dat); } [HttpPost] public IActionResult Edit(DatiAziendaTable model) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); tenant = helper.GetStringValue("tenant"); tenant2 = helper.GetStringValue("tenant2"); if (string.IsNullOrEmpty(token)) { return RedirectToAction("Login2", "Login"); } //model.azienda = tenant; if(model.logo2!=null) { string pic = System.IO.Path.GetFileName(model.logo2.FileName); string path = string.Format("{0}{1}\\{2}", _pathLoghi, tenant2, pic); //string projectRootPath = _hostingEnvironment.ContentRootPath; //// file is uploaded using (Stream fileStream = new FileStream(path, FileMode.Create)) { model.logo2.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.logo2.CopyTo(ms); byte[] array = ms.GetBuffer(); model.logo = array; } model.logo2 = null; model.url_logo = string.Format("{0}{1}/{2}", _urlLoghi, tenant2, 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) { //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.Trim(); //urlBase = urlBase + "&pathSrv=" + "C:\\ZAPIPOLO\\loghi"; urlBase = urlBase + "&pathSrv=" + _pathLoghi; baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; data = JsonConvert.SerializeObject(model); content = new StringContent(data, Encoding.UTF8, "application/json"); HttpResponseMessage response2 = client.PostAsync(baseAddress, content).Result; if (response2.IsSuccessStatusCode) { return RedirectToAction("Index"); } else { errMes = response2.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } #endregion #region DETAIL public IActionResult Detail(string azienda, string tecnico) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); apiUrl = helper.GetStringValue("apiUrl"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; urlBase = apiUrl + "datiaziendaList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; DatiAziendaTable dat = new DatiAziendaTable(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); dat = modelList.Where(t => t.azienda.Equals(azienda) && t.tecnico.Equals(tecnico)).First(); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } ViewBag.AllTecnici = getTecnici(); return View(dat); } #endregion #region DELETE [HttpGet] public IActionResult Delete(string azienda, string tecnico) { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); apiUrl = helper.GetStringValue("apiUrl"); admin = helper.GetStringValue("admin"); ViewBag.Admin = admin; urlBase = apiUrl + "datiaziendaList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; DatiAziendaTable dat = new DatiAziendaTable(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); dat = modelList.Where(x => x.azienda.Equals(azienda) && x.tecnico.Equals(tecnico)).First(); } return View(dat); } [HttpPost, ActionName("DeleteConfirmed")] public IActionResult DeleteConfirmed(string azienda, string tecnico) { 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 + "datiazienda/del"; urlBase = urlBase + "?azienda=" + azienda; urlBase = urlBase + "&tecnico=" + tecnico; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; string data = JsonConvert.SerializeObject(azienda); 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 }); } private List getTecnici() { SessionHelper helper = new SessionHelper(this); token = helper.GetStringValue("tok"); apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "tecniciList"; urlBase = urlBase + "?token=" + token; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; List selectItems = new List(); List modelList = new List(); HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); //per gestire primo elemento tendina (deve essere vuoto) SelectListItem listItemFirst = new SelectListItem(); listItemFirst.Value = string.Empty; listItemFirst.Text = " - Tecnico"; selectItems.Add(listItemFirst); foreach (var role in modelList) { SelectListItem listItem = new SelectListItem(); string s = role.tccodice + " - " + role.tcdescri; listItem.Value = role.tccodice; listItem.Text = s; selectItems.Add(listItem); } } return selectItems; } } }