SoftwayWeb/Controllers/GiriChiudiController.cs
2025-04-02 15:48:42 +02:00

131 lines
4.7 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SoftwayWeb.Models;
using System.Diagnostics;
using System.Text;
namespace SoftwayWeb.Controllers
{
public class GiriChiudiController : Controller
{
string apiUrl = string.Empty;
string urlBase = string.Empty;
string errMes = string.Empty;
string token = string.Empty;
private readonly IConfiguration _configuration;
HttpClient client;
public GiriChiudiController(IConfiguration configuration)
{
client = new HttpClient();
_configuration = configuration;
var key = _configuration["ApplicationInsights:rootUrlApi"];
apiUrl = key;
}
// GET: GiriChiudiController
public ActionResult Index()
{
SessionHelper helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
// return View();
return RedirectToAction("index", "giri");
}
public ActionResult Chiudi(string id, int nbanc,decimal imp)
{
SessionHelper helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
GiriConsegnaView model = new GiriConsegnaView();
List<GiriConsegnaView> lst=new List<GiriConsegnaView>();
//https://api.poloinformatico.it:8000/api/Giri/listaGiri?aperto=true
apiUrl = helper.GetStringValue("apiUrl");
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;
lst = JsonConvert.DeserializeObject<List<GiriConsegnaView>>(data);
model = lst.Where(x => x.SerialeGiro.Equals(id)).First();
model.BancaliRecuperati = nbanc;
model.ImportoRecuperato = imp;
}
else
{
errMes = helper.getErrMsgFromResponse(response);
helper.SetStringValue("errMsg", errMes);
return RedirectToAction("Error");
}
return View(model);
}
[HttpPost]
public ActionResult Chiudi(GiriConsegnaView model)
{
//https://api.poloinformatico.it:8000/api/Giri/closeGiro?serialeGiro=0000000002&bancaliRecuperati=11&importoRecuperato=100
SessionHelper helper = new SessionHelper(this);
helper.ClearFormatedKey("errMsg");
token = helper.GetStringValue("tok");
if (string.IsNullOrEmpty(token))
{
return RedirectToAction("Login", "Login");
}
apiUrl = helper.GetStringValue("apiUrl");
urlBase = apiUrl + "Giri/closeGiro?serialeGiro=";
urlBase = urlBase + model.SerialeGiro;
urlBase = urlBase + "&bancaliRecuperati=";
urlBase = urlBase + model.BancaliRecuperati;
urlBase = urlBase + "&importoRecuperato=";
urlBase = urlBase + model.ImportoRecuperato;
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 = helper.getErrMsgFromResponse(response);
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 });
}
}
}