Exemplo n.º 1
0
        public async Task <IActionResult> PutGame(int id, Game game)
        {
            if (id != game.Id)
            {
                return(BadRequest());
            }

            _context.Entry(game).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GameExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task <ActionResult <int> > PostPlayer([FromBody] JObject data)
        {
            var    currDateTime = DateTime.Now;
            Player player       = data["player"].ToObject <Player>();
            var    playerName   = player.Name;
            int    rounds       = data["rounds"].ToObject <int>();

            if (ModelState.IsValid)
            {
                try
                {
                    player = PlayerExists(playerName);
                    if (player == null)
                    {
                        player = new Player
                        {
                            Name = playerName
                        };
                        _context.Players.Add(player);
                        await _context.SaveChangesAsync();
                    }

                    var game = new Game
                    {
                        Datetime = currDateTime,
                        Rounds   = rounds,
                        Winner   = player
                    };
                    _context.Games.Add(game);

                    var log = new Log
                    {
                        IsError     = 0,
                        Datetime    = currDateTime,
                        Description = $"Player {playerName} has won a new game."
                    };
                    _context.Logs.Add(log);
                    await _context.SaveChangesAsync();

                    return(1);
                }
                catch (Exception exc)
                {
                    var log = new Log
                    {
                        IsError     = 1,
                        Datetime    = currDateTime,
                        Description = $"The following error was thrown while trying to register a new win to {playerName}: " + exc.Message
                    };
                    _context.Logs.Add(log);
                    await _context.SaveChangesAsync();

                    return(0);
                }
            }
            return(-1);
        }