Esempio n. 1
0
        public Pokemon GetPokemonByName(string name)
        {
            var pokemon = _repository.GetPokemonByName(name);

            if (pokemon == null)
            {
                throw new NotFoundException($"Pokemon with name {name} not found.");
            }
            return(pokemon);
        }
Esempio n. 2
0
        public async Task <ActionResult <Pokemon> > CreatePokemon(Pokemon pokemon)
        {
            try
            {
                if (pokemon == null)
                {
                    return(BadRequest());
                }


                var pokemonByName = await _pokemonRepository.GetPokemonByName(pokemon.Name);

                if (pokemonByName != null)
                {
                    ModelState.AddModelError("name", "Pokemon name already in use");
                    return(BadRequest(ModelState));
                }

                var createdPokemon = await _pokemonRepository.AddPokemon(pokemon);

                return(CreatedAtAction(nameof(GetPokemon), new { id = 1 }, createdPokemon));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  "Error retrieving data from the database"));
            }
        }