Exemple #1
0
        public IActionResult AddItemToCart(int stickId, int quantity)
        {
            Log.Information($"About to add {quantity} stick(s) with Id: {stickId}, to cart");
            Stick stick = new Stick();

            customer = HttpContext.Session.GetObject <Customer>("Customer");
            if (customer == null)
            {
                Log.Error("Customer session not found");
                return(RedirectToAction("LoginC", "Home"));
            }
            if (ModelState.IsValid)
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(url);
                    var response = client.GetAsync($"stick/get?Id={stickId}");
                    response.Wait();
                    var result = response.Result;
                    if (result.IsSuccessStatusCode)
                    {
                        var jsonString = result.Content.ReadAsStringAsync();
                        jsonString.Wait();
                        var prod = JsonConvert.DeserializeObject <Stick>(jsonString.Result);
                        stick = prod;
                        Log.Information($"Got Stick {stick.Id}");
                    }
                }
                CartItem cartItem = new CartItem();
                cartItem.stickId         = stickId;
                cartItem.quantity        = quantity;
                customer.cart.totalCost += (stick.cost * quantity);
                customer.cart.cartItems.Add(cartItem);
                HttpContext.Session.SetObject("Customer", customer);
                Log.Information($"Updated {customer.email}'s session");
                alertServices.Information($"{stick.name} added to the cart.", true);
                return(View("GetInventory", customer.location.inventory));
            }
            Log.Error($"State not valid {ModelState}");
            return(RedirectToAction("GetInventory"));
        }