SoftwayWeb/Controllers/GiriEliminaController.cs

106 lines
3.7 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SoftwayWeb.Models;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Text;
namespace SoftwayWeb.Controllers
{
public class GiriEliminaController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string errMes = string.Empty;
private readonly IConfiguration _configuration;
HttpClient client;
public GiriEliminaController(IConfiguration configuration)
{
client = new HttpClient();
_configuration = configuration;
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
public IActionResult Index()
{
return RedirectToAction("index", "Giri");
}
public IActionResult Elimina(string id)
{
SessionHelper helper = new SessionHelper(this);
GiriConsegnaView model = new GiriConsegnaView();
List<GiriConsegnaView> modelList = new List<GiriConsegnaView>();
apiUrl = helper.GetStringValue("apiUrl");
//urlBase = apiUrl + "Giri/delGiro";
urlBase = apiUrl + "Giri/listaGiri?aperto=true";
Uri baseAddress = new Uri(urlBase);
client = new HttpClient();
client.BaseAddress = baseAddress;
HttpResponseMessage response = client.GetAsync(baseAddress).Result;
if (response.IsSuccessStatusCode)
{
string data = response.Content.ReadAsStringAsync().Result;
modelList = JsonConvert.DeserializeObject<List<GiriConsegnaView>>(data);
model = modelList.Where(x => x.SerialeGiro.Equals(id)).First();
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(model);
}
[HttpPost, ActionName("Elimina")]
public IActionResult Elimina(GiriConsegnaView model)
{
SessionHelper helper = new SessionHelper(this);
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/delGiro?serialeGiro=";
urlBase = urlBase + model.SerialeGiro.TrimEnd();
urlBase = urlBase + "&autista=";
urlBase = urlBase + model.CodAutista.TrimEnd();
urlBase = urlBase + "&data=";
urlBase = urlBase + model.DataGiro;
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)
{
return RedirectToAction("Index", "Giri");
}
else
{
errMes = response.Content.ReadAsStringAsync().Result;
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
}
[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 });
}
}
}