165 lines
3.5 KiB
C#
165 lines
3.5 KiB
C#
using System.Web;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Diagnostics;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using CVRLIB.CARTS;
|
|
using static CVRLIB.CVSTRINGS.TextFuncs;
|
|
|
|
using CVRLIB;
|
|
using CVRLIB.Generic;
|
|
|
|
|
|
using ZXing;
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
namespace VSERVERWS.Controllers;
|
|
|
|
|
|
|
|
//https://vserverws.cvr.cvrco.ca/api/cart/CartStatus
|
|
|
|
[Route("api/Cart/[action]")]
|
|
[ApiController]
|
|
public class CartController : ControllerBase {
|
|
|
|
|
|
|
|
[HttpGet]
|
|
public IActionResult CartStatus() {
|
|
|
|
var rOBJ = new { STATUS = "OK" };
|
|
|
|
return new JsonResult(rOBJ);
|
|
}
|
|
|
|
|
|
//https://localhost:7210/api/Cart/SubmitCartHTML?id=1337&source=beta%2Ecvr%2Ecvrco%2Eca
|
|
|
|
[HttpPost]
|
|
[Consumes("text/plain")]
|
|
public async Task<IActionResult> SubmitCartHTML(string id, string source) {
|
|
|
|
var rTOR = new TryOperationResult();
|
|
|
|
var isEncrypted = false;
|
|
var tdata = "";
|
|
var data = "";
|
|
|
|
var HTML_PATH = Program.XDRIVE + "\\CVRCO\\BU\\Carts\\HTML";
|
|
|
|
var inSource = HttpUtility.UrlDecode(source);
|
|
|
|
if (!string.IsNullOrEmpty(inSource)) {
|
|
HTML_PATH = HTML_PATH + "\\" + inSource;
|
|
|
|
if (!Directory.Exists(HTML_PATH)) {
|
|
Directory.CreateDirectory(HTML_PATH);
|
|
}
|
|
} else {
|
|
|
|
rTOR.AddResult(false, "READ_SOURCE", "no source provided!");
|
|
|
|
return new JsonResult(rTOR);
|
|
}
|
|
|
|
|
|
using (var SR = new StreamReader(Request.Body)) {
|
|
data = await SR.ReadToEndAsync();
|
|
}
|
|
|
|
|
|
|
|
if (!Directory.Exists(HTML_PATH)) {
|
|
Directory.CreateDirectory(HTML_PATH);
|
|
}
|
|
|
|
if (!tdata.Contains("<html>")) { isEncrypted = true; }
|
|
|
|
|
|
try {
|
|
|
|
if (isEncrypted) {
|
|
tdata = NonSecureDecryptHexStringToString(data);
|
|
}
|
|
|
|
if (!tdata.Contains("<html>")) {
|
|
throw new Exception("Invalid html string");
|
|
}
|
|
|
|
|
|
|
|
using (var FS = new FileStream(HTML_PATH + "\\" + id + ".txt", FileMode.Create, FileAccess.Write, FileShare.None)) {
|
|
using (var SW = new StreamWriter(FS)) {
|
|
|
|
if (isEncrypted) {
|
|
await SW.WriteAsync(data);
|
|
} else {
|
|
await SW.WriteAsync(NonSecureEncryptStringToHexString(tdata));
|
|
}
|
|
|
|
};
|
|
};
|
|
|
|
rTOR.AddResult(true, "TRY_SAVE_HTML_CART");
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
rTOR.AddResult(false, "TRY_SAVE_HTML_CART", ex.Message, ex);
|
|
|
|
Debug.WriteLine(ex.Message);
|
|
Debug.WriteLine(ex.StackTrace);
|
|
}
|
|
|
|
return new JsonResult(rTOR);
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> SubmitCartData([FromBody] byte[] data) {
|
|
|
|
var JSON = await NonSecureDecryptByteAToStringAsync(data);
|
|
|
|
var rOBJ = new TryCartOperationResult();
|
|
|
|
try {
|
|
var tCART = JsonConvert.DeserializeObject<ShoppingCart>(JSON);
|
|
|
|
if (tCART != null) {
|
|
|
|
tCART.TrySyncToInventory(CVGlobal.INVENTORY);
|
|
|
|
await tCART.SaveToDBAsync();
|
|
|
|
rOBJ.AddResult(true, "CART_SAVE_TO_DB");
|
|
|
|
|
|
} else {
|
|
|
|
rOBJ.AddResult(false, "CONVERT_TO_JSON", "Failed to convert to json from byte data.");
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
rOBJ.AddResult(false, "CART_SAVE_TRY", ex.Message, ex);
|
|
|
|
}
|
|
|
|
|
|
//return new result
|
|
return new JsonResult(rOBJ);
|
|
}
|
|
|
|
|
|
|
|
}
|