Example #1
0
        public async Task <ActionResult> PostScore(string nickname, string game, [FromBody] int points, CancellationToken cancellationToken)
        {
            // Lookup gamer based on nickname
            var gamer = await context.Gamers
                        .FirstOrDefaultAsync(g => g.Nickname.ToLower() == nickname.ToLower(), cancellationToken);

            if (gamer == null)
            {
                return(BadRequest());
            }

            // Find highest score for game
            var score = await context.Scores
                        .Where(s => s.Game == game && s.Gamer == gamer)
                        .OrderByDescending(s => s.Points)
                        .FirstOrDefaultAsync(cancellationToken);

            if (score == null)
            {
                score = new Score()
                {
                    Gamer = gamer, Points = points, Game = game
                };
                await context.Scores.AddAsync(score, cancellationToken);
            }
            else
            {
                if (score.Points > points)
                {
                    return(BadRequest());
                }

                score.Points = points;
            }
            await context.SaveChangesAsync(cancellationToken);

            mailService.SendMail($"New high score of {score.Points} for game '{score.Game}'");

            return(Ok());
        }
Example #2
0
        public async Task PostScore(string nickname, string game, [FromBody] int points)
        {
            // Lookup gamer based on nickname
            Gamer gamer = await context.Gamers
                          .FirstOrDefaultAsync(g => g.Nickname.ToLower() == nickname.ToLower())
                          .ConfigureAwait(false);

            if (gamer == null)
            {
                return;
            }

            // Find highest score for game
            var score = await context.Scores
                        .Where(s => s.Game == game && s.Gamer == gamer)
                        .OrderByDescending(s => s.Points)
                        .FirstOrDefaultAsync()
                        .ConfigureAwait(false);

            if (score == null)
            {
                score = new Score()
                {
                    Gamer = gamer, Points = points, Game = game
                };
                await context.Scores.AddAsync(score);
            }
            else
            {
                if (score.Points > points)
                {
                    return;
                }
                score.Points = points;
            }
            await context.SaveChangesAsync().ConfigureAwait(false);

            mailService.SendMail($"New high score of {score.Points} for game '{score.Game}'");
        }