/*********************************************************
         #A This action is called if a user clicks any of the  "Buy n books" dropdown buttons.
         #B To isolate the CheckoutCookieService from the ASP.NET Core's HttpContext we have designed a BasketCookie that provides an interface that can be used in a unit test
         #C The CheckoutCookieService is created, with the content of the current basket Cookie, or null if no basket Cookie currently exists
         #D This method adds a new OrderLineItem entry to the CheckoutCookieService list of OrderLineItems
         #E This statement encodes the list of OrderLineItems into a string, ready to go out in the updated basket Cookie
         #F This method sets the basket cookie content to the encoded string
         #G Finally we go to the Checkout page which shows the user the books, quantities and prices of the books they have in their basket
         * ******************************************************/


        public IActionResult DeleteLineItem(int lineNum)
        {
            var cookie  = new BasketCookie(HttpContext.Request.Cookies, HttpContext.Response.Cookies);
            var service = new CheckoutCookieService(cookie.GetValue());

            service.DeleteLineItem(lineNum);
            cookie.AddOrUpdateCookie(service.EncodeForCookie());
            SetupTraceInfo();
            return(RedirectToAction("Index"));
        }
        public IActionResult Buy(OrderLineItem itemToBuy)
        {
            var cookie = new BasketCookie(
                HttpContext.Request.Cookies,
                HttpContext.Response.Cookies);
            var service = new CheckoutCookieService(cookie.GetValue());

            service.AddLineItem(itemToBuy);
            var cookieOutString = service.EncodeForCookie();

            cookie.AddOrUpdateCookie(cookieOutString);
            SetupTraceInfo(); //Remove this when shown in book listing
            return(RedirectToAction("Index"));
        }
示例#3
0
        /// <summary>
        /// This creates the order and, if successful clears the cookie
        /// </summary>
        /// <returns>Returns the OrderId, or zero if errors</returns>
        public int PlaceOrder(bool acceptTAndCs)
        {
            var checkoutService = new CheckoutCookieService(
                _basketCookie.GetValue());

            var order = _runner.RunAction(
                new PlaceOrderInDto(acceptTAndCs,
                                    checkoutService.UserId, checkoutService.LineItems));

            if (_runner.HasErrors)
            {
                return(0);
            }

            //successful so clear the cookie line items
            checkoutService.ClearAllLineItems();
            _basketCookie.AddOrUpdateCookie(
                checkoutService.EncodeForCookie());

            return(order.OrderId);
        }
示例#4
0
        /// <summary>
        /// This creates the order and, if successful clears the cookie
        /// </summary>
        /// <returns>Returns the OrderId, or zero if errors</returns>
        public async Task <IStatusGeneric <int> > PlaceOrderAndClearBasketAsync(bool acceptTAndCs)
        {
            var status = new StatusGenericHandler <int>();

            var checkoutService = new CheckoutCookieService(
                _basketCookie.GetValue());

            var bizStatus = await _placeOrder.CreateOrderAndSaveAsync(
                new PlaceOrderInDto(acceptTAndCs, checkoutService.UserId, checkoutService.LineItems));


            if (status.CombineStatuses(bizStatus).HasErrors)
            {
                return(status);
            }

            //successful so clear the cookie line items
            checkoutService.ClearAllLineItems();
            _basketCookie.AddOrUpdateCookie(
                checkoutService.EncodeForCookie());

            return(status.SetResult(bizStatus.Result.OrderId));
        }