public ShoppingCart GetShoppingCart() { ShoppingCart cart; if (SignInManager.IsSignedIn(Context.User)) { string cartId = UserManager.GetUserId(Context.User); cart = _repo.GetCartById(cartId); } else { try { string requestCookie = Context.Request.Cookies[_cartCookieName]; cart = JsonConvert.DeserializeObject <ShoppingCart>(requestCookie); } catch (Exception) { cart = new ShoppingCart() { Id = Guid.NewGuid().ToString(), CartProducts = new List <CartProduct>() }; Context.Response.Cookies .Append(_cartCookieName, JsonConvert.SerializeObject(cart)); } } return(cart); }
public IActionResult ShopList(ShopListOptions options) { ShoppingCart cart; if (SignInManager.IsSignedIn(User)) { string cartId = UserManager.GetUserId(User); cart = Repository.GetCartById(cartId); } else { try { string requestCookie = HttpContext.Request.Cookies[_cartCookieName]; cart = JsonConvert.DeserializeObject <ShoppingCart>(requestCookie); } catch (Exception) { cart = new ShoppingCart() { Id = Guid.NewGuid().ToString(), CartProducts = new List <CartProduct>() }; HttpContext.Response.Cookies .Append(_cartCookieName, JsonConvert.SerializeObject(cart)); } } var model = new ShopListModel(); int total; model.ShopList = Repository.GetShopList(options, cart, out total); model.Brands = Repository.GetAllBrands(); model.TotalPage = total / (options.PageSize ?? 20); ViewBag.Options = options; ViewBag.CountCartItems = cart.CartProducts.Count(); return(View(model)); }