Esempio n. 1
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"));
            }
        }
Esempio n. 2
0
        public IActionResult AddPokemon([FromBody] PokemonForCreationDto pokemonForCreateDto)
        {
            var pokemon = _mapper.Map <Core.Entities.Pokemon>(pokemonForCreateDto);

            _pokemonRepository.AddPokemon(pokemon);

            var pokemonDto         = _mapper.Map <PokemonDto>(pokemon);
            var genericApiResponse = new GenericApiResponse <Core.Entities.Pokemon> {
                Data = pokemon
            };

            return(Created($"{ControllerRoute}/{nameof(GetPokemon)}", genericApiResponse));
        }
Esempio n. 3
0
        public async Task <Pokemon> AddPokemon(PokemonDTO pokemonDTO)
        {
            // Add a Pokemon to database

            // Check if Pokemon with entry and gen already exists
            PokemonDTO checkExists = await GetPokemon_ByEntryAndGen(pokemonDTO.PokedexEntry, pokemonDTO.Generation);

            if (checkExists != null)
            {
                return(null);
            }

            // Add Pokemon
            Pokemon pokemon = await CreatePokemonDTO_FromPokemon(pokemonDTO);

            pokemon = await _pokemonRepository.AddPokemon(pokemon);

            return(pokemon);
        }