Exemple #1
0
        public IHttpResponse Buy(IHttpRequest req)
        {
            int gameToBuyId = int.Parse(req.UrlParameters["id"]);

            if (!req.Session.IsAuthenticated())
            {
                return(new RedirectResponse("/login"));
            }

            string userEmail = req.Session.Get <LoginViewModel>(SessionStore.CurrentUserKey).Email;

            if (!req.Session.Contains(SessionStore.ShoppingCartKey))
            {
                req.Session.Add(SessionStore.ShoppingCartKey, new CartViewModel(this.mapper));
            }

            CartViewModel cart = req.Session.Get <CartViewModel>(SessionStore.ShoppingCartKey);

            List <int> gamesIds = cart.GetGamesIds();

            if (gamesIds.Contains(gameToBuyId) ||
                this.gameService.GameIsOwnedByYou(gameToBuyId, userEmail))
            {
                this.ViewData["show-error"] = AppConstants.PossesedGame;

                return(new RedirectResponse("/"));
            }


            cart.AddGame(gameToBuyId);

            this.ViewData["buy-success"] = AppConstants.SuccessfullOrder;

            return(new RedirectResponse("/"));
        }