예제 #1
0
        public async Task Post(int id)
        {
            var userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (userId == null)
            {
                var value      = Request.Cookies[KEY];
                var collection = string.IsNullOrEmpty(value) ? new List <CartHolder>() :
                                 (await _json.DeserializeAsync <IList <CartHolder> >(value));
                var index = collection.FindIndex(f => f.Id == id);
                if (index != -1)
                {
                    collection[index].Amount++;
                }
                else
                {
                    collection.Add(new CartHolder
                    {
                        Id     = id,
                        Amount = 1
                    });
                }

                Response.Cookies.Append(KEY, _json.Serialize(collection),
                                        new CookieOptions
                {
                    Expires = DateTime.Now.AddDays(5)
                });
            }
            else
            {
                await _cart.Check(id, userId);
            }
        }
예제 #2
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            var userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (userId == null)
            {
                var value      = Request.Cookies[KEY];
                var collection = string.IsNullOrEmpty(value) ? new List <CartHolder>() :
                                 (await _json.DeserializeAsync <IReadOnlyList <CartHolder> >(value));
                return(View("Default", await _header.GetViewModel(collection)));
            }
            else
            {
                return(View("Default", await _header.GetViewModel(userId)));
            }
        }