public async Task <IActionResult> PutPlayer([FromRoute] Guid id, [FromBody] UpdatePlayerRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Find our player. PlayerDataModel player = await _context.Players.FindAsync(id); if (player == null) { return(NotFound()); } // Update properties. player.Name = request.Name; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { // If we hit a concurrency exception and the player no longer exists, // It must have been deleted while we were working. if (!_context.Players.Any(e => e.Id == id)) { return(NotFound()); } throw; } return(NoContent()); }
public async Task <IActionResult> CreateGame([FromBody] CreateGameRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } PlayerDataModel player = await _context.Players.FindAsync(request.PlayerId); if (player == null) { return(BadRequest($"No player with Id {request.PlayerId} exists.")); } // Create new Game entry in DB. GameDataModel gameDataModel = new GameDataModel { Player = player, CreationDate = DateTime.Now, Frames = new List <FrameDataModel>() }; _context.Games.Add(gameDataModel); await _context.SaveChangesAsync(); // Create first Frame entry for the new Game. FrameDataModel firstFrameDataModel = new FrameDataModel { CreationDate = DateTime.Now, GameId = gameDataModel.Id, FrameNumber = 1 }; _context.Frames.Add(firstFrameDataModel); await _context.SaveChangesAsync(); // Update new Game entry with the new Frame and throw. gameDataModel.Frames.Add(firstFrameDataModel); gameDataModel.CurrentFrameId = gameDataModel.Frames.First().Id; gameDataModel.CurrentThrow = 1; await _context.SaveChangesAsync(); return(CreatedAtAction("GetGame", new { gameId = gameDataModel.Id }, ViewModelFactory.CreateGameViewModel(gameDataModel))); }