VirtualTask/Controllers/Rapp_NewController.cs

514 lines
18 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
using VirtualTask.Models;
using X.PagedList;
namespace VirtualTask.Controllers
{
public class Rapp_NewController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string token = string.Empty;
string errMes = string.Empty;
string admin = string.Empty;
string time_sheet = string.Empty;
HttpClient client;
private readonly IConfiguration _configuration;
public Rapp_NewController(IConfiguration configuration)
{
client = new HttpClient();
_configuration = configuration;
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
#region INDEX
public IActionResult Index(DateTime dataIni, DateTime dataFin, string Codbuono, string tecnico, 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 + "rappnewList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<Rapp_New> modelList = new List<Rapp_New>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Rapp_New>>(data);
if (dataIni.Date != DateTime.MinValue)
{
modelList = modelList.Where(x => x.data_rapportino.Date >= dataIni.Date).ToList();
ViewData["dataIni"] = dataIni;
}
else
{
ViewData["dataIni"] = null;
}
if (dataFin.Date != DateTime.MinValue)
{
modelList = modelList.Where(x => x.data_rapportino.Date <= dataFin.Date).ToList();
ViewData["dataFin"] = dataFin;
}
else
{
ViewData["dataFin"] = null;
}
if (!string.IsNullOrEmpty(Codbuono))
{
//modelList = modelList.Where(x => x.seriale_rapportino.ToUpper().Contains(Codbuono)).ToList();
modelList = modelList.Where(x => x.ser_buono.ToUpper().Contains(Codbuono)).ToList();
ViewData["Codbuono"] = Codbuono;
}
else
ViewData["Codbuono"] = null;
if (!string.IsNullOrEmpty(tecnico))
{
modelList = modelList.Where(x => x.codice_tecnico.ToUpper().Contains(tecnico)).ToList();
ViewData["tecnico"] = tecnico;
}
else
ViewData["tecnico"] = null;
if (page != null && page < 1)
{
page = 1;
}
var pageSize = 10;
var shortLinks = modelList
.OrderByDescending(s => s.seriale_rapportino)
.ToPagedList(page ?? 1, pageSize);
ViewBag.Tecnici = GetTecnici();
return View(shortLinks);
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
#endregion INDEX
#region CREATE
// 08/11/2023 TOLTI METODI CREATE/EDIT/DELETE PERCHE' IL RAPPORTINO VIENE TUTTO GESTITO DALL'APP.
//public IActionResult Create()
//{
// ViewBag.Tecnici = GetTecnici();
// ViewBag.Chiusure = GetCodChiusura();
// return View();
//}
//[HttpPost]
//public IActionResult Create(Rapp_New model)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// if (string.IsNullOrEmpty(token))
// return RedirectToAction("Index", "Login");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnew/add";
// urlBase = urlBase + "?token=" + token;
// Uri baseAddress = new Uri(urlBase);
// 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("Error");
// }
// return View();
//}
#endregion CREATE
#region DETAIL
public IActionResult Details(string id)
{
SessionHelper helper = new SessionHelper(this);
//id = "V000000135";
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "rappnewList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Rapp_New rapp = new Rapp_New();
List<Rapp_New> modelList = new List<Rapp_New>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Rapp_New>>(data);
rapp = modelList.Where(x => x.seriale_rapportino.Equals(id)).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(rapp);
}
#endregion DETAIL
#region EDIT
// 08/11/2023 TOLTI METODI CREATE/EDIT/DELETE PERCHE' IL RAPPORTINO VIENE TUTTO GESTITO DALL'APP.
//public IActionResult Edit(string id)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnewList";
// urlBase = urlBase + "?token=" + token;
// Uri baseAddress = new Uri(urlBase);
// client = new HttpClient();
// client.BaseAddress = baseAddress;
// Rapp_New rapp = new Rapp_New();
// List<Rapp_New> modelList = new List<Rapp_New>();
// HttpResponseMessage response = client.GetAsync(baseAddress).Result;
// if (response.IsSuccessStatusCode)
// {
// string data = response.Content.ReadAsStringAsync().Result;
// modelList = JsonConvert.DeserializeObject<List<Rapp_New>>(data);
// rapp = modelList.Where(x => x.seriale_rapportino.Equals(id)).First();
// }
// else
// {
// errMes = response.Content.ReadAsStringAsync().Result;
// helper.SetStringValue("errMsg", errMes);
// return RedirectToAction("Error");
// }
// ViewBag.Chiusure = GetCodChiusura();
// return View(rapp);
//}
//[HttpPost]
//public IActionResult Edit(Rapp_New rapp)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnew/mod";
// urlBase = urlBase + "?token=" + token;
// Uri baseAddress = new Uri(urlBase);
// client = new HttpClient();
// client.BaseAddress = baseAddress;
// string data = JsonConvert.SerializeObject(rapp);
// 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(rapp);
//}
#endregion EDIT
#region DELETE
// 08/11/2023 TOLTI METODI CREATE/EDIT/DELETE PERCHE' IL RAPPORTINO VIENE TUTTO GESTITO DALL'APP.
//[HttpGet]
//public IActionResult Delete(string id)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnewList";
// urlBase = urlBase + "?token=" + token;
// Uri baseAddress = new Uri(urlBase);
// client = new HttpClient();
// client.BaseAddress = baseAddress;
// Rapp_New rapp = new Rapp_New();
// List<Rapp_New> modelList = new List<Rapp_New>();
// HttpResponseMessage response = client.GetAsync(baseAddress).Result;
// if (response.IsSuccessStatusCode)
// {
// string data = response.Content.ReadAsStringAsync().Result;
// modelList = JsonConvert.DeserializeObject<List<Rapp_New>>(data);
// rapp = modelList.Where(x => x.seriale_rapportino.Equals(id)).First();
// }
// else
// {
// errMes = response.Content.ReadAsStringAsync().Result;
// helper.SetStringValue("errMsg", errMes);
// return RedirectToAction("Error");
// }
// return View(rapp);
//}
//[HttpPost, ActionName("DeleteConfirmed")]
//public IActionResult DeleteConfirmed(string id)
//{
// SessionHelper helper = new SessionHelper(this);
// token = helper.GetStringValue("tok");
// apiUrl = helper.GetStringValue("apiUrl");
// urlBase = apiUrl + "rappnew/del?" + "serialeRapportino=" + 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");
// }
// return View();
//}
#endregion DELETE
#region IMMAGINE
public IActionResult Immagine(string id)
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
admin = helper.GetStringValue("admin");
ViewBag.Admin = admin;
urlBase = apiUrl + "rappnewList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
Rapp_New rapp = new Rapp_New();
List<Rapp_New> modelList = new List<Rapp_New>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Rapp_New>>(data);
rapp = modelList.Where(x => x.seriale_rapportino.Equals(id)).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(rapp);
}
#endregion
#region FOTO ALLEGATE
public IActionResult ShowImage(string filePath)
{
//ViewBag.FileName = Path.GetFileName(filePath);
ViewBag.FileName = filePath;
return View();
}
public IActionResult GetImage(string filePath)
{
//var filePath = Path.Combine(_basePath, fileName);
if (!System.IO.File.Exists(filePath))
return NotFound();
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
return File(stream, "image/jpeg");
}
#endregion
//metodo per riempire combobox con la tabella tecnici
private List<SelectListItem> 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<SelectListItem> selectItems = new List<SelectListItem>();
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();
//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/*role.tcdescri*/;
selectItems.Add(listItem);
}
}
return selectItems;
}
private List<SelectListItem> GetCodChiusura()
{
SessionHelper helper = new SessionHelper(this);
token = helper.GetStringValue("tok");
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "chiusureVtList";
urlBase = urlBase + "?token=" + token;
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
List<SelectListItem> selectItems = new List<SelectListItem>();
List<Chiusure> modelList = new List<Chiusure>();
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<Chiusure>>(data);
foreach (var role in modelList)
{
SelectListItem listItem = new SelectListItem();
if (role == modelList.First())
{
listItem.Value = string.Empty;
listItem.Text = "--Selezionare codice chiusura--";
selectItems.Add(listItem);
}
else
{
listItem.Value = role.cccodice;
listItem.Text = role.ccdescr;
selectItems.Add(listItem);
}
}
}
return selectItems;
}
[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 });
}
}
}