public string CheckForCookies() { Cryptor cryptor = new Cryptor(); if (Request.Cookies["Guest"] == null) { HttpCookie GuestCookie = new HttpCookie("Guest"); Random rnd = new Random(); int randomNumber1 = rnd.Next(1, 10); int randomNumber2 = rnd.Next(11, 200); string GuestValue = cryptor.Encrypt(DateTime.Now.ToString() + randomNumber1 + randomNumber2, true); GuestCookie.Value = GuestValue; GuestCookie.Expires = DateTime.Now.AddHours(1); Response.Cookies.Add(GuestCookie); try { CookieCart cookieCart = new CookieCart(); cookieCart.CookieId = GuestValue; cookieCart.ProductsInCart = new List <ProductInCart>(); cookieCart.WholePrice = 0; CookieCartService.CreateCookieCart(cookieCart); CookieCartService.SaveCookieCart(); return(GuestValue); } catch (Exception E) { throw new Exception(E.ToString()); } } else { return(Request.Cookies["Guest"].Value); } }
public void ConfigureServices(IServiceCollection services) { services.AddMvc(option => { option.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); option.Filters.Add(new RequireHttpsAttribute()); }); services.AddScoped(sp => CookieCart.GetCart(sp)); services.AddScoped(sp => DefaultDataTier.GetUnitOfWork(sp)); services.AddHttpContextAccessor(); services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddMemoryCache(); services.AddSession(); services.TryAddSingleton <IActionContextAccessor, ActionContextAccessor>(); }
public void CreateCookieCart(CookieCart CookieCart) { CookieCartsRepository.Add(CookieCart); }
public ActionResult ListSizes(int ProdId, int Quantity) { ProductInCart productInCart = new ProductInCart(); Product actualProduct = ProductService.GetProduct(ProdId); productInCart.ProductId = ProdId; productInCart.Quantity = Quantity; productInCart.SubtotalForProduct = Decimal.Multiply(productInCart.Quantity, actualProduct.PriceEU); if (User.Identity.IsAuthenticated) { var UserId = User.Identity.GetUserId(); var user = UserService.GetApplicationUser(UserId); var product = user.Cart.ProductsInCart .Where(a => a.CartId == UserId && a.ProductId == productInCart.ProductId && a.OrderId == null) .FirstOrDefault(); if (product != null) { product.Quantity += productInCart.Quantity; product.SubtotalForProduct += productInCart.SubtotalForProduct; } else { user.Cart.ProductsInCart.Add(productInCart); } UserService.UpdateApplicationUser(user); UserService.SaveApplicationUser(); } else { Store.Model.Models.Cryptor cryptor = new Cryptor(); if (Request.Cookies["Guest"] == null) { HttpCookie GuestCookie = new HttpCookie("Guest"); Random rnd = new Random(); int randomNumber1 = rnd.Next(1, 10); int randomNumber2 = rnd.Next(11, 200); string GuestValue = cryptor.Encrypt(DateTime.Now.ToString() + randomNumber1 + randomNumber2, true); GuestCookie.Value = GuestValue; GuestCookie.Expires = DateTime.Now.AddHours(1); Response.Cookies.Add(GuestCookie); try { CookieCart cookieCart = new CookieCart(); cookieCart.CookieId = GuestValue; cookieCart.ProductsInCart = new List <ProductInCart>(); cookieCart.WholePrice = 0; CookieCartService.CreateCookieCart(cookieCart); CookieCartService.SaveCookieCart(); var product = cookieCart.ProductsInCart .Where(a => a.ProductId == productInCart.ProductId && a.OrderId == null) .FirstOrDefault(); if (product != null) { product.Quantity += productInCart.Quantity; product.SubtotalForProduct += productInCart.SubtotalForProduct; } else { cookieCart.ProductsInCart.Add(productInCart); } CookieCartService.UpdateCookieCart(cookieCart); CookieCartService.SaveCookieCart(); } catch (Exception E) { throw new Exception(E.ToString()); } } else { try { CookieCart cookieCart = CookieCartService.GetCookieCart(Request.Cookies["Guest"].Value); var product = cookieCart.ProductsInCart .Where(a => a.ProductId == productInCart.ProductId && a.OrderId == null) .FirstOrDefault(); if (product != null) { product.Quantity += productInCart.Quantity; product.SubtotalForProduct += productInCart.SubtotalForProduct; } else { cookieCart.ProductsInCart.Add(productInCart); } CookieCartService.UpdateCookieCart(cookieCart); CookieCartService.SaveCookieCart(); } catch (Exception E) { throw new Exception(E.ToString()); } } return(RedirectToAction("Cart", "Manage")); } return(RedirectToAction("Cart", "Manage")); }
public void UpdateCookieCart(CookieCart CookieCart) { CookieCartsRepository.Update(CookieCart); }