Example #1
0
        public async Task <ActionResult <Highscore> > AddHighscore(Highscore highscore)
        {
            if (highscore.Score < 0)
            {
                return(BadRequest("Not a highScore"));
            }

            var highscores = await _context.Highscores.OrderByDescending(a => a.Score).ToListAsync();

            if (highscores.Count < 10)
            {
                _context.Highscores.Add(highscore);
                await _context.SaveChangesAsync();

                return(Ok(highscore));
            }
            else
            {
                for (int i = 0; i < highscores.Count; i++)
                {
                    if (highscore.Score > highscores[i].Score)
                    {
                        _context.Remove(highscores.Last());
                        _context.Add(highscore);
                        await _context.SaveChangesAsync();

                        return(Ok(highscore));
                    }
                }
            }
            return(BadRequest("Not a highScore"));
        }
Example #2
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Create([Bind("Name,Score,Level,Date")] Highscore highscore)
        {
            if (ModelState.IsValid && Request.Headers["Token"] == "12414141414")
            {
                _context.Add(highscore);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(highscore));
        }