Exemplo n.º 1
0
        public IHttpActionResult PostGame(Game game)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            game.Id = Guid.NewGuid();

            //Match the players in the database
            Player winner = db.Players.FirstOrDefault(p => p.Id == game.Winner.Id);
            int winnerRating = int.Parse(String.Copy(winner.Rating.ToString()));
            Player loser = db.Players.FirstOrDefault(p => p.Id == game.Loser.Id);

            //Adjust player stats
            Player scoredWinner = Player.scoreGame(winner, loser.Rating, true);
            Player scoredLoser = Player.scoreGame(loser, winnerRating, false);

            //Update players for the response
            game.Winner = scoredWinner;
            game.Loser = scoredLoser;

            //Update database entries
            db.Games.Add(game);
            db.Entry(scoredWinner).State = EntityState.Modified;
            db.Entry(scoredLoser).State = EntityState.Modified;

            //Save changes
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (GameExists(game.Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = game.Id }, game);
        }
Exemplo n.º 2
0
        public IHttpActionResult PutGame(Guid id, Game game)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != game.Id)
            {
                return BadRequest();
            }

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

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GameExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }