Skip to content
Snippets Groups Projects
Commit 62fb5d5d authored by CarimPopa's avatar CarimPopa
Browse files

Method for handling POST requests is created.

parent 72426d0d
Branches newFeature1
No related tags found
No related merge requests found
......@@ -9,11 +9,62 @@ public class PokemonEntity extends NamedEntity {
public int pokemonType;
public int trainerId;
public PokemonEntity(String name, int pokemonType, int trainerId) {
public PokemonEntity(String name, int pokemonType, int trainerId, int id) {
super();
this.name = name;
this.pokemonType = pokemonType;
this.trainerId = trainerId;
this.id = id;
}
public PokemonEntity() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastUpDate() {
return lastUpDate;
}
public void setLastUpDate(String lastUpDate) {
this.lastUpDate = lastUpDate;
}
public int getPokemonType() {
return pokemonType;
}
public void setPokemonType(int pokemonType) {
this.pokemonType = pokemonType;
}
public int getTrainerId() {
return trainerId;
}
public void setTrainerId(int trainerId) {
this.trainerId = trainerId;
}
}
......@@ -16,8 +16,7 @@ public class Pokemon {
@GET
@Produces(MediaType.APPLICATION_JSON)
public ResourceCollection<PokemonEntity> getAllPokemon() {
PokemonEntity[] pokemons = new PokemonEntity[1];
pokemons[0] = new PokemonEntity("Pikachu", 513, 1);
PokemonEntity[] pokemons = PokemonHelperClass.getPokemons();
return new ResourceCollection<>(pokemons, 1, 1, 1);
}
......@@ -45,4 +44,25 @@ public class Pokemon {
}
}
@PUT
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public ResourceCollection<PokemonEntity> updatePokemonById(@PathParam("id") int id, PokemonEntity toReplace) {
PokemonEntity pokemonToUpdate = PokemonHelperClass.getPokemon(id);
if (pokemonToUpdate == null || toReplace.id != pokemonToUpdate.id) {
throw new NotFoundException();
} else {
PokemonHelperClass.createPokemon(toReplace);
}
return new ResourceCollection<>(new PokemonEntity[]{PokemonHelperClass.getPokemon(id)}, 1, 1, 1);
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ResourceCollection<PokemonEntity> addPokemon(PokemonEntity toAdd) {
PokemonHelperClass.createPokemon(toAdd);
return new ResourceCollection<>(new PokemonEntity[]{PokemonHelperClass.getPokemon(toAdd.id)}, 1, 1, 1);
}
}
......@@ -9,8 +9,8 @@ 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));
pokemonMap.put(0, new PokemonEntity("Pikachu", 513, 1, 0));
pokemonMap.put(1, new PokemonEntity("Pikipek", 514, 2, 1));
}
public static PokemonEntity getPokemon(int id) {
......@@ -21,4 +21,16 @@ public class PokemonHelperClass {
pokemonMap.remove(id);
}
public static void createPokemon(PokemonEntity pokemon) {
if(pokemon.id == 0) {
pokemon.setId(pokemonMap.size());
pokemonMap.put(pokemonMap.size(), pokemon);
}
pokemonMap.put(pokemon.id, pokemon);
}
public static PokemonEntity[] getPokemons() {
return pokemonMap.values().toArray(new PokemonEntity[0]);
}
}
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