public async Task <ActionResult <Pokemon> > AddPokemon(PokemonDTO pokemonDTO) { try{ // Clean + verify pokemonDTO = PokemonHelper.CleanupPokemonDTO(pokemonDTO); var verify = await _pokemonService.VerifyPokemonDTO(pokemonDTO); if (verify.Item1 == false) { return(new BadRequestObjectResult($"The provided values cannot be used to create a Pokemon - {verify.Item2}")); } // Add pokemon var result = await _pokemonService.AddPokemon(pokemonDTO); if (result == null) { return(new BadRequestObjectResult("Pokemon with given entry and generation already exists in database")); } _logger.LogInformation($"Pokemon was added - {result}"); return(new OkObjectResult(result)); } catch (Exception ex) { _logger.LogWarning($"Warning {ex.Message}"); return(new StatusCodeResult(500)); } }
private async Task AddPokemon_Ok(PokemonDTO pokemonDTO) { string json = JsonConvert.SerializeObject(pokemonDTO); StringContent contentSend = new StringContent(json, Encoding.UTF8, "application/json"); var response = await Client.PostAsync("/api/pokemon", contentSend); response.StatusCode.Should().Be(HttpStatusCode.OK); pokemonDTO = PokemonHelper.CleanupPokemonDTO(pokemonDTO); var contentReceived = await response.Content.ReadAsStringAsync(); var body = JsonConvert.DeserializeObject <Pokemon>(contentReceived); Assert.NotNull(body); Assert.Equal(pokemonDTO.Name, body.Name); Assert.Equal(pokemonDTO.PokedexEntry, body.PokedexEntry); Assert.Equal(pokemonDTO.Generation, body.Generation); }
public void UnitTest_CleanupPokemonDTO() { var testDto = new PokemonDTO() { Name = "Villager #27", Types = new List <string> { "Gr ass " }, EggGroup = "Mine craft ", PokedexEntry = 111, Generation = 111, Classification = "Village Pokemon", Hp = 1, Attack = 1, Defense = 1, SpAtk = 1, SpDef = 1, Speed = 1 }; var result = PokemonHelper.CleanupPokemonDTO(testDto); Assert.Equal("Villager#27", result.Name); Assert.Equal("Minecraft", result.EggGroup); Assert.Equal(new List <string> { "Grass" }, result.Types); }