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.Diagnostics; 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; 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"); 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); //if (!string.IsNullOrEmpty(searchString)) //{ // modelList = modelList.Where(s => s.andescri.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.ancodice) // .ToPagedList(page ?? 1, pageSize); 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) { return RedirectToAction("Index"); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } #endregion CREATE [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; } } }