コード例 #1
0
        public async Task <ActionResult <PlayerModel> > UpdatePlayer([FromBody] PlayerModel dto, [FromRoute] string username)
        {
            if (dto == null)
            {
                return(BadRequest(ResponseMessages.PutNullObjectErrorMessage));
            }
            try
            {
                Player player = await _service.GetPlayerByUsernameAsync(username);

                if (player == null)
                {
                    return(BadRequest(ResponseMessages.PlayerDoesNotExistMessage(username)));
                }

                player = await _service.UpdatePlayerAsync(player, dto);

                PlayerModel playerModel = (PlayerModel)player;
                return(Ok(playerModel));
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, ResponseMessages.InternalServerErrorMessage));
            }
        }
コード例 #2
0
        public async Task <ActionResult> RemovePlayer([FromRoute] string username)
        {
            try
            {
                var oldPlayer = await _service.GetPlayerByUsernameAsync(username);

                if (oldPlayer == null)
                {
                    return(BadRequest(ResponseMessages.PlayerDoesNotExistMessage(username)));
                }

                await _service.RemovePlayerAsync(oldPlayer);

                return(Ok($"Player '{oldPlayer.Username}' was removed from the database"));
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, ResponseMessages.InternalServerErrorMessage));
            }
        }