public ActionResult Summary()
        {
            if (Session["UserId"] == null)
            {
                Session["UserId"] = _userSessionService.NewUser();
            }

            var previousOrdersResponse =
                _basketService.GetPreviousOrders(_userSessionService.GetUserByUserToken(Session["UserId"].ToString()));

            if (previousOrdersResponse.HasError)
            {
                Redirect("/Basket");
            }

            previousOrdersResponse.BasketDetails.Reverse();
            var response = new BasketControllerSummaryData
            {
                BasketDetails = previousOrdersResponse.BasketDetails.FirstOrDefault(),
                Total         = _userSessionService.GetBasketTotalForUser(Session["UserId"].ToString()),
                LoggedIn      = _userSessionService.IsLoggedIn(Session["UserId"].ToString())
            };

            return(View(response));
        }
Beispiel #2
0
        public BasketCheckoutResponse Checkout(DeliveryType delivery, string voucher, string userId, OrderStatus orderStatus)
        {
            var response = new BasketCheckoutResponse();

            if (!_userSessionService.IsLoggedIn(userId))
            {
                response.AddError(new Error
                {
                    Code        = ErrorCodes.UserNotLoggedIn,
                    UserMessage = "You have to be logged in to complete your order"
                });
                return(response);
            }

            if (delivery == DeliveryType.Unknown)
            {
                response.AddError(new Error
                {
                    Code        = ErrorCodes.DeliveryTypeUnknown,
                    UserMessage = "Delivery type not specified."
                });
                return(response);
            }

            if (orderStatus == OrderStatus.Unknown)
            {
                response.AddError(new Error
                {
                    Code        = ErrorCodes.OrderStatusUnkown,
                    UserMessage = "Order status not specified."
                });
                return(response);
            }

            var userBasket = _userSessionService.GetBasketForUser(userId);

            if (!voucher.IsEmpty())
            {
                var verifyVoucherResponse = _voucherService.Verify(userBasket, new List <DeliveryType> {
                    delivery
                }, voucher);

                if (!verifyVoucherResponse.HasError)
                {
                    userBasket.Total = verifyVoucherResponse.Total;
                }
            }

            var orderRequest = new SaveOrderRequest
            {
                UserId       = _userSessionService.GetUserByUserToken(userId),
                DeliveryType = delivery.ToString(),
                Voucher      = voucher,
                GrandTotal   = userBasket.Total.InPence,
                Status       = orderStatus.ToString(),
                Orders       = userBasket.Items.ConvertAll(x => new Order
                {
                    PizzaId         = x.Pizza.Id,
                    SizeId          = x.Size.Id,
                    ExtraToppingIds = x.ExtraToppings.ConvertAll(y => y.Id),
                    SubTotal        = x.Total.InPence
                })
            };
            var saveOrderResponse = _orderRepository.SaveOrder(orderRequest);

            if (saveOrderResponse.HasError)
            {
                response.AddError(saveOrderResponse.Error);
            }

            _userSessionService.ClearBasketForUser(userId);

            return(response);
        }