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")); } }
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)); }
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); }