Skip to content
Snippets Groups Projects
Commit 72426d0d authored by CarimPopa's avatar CarimPopa
Browse files

Method for handling DELETE request is implemented.

Added a helper static class to store data about pokemons.
parent 19a9211f
Branches newFeature1
No related tags found
No related merge requests found
......@@ -11,6 +11,8 @@ import nl.utwente.mod4.pokemon.model.ResourceCollection;
@Path("/pokemon")
public class Pokemon {
public int counter = 0;
@GET
@Produces(MediaType.APPLICATION_JSON)
public ResourceCollection<PokemonEntity> getAllPokemon() {
......@@ -23,17 +25,24 @@ public class Pokemon {
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public ResourceCollection<PokemonEntity> getPokemonById(@PathParam("id") int id) {
HashMap<Integer, PokemonEntity> map = new HashMap<>();
map.put(1, new PokemonEntity("Pikachu", 513, 1));
map.put(2, new PokemonEntity("Pikipek", 514, 2));
PokemonEntity requestedPokemon = map.get(id);
if(requestedPokemon == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return new ResourceCollection<>(new PokemonEntity[]{requestedPokemon}, 1, 1, 1);
PokemonEntity requestedPokemon = PokemonHelperClass.getPokemon(id);
if (requestedPokemon == null) {
throw new NotFoundException();
}
return new ResourceCollection<>(new PokemonEntity[]{requestedPokemon}, 1, 1, 1);
}
@DELETE
@Path("/{id}")
public void deletePokemonById(@PathParam("id") int id) {
PokemonEntity pokemonToDelete = PokemonHelperClass.getPokemon(id);
if (pokemonToDelete == null) {
throw new WebApplicationException(Response.Status.BAD_REQUEST);
} else {
PokemonHelperClass.deletePokemon(id);
}
}
}
package nl.utwente.mod4.pokemon.routes;
import nl.utwente.mod4.pokemon.model.PokemonEntity;
import java.util.*;
public class PokemonHelperClass {
public static Map<Integer, PokemonEntity> pokemonMap = new HashMap<Integer, PokemonEntity>();
static {
pokemonMap.put(1, new PokemonEntity("Pikachu", 513, 1));
pokemonMap.put(2, new PokemonEntity("Pikipek", 514, 2));
}
public static PokemonEntity getPokemon(int id) {
return pokemonMap.get(id);
}
public static void deletePokemon(int id) {
pokemonMap.remove(id);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment