コード例 #1
0
        public ActionResult Pay()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }
            var customer     = db.Customers.FirstOrDefault(x => x.Login == User.Identity.Name);
            var currentOrder = db.Orders.Include(o => o.OrderPositions.Select(op => op.Game)).FirstOrDefault(x => x.CustomerId == customer.Id && x.Current);
            var rand         = new Random();

            foreach (var orderPosition in currentOrder.OrderPositions)
            {
                var purchasedGame = new PurchasedGame();
                purchasedGame.Name   = orderPosition.Game.Name;
                purchasedGame.Price  = orderPosition.Game.Price;
                purchasedGame.GameId = orderPosition.Game.Id;

                purchasedGame.Key        = rand.Next(1000, 9999).ToString() + '-' + rand.Next(1000, 9999).ToString() + '-' + rand.Next(1000, 9999).ToString();
                purchasedGame.Time       = DateTime.Now;
                purchasedGame.CustomerId = customer.Id;
                db.PurchasedGames.Add(purchasedGame);
            }
            currentOrder.Current = false;
            db.SaveChanges();

            setGamesInCart(0); // обнуление количества игр в корзине в куки

            return(RedirectToAction("Profile", "Account"));
        }
コード例 #2
0
        public async Task <ActionResult <GameDto> > AddGameToPurchased(int gameId)
        {
            if (!GameExists(gameId))
            {
                return(BadRequest("Game does not exist"));
            }

            var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var purchasedGame = await _context.PurchasedGames.FirstOrDefaultAsync(pg => pg.GameId == gameId && pg.UserId == userId);

            if (purchasedGame != null)
            {
                return(BadRequest("You have already purchased this game"));
            }

            purchasedGame = new PurchasedGame {
                GameId    = gameId,
                UserId    = userId,
                SerialKey = GenerateKey()
            };

            var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == userId);

            var game = await _context.Games.FirstOrDefaultAsync(g => g.Id == gameId);

            user.Budget = user.Budget - game.Price;

            if (user.Budget < 0)
            {
                return(BadRequest("You have insufficient funds"));
            }

            await _context.PurchasedGames.AddAsync(purchasedGame);

            if (await _context.SaveChangesAsync() > 0)
            {
                return(Ok());
            }

            return(BadRequest("Failed to add game to purchased games"));
        }