private List <CartOverviewItem> GetCartItemsFromCookie() { try { // Tries to parse the cart from the cookie to a C# object, if it fails, the cookie will be removed and a 404 will be thrown. CartFromJSON cart = Newtonsoft.Json.JsonConvert.DeserializeObject <CartFromJSON>(Request.Cookies[ConstVal.cookieCartName].Value); // Makes a list of the product IDs (duplicate mean quantity > 1) List <int> IDs = new List <int>(); foreach (var prod in cart.ProductList) { for (int i = 0; i < prod.Quantity; i++) { IDs.Add(prod.ID); } } var xy = _prodRepo.GetCartOverview(GetISO(), IDs).OrderBy(x => x.ProductName).ToList(); return(xy); } catch (Exception) { HttpContext.Response.Cookies[ConstVal.cookieCartName].Expires = DateTime.Now.AddDays(-1); return(null); } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { // Checks if there is a cookie for the cart if (filterContext.HttpContext.Request.Cookies[ConstVal.cookieCartName] == null) { RedirectToOtherPage(filterContext); } else { // If a cookie exists, check if it has a value var cookie = filterContext.HttpContext.Request.Cookies[ConstVal.cookieCartName]; if (cookie.Value == null || cookie.Value == "undefined") { RedirectToOtherPage(filterContext); } else { try { // If the cookie exists, and has a value, check if it can be Deserialized to a c# object CartFromJSON cart = Newtonsoft.Json.JsonConvert.DeserializeObject <CartFromJSON>(cookie.Value); } catch (Exception) { // If the value is corrupted, delete the cookie filterContext.HttpContext.Response.Cookies[ConstVal.cookieCartName].Expires = DateTime.Now.AddDays(-1); RedirectToOtherPage(filterContext); } } } }
private Decimal GetTotalPriceFromCookieCart() { // TODO Error // Gets a list of IDs from the products in the cart (can contain duplicates which means higher quantity) CartFromJSON cart = Newtonsoft.Json.JsonConvert.DeserializeObject <CartFromJSON>(Request.Cookies[ConstVal.cookieCartName].Value); List <int> IDs = new List <int>(); foreach (var prod in cart.ProductList) { for (int i = 0; i < prod.Quantity; i++) { IDs.Add(prod.ID); } } return(_prodRepo.GetTotalPrice(IDs)); }