Ejemplo n.º 1
0
        public async Task <ActionResult <GameResource> > CreateGame([FromBody] SaveGameResource saveGameResource)
        {
            var validator        = new SaveGameResourceValidator();
            var validationResult = await validator.ValidateAsync(saveGameResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var gameToCreate = _mapper.Map <SaveGameResource, Game>(saveGameResource);
            var newGame      = await _gameService.CreateGame(gameToCreate);

            var gameResource = _mapper.Map <Game, GameResource>(newGame);

            return(Ok(gameResource));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <GameResource> > CreateGame([FromBody] SaveGameResource saveGameResource)
        {
            var validator        = new SaveGameResourceValidator();
            var validationResult = await validator.ValidateAsync(saveGameResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var gameToCreate = _mapper.Map <SaveGameResource, Game>(saveGameResource);

            var newGame = await _gameService.CreateGame(gameToCreate);

            await _gameService.SetGameGenres(newGame, saveGameResource.GenreIds);

            var game = await _gameService.GetGameByRef(newGame.Ref);

            var musicResource = _mapper.Map <Game, GameResource>(game);

            return(Ok(musicResource));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <GameResource> > UpdateGame(int id, [FromBody] SaveGameResource saveGameResource)
        {
            var validator        = new SaveGameResourceValidator();
            var validationResult = await validator.ValidateAsync(saveGameResource);

            var requestIsInvalid = id == 0 || !validationResult.IsValid;

            if (requestIsInvalid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var game = _mapper.Map <SaveGameResource, Game>(saveGameResource);

            await _gameService.UpdateGame(id, game);

            var updatedGame = await _gameService.GetGameById(id);

            var updatedGameResource = _mapper.Map <Game, GameResource>(updatedGame);

            return(Ok(updatedGameResource));
        }