Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • s3206149/mod4-wp-2023-2024-pokemon
  • s3281744/mod4-wp-2023-2024-pokemon
  • s3128989/mod4-wp-2023-2024-pokemon
  • s3263797/m-4-pokemon
  • s3203255/mod4-wp-2023-2024-pokemon
  • s2854961/mod-4-wp-2023-2024-pokemons
  • s3192016/pokemon-practical-sessions
  • s3126080/mod4-wp-2023-2024-pokemon
  • s3165442/poke-mon
  • s3187152/mod4-wp-2023-2024-pokemon
  • s3209105/mod4-wp-2023-2024-pokemon
  • s3362779/mod4-wp-2023-2024-pokemon
  • s3173011/mod-4-wp-3-2023-2024-pokemon
  • s3174506/mod4-wp-2023-2024-pokemon
  • s3216926/mod-4-wp-2023-2024-pokemon-2nd
  • s3211770/mod4-wp-2023-2024-pokemon
  • s2936011/mod4-wp-2023-2024-pokemon
  • s3234312/mod4-wp-2023-2024-pokemon
  • s3362264/mod4-wp-2023-2024-pokemon
  • s3204766/mod4-wp-2023-2024-pokemon
  • s3363775/mod4-wp-2023-2024-pokemon
  • s3142906/mod4-wp-2023-2024-pokemon
  • s3298892/mod4-wp-2023-2024-pokemon
  • s3125866/mod-4-wp-2023-2024-pokemon-update
  • s2900556/mod-4-wp-2023-2024-pokemontut-3
  • s3196801/mod4-wp-2023-2024-pokemon
  • s2507919/mod4-wp-2023-2024-pokemon
  • Marius/mod-4-pokemon
  • s2948486/mod4-wp-2023-2024-pokemon
  • s3193748/mod4-wp-2023-2024-pokemon
  • s3199800/mod4-wp-2023-2024-pokemon
  • s3152340/mod4-wp-2023-2024-pokemon
  • s3194108/mod4-wp-2023-2024-pokemon
  • s3364518/mod4-wp-2023-2024-pokemon
  • s3200205/mod4-wp-2023-2024-pokemon
  • s3187667/pokemon-server
  • s3259633/mod4-wp-2023-2024-pokemon
  • s3137279/mod4-wp-2023-2024-pokemon
  • s3213552/mod4-wp-2023-2024-pokemon
  • s3200779/mod4-wp-2023-2024-pokemon
  • s3148157/mod4-wp-2023-2024-pokemon
  • s3196372/mod4-wp-2023-2024-pokemon
  • s3126714/mod4-wp-2023-2024-pokemon
  • s3286029/pracitcal-2
  • s3262499/mod4-wp-2023-2024-pokemon
  • s3290387/mod4-wp-2023-2024-pokemon
  • s3202798/mod4-wp-2023-2024-pokemon
  • s3210715/mod4-wp-2023-2024-pokemon
  • s3198308/mod4-wp-2023-2024-pokemon
  • s3211614/mod4-wp-2023-2024-pokemon
  • s3193691/mod4-wp-2023-2024-pokemon
  • s3273733/mod4-wp-2023-2024-pokemon
  • s3210898/mod4-wp-2023-2024-pokemon
  • s3133931/mod4-wp-2023-2024-pokemon
  • s3199479/mod4-wp-2023-2024-pokemon
  • s3190285/mod4-wp-2023-2024-pokemon
  • s3002896/mod4-wp-2023-2024-pokemon
  • s3166244/mod4-wp-2023-2024-pokemon
  • s3165582/mod4-wp-2023-2024-pokemon
  • s3147851/mod-4-wp-2023-2024-pokemon
  • web-programming-sessions/mod4-wp-2023-2024-pokemon
  • s3309630/mod4-wp-2023-2024-pokemon
  • s2914980/mod4-wp-2023-2024-pokemon
  • s3000885/mod4-wp-2023-2024-pokemon
  • s3209539/mod-4-pokemon-project
  • s3203573/kai-poke-mon
  • s3138127/mod-4-wp-2023-2024-pokemon-daniel
  • s3106586/mod4-wp-2023-2024-pokemon
  • s3183394/mod4-wp-2023-2024-pokemon
  • s3245748/mod4-wp-2023-2024-pokemon
  • s3252159/dm-mod-4-wp-2023-2024-pokemon
  • s2695154/mod4-wp-2023-2024-pokemon
  • s3171825/mod4-wp-2023-2024-pokemon
  • s3200183/mod4-wp-2023-2024-pokemon
  • s3000087/mod4-wp-2023-2024-pokemon
  • s3127168/mod4-wp-2023-2024-pokemon
  • s3137333/mod4-wp-2023-2024-pokemon
  • s3128830/mod4-wp-2023-2024-pokemon
  • s3102378/mod4-wp-2023-2024-pokemon
  • s3202224/mod4-wp-2023-2024-pokemon
  • s2590654/mod4-wp-2023-2024-pokemon
  • s3154882/mod4-wp-2023-2024-pokemon
  • s2958732/mod4-wp-2023-2024-pokemon
  • s3161293/mod4-wp-2023-2024-pokemon
  • s3133656/mod4-wp-2023-2024-pokemon
  • s3157385/mod-4-wp-2023-2024-pokemon-ali
  • s3192393/mod4-wp-2023-2024-pokemon
  • s3192016/mod4-wp-2023-2024-pokemon
  • s3211347/mod4-wp-2023-2024-pokemon
  • s3152790/mod4-wp-2023-2024-pokemon
  • s2959380/mod4-wp-2023-2024-pokemon
  • s3362906/mod4-wp-2023-2024-pokemon
  • s2964546/mod4-wp-2023-2024-pokemon
  • s3214915/mod4-wp-2023-2024-pokemon
  • s2219174/mod4-wp-2023-2024-pokemon
  • s3161595/mod4-wp-2023-2024-pokemon
  • s3207803/mod4-wp-2023-2024-pokemon
  • s3209334/mod4-wp-2023-2024-pokemon
  • s3191915/mod4-wp-2023-2024-pokemon
  • s3192253/mod4-wp-2023-2024-pokemon
  • s3137090/mod4-wp-2023-2024-pokemon
  • s3190536/mod4-wp-2023-2024-pokemon2
  • s3138585/mod4-wp-2023-2024-pokemon
  • s3186792/mod4-wp-2023-2024-pokemon
  • s2998491/mod4-wp-2023-2024-pokemon
  • s3145786/mod4-wp-2023-2024-pokemon
  • s3231046/mod4-wp-2023-2024-pokemon
  • s3125866/mod4-wp-2023-2024-pokemon
  • s3140458/mod4-wp-2023-2024-pokemon
  • s3133346/mod4-wp-2023-2024-pokemon
  • s3274519/mod4-wp-2023-2024-pokemon
  • s3216551/mod4-wp-2023-2024-pokemon
  • s3237478/mod4-wp-2023-2024-pokemon
  • s2985063/mod4-wp-2023-2024-pokemon
  • s3251942/mod4-wp-2023-2024-pokemon
  • s3305155/mod-4-wp-2023-2024-pokemon-david-sastre-madueno
  • s2675072/mod4-wp-2023-2024-pokemon
  • s3136027/mod4-wp-2023-2024-pokemon
  • s3186156/mod4-wp-2023-2024-pokemon
  • s3277305/mod4-wp-2023-2024-pokemon
  • s3242404/mod4-wp-2023-2024-pokemon
  • s3205967/mod4-wp-2023-2024-pokemon
  • s3173011/mod4-wp-2023-2024-pokemon
  • s3159736/mod4-wp-2023-2024-pokemon
  • s3207269/wplab-2
  • s3195945/mod4-wp-2023-2024-pokemon
  • s3151913/mod4-wp-2023-2024-pokemon
  • s3135136/mod4-wp-2023-2024-pokemon
  • s3216926/mod4-wp-2023-2024-pokemon
  • s3161218/mod4-wp-2023-2024-pokemon
  • s2921103/mod4-wp-2023-2024-pokemon
  • s3175561/mod4-wp-2023-2024-pokemon
  • SimeonNikolov/mod4-wp-2023-2024-pokemon
  • s3200329/mod4-wp-2023-2024-pokemon
  • s3148726/mod4-wp-2023-2024-pokemon
  • s2960796/mod4-wp-2023-2024-pokemon
  • s3098117/mod4-wp-2023-2024-pokemon
  • s3180794/mod4-wp-2023-2024-pokemon
  • s3179176/mod4-wp-2023-2024-pokemon
  • s3170268/mod4-wp-2023-2024-pokemon
  • s3126188/mod4-wp-2023-2024-pokemon
  • s3166449/pokemon-sarah-vonk
  • s3191567/mod4-wp-2023-2024-pokemon
  • s3271498/mod4-wp-2023-2024-pokemon
  • s3173089/mod4-wp-2023-2024-pokemon
  • s3172716/mod4-wp-2023-2024-pokemon
  • s3188981/mod4-wp-2023-2024-pokemon
  • s3184609/mod-4-lab-1-pokemon
  • s3144569/mod4-wp-2023-2024-pokemon
  • s3181340/poke-app-lab-06-05
  • s2900556/mod4-wp-2023-2024-pokemon
  • s3177858/mod4-wp-2023-2024-pokemon
  • s3209180/mod4-wp-2023-2024-pokemon
  • s3199495/mod4-wp-2023-2024-pokemon
  • s3163660/mod4-wp-2023-2024-pokemon
  • s3172708/mod4-wp-2023-2024-pokemon
  • s3206068/mod4-wp-2023-2024-pokemon
  • s3210774/mod4-wp-2023-2024-pokemon
  • s3200175/mod4-wp-2023-2024-pokemon
  • s3127141/mod4-wp-2023-2024-pokemon
  • s2995522/pokemon-practical-2-module-4
  • s3156621/mod4-wp-2023-2024-pokemon
  • s3126099/mod4-wp-2023-2024-pokemon
  • s3128121/mod4-wp-2023-2024-pokemon
  • s3233405/mod4-wp-2023-2024-pokemon
  • s3087921/mod4-wp-2023-2024-pokemon
  • s3202119/mod4-wp-2023-2024-pokemon
  • claudenirmf/mod4-wp-2023-2024-pokemon
168 results
Show changes
Showing
with 2945 additions and 2454 deletions
package nl.utwente.mod4.pokemon;
import jakarta.servlet.http.HttpServlet;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
@ApplicationPath("/")
public class PokemonTrainerDatabase extends HttpServlet {
@ApplicationPath("/api")
public class PokeApp extends Application {
}
\ No newline at end of file
package nl.utwente.mod4.pokemon;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.ext.Provider;
@Provider
public class RequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) {
System.out.println("Incoming Request:");
System.out.println("Method: " + requestContext.getMethod());
System.out.println("URI: " + requestContext.getUriInfo().getRequestUri());
System.out.println("Headers: " + requestContext.getHeaders());
}
}
package nl.utwente.mod4.pokemon;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
public class Utils {
public static String getAbsolutePathToResources() {
var path = Utils.class.getClassLoader().getResource("").getPath();
return URLDecoder.decode(path, StandardCharsets.UTF_8);
}
public static <T extends Comparable<T>> int compare(T o1, T o2) {
if (o1 == null && o2 == null)
return 0;
if (o1 == null)
return -1;
if (o2 == null)
return 1;
return o1.compareTo(o2);
}
public static List<?> pageSlice(List<?> list, int pageSize, int pageNumber) {
int total = list.size();
int firstIndex = (pageNumber - 1) * pageSize;
int lastIndex = Math.min(pageNumber * pageSize, total);
return list.subList(firstIndex,lastIndex);
}
}
package nl.utwente.mod4.pokemon.dao;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.NotSupportedException;
import nl.utwente.mod4.pokemon.Utils;
import nl.utwente.mod4.pokemon.model.PokemonType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.time.Instant;
import java.util.*;
public class PokemonTypeDao {
public enum PokemonTypeDao {
private static final String filePath = PokemonTypeDao.class.getClassLoader().getResource("/aggregated-and-filtered-pokemon-dataset.json").getPath();
INSTANCE;
public List<PokemonType> loadPokemonType() throws IOException {
private static final String ORIGINAL_POKEMON_TYPES = Utils.getAbsolutePathToResources() + "/aggregated-and-filtered-pokemon-dataset.json";
private static final String POKEMON_TYPES = Utils.getAbsolutePathToResources() + "/pokemon-types.json";
private HashMap<String, PokemonType> pokemonTypes = new HashMap<>();
public void delete(String id) {
if(pokemonTypes.containsKey(id)) {
pokemonTypes.remove(id);
} else {
throw new NotFoundException("Pokemon type '" + id + "' not found.");
}
}
public List<PokemonType> getPokemonTypes(int pageSize, int pageNumber, String sortBy) {
List<PokemonType> list = new ArrayList<>(pokemonTypes.values());
if (sortBy == null || sortBy.isEmpty() || "id".equals(sortBy))
list.sort((pt1, pt2) -> Utils.compare(Integer.parseInt(pt1.id), Integer.parseInt(pt2.id)));
else if ("pokedexNumber".equals(sortBy))
list.sort((pt1, pt2) -> Utils.compare(pt1.pokedexNumber, pt2.pokedexNumber));
else if ("lastUpDate".equals(sortBy))
list.sort((pt1, pt2) -> Utils.compare(pt1.lastUpDate, pt2.lastUpDate));
else
throw new NotSupportedException("Sort field not supported");
return (List<PokemonType>) Utils.pageSlice(list,pageSize,pageNumber);
}
public PokemonType getPokemonType(String id) {
var pt = pokemonTypes.get(id);
if (pt == null) {
throw new NotFoundException("Pokemon '" + id + "' not found!");
}
return pt;
}
public void load() throws IOException {
ObjectMapper mapper = new ObjectMapper();
File source = existsPokemonTypes() ?
new File(POKEMON_TYPES) :
new File(ORIGINAL_POKEMON_TYPES);
PokemonType[] arr = mapper.readValue(source, PokemonType[].class);
Arrays.stream(arr).forEach(pt -> pokemonTypes.put(pt.id, pt));
}
private boolean existsPokemonTypes() {
File f = new File(POKEMON_TYPES);
return f.exists() && !f.isDirectory();
}
public void save() throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(new File(filePath), new ArrayList<PokemonType>().getClass());
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
File destination = new File(POKEMON_TYPES);
writer.writeValue(destination, pokemonTypes.values());
}
public PokemonType create(PokemonType newPokemonType) {
String nextId = "" + (getMaxId() + 1);
newPokemonType.id = nextId;
newPokemonType.created = Instant.now().toString();
newPokemonType.lastUpDate = Instant.now().toString();
pokemonTypes.put(nextId,newPokemonType);
return newPokemonType;
}
private int getMaxId() {
Set<String> ids = pokemonTypes.keySet();
return ids.isEmpty() ? 0 : ids.stream()
.map(Integer::parseInt)
.max(Integer::compareTo)
.get();
}
public PokemonType replace(PokemonType updated) {
if(!updated.checkIsValid())
throw new BadRequestException("Invalid pokemon type.");
if(pokemonTypes.get(updated.id) == null)
throw new NotFoundException("Pokemon type id '" + updated.id + "' not found.");
updated.lastUpDate = Instant.now().toString();
pokemonTypes.put(updated.id,updated);
return updated;
}
public int getTotalPokemonTypes() {
return pokemonTypes.keySet().size();
}
}
package nl.utwente.mod4.pokemon.dao;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.NotFoundException;
import nl.utwente.mod4.pokemon.Utils;
import nl.utwente.mod4.pokemon.model.Trainer;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.*;
public enum TrainerDao {
INSTANCE;
private static final String ORIGINAL_TRAINERS = Utils.getAbsolutePathToResources() + "/default-trainers-dataset.json";
private static final String TRAINERS = Utils.getAbsolutePathToResources() + "/trainers.json";
private HashMap<String, Trainer> trainers = new HashMap<>();
public void delete(String id) {
if(trainers.containsKey(id)) {
trainers.remove(id);
} else {
throw new NotFoundException("Trainer '" + id + "' not found.");
}
}
public List<Trainer> getTrainers(int pageSize, int pageNumber) {
var list = new ArrayList<>(trainers.values());
return (List<Trainer>) Utils.pageSlice(list,pageSize,pageNumber);
}
public Trainer getTrainer(String id) {
var pt = trainers.get(id);
if (pt == null) {
throw new NotFoundException("Pokemon '" + id + "' not found!");
}
return pt;
}
public void load() throws IOException {
ObjectMapper mapper = new ObjectMapper();
File source = existsTrainers() ?
new File(TRAINERS) :
new File(ORIGINAL_TRAINERS);
Trainer[] arr = mapper.readValue(source, Trainer[].class);
Arrays.stream(arr).forEach(trainer -> trainers.put(trainer.id, trainer));
}
public void save() throws IOException {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
File destination = new File(TRAINERS);
writer.writeValue(destination, trainers.values());
}
private boolean existsTrainers() {
File f = new File(TRAINERS);
return f.exists() && !f.isDirectory();
}
private int getMaxId() {
Set<String> ids = trainers.keySet();
return ids.isEmpty() ? 0 : ids.stream()
.map(Integer::parseInt)
.max(Integer::compareTo)
.get();
}
public Trainer create(Trainer newTrainer) {
String nextId = "" + (getMaxId() + 1);
newTrainer.id = nextId;
newTrainer.created = Instant.now().toString();
newTrainer.lastUpDate = Instant.now().toString();
trainers.put(nextId,newTrainer);
return newTrainer;
}
public Trainer replace(Trainer updated) {
if(!updated.checkIsValid())
throw new BadRequestException("Invalid trainer.");
if(trainers.get(updated.id) == null)
throw new NotFoundException("Trainer id '" + updated.id + "' not found.");
updated.lastUpDate = Instant.now().toString();
trainers.put(updated.id,updated);
return updated;
}
public int getTotalTrainers() {
return trainers.keySet().size();
}
}
package nl.utwente.mod4.pokemon.model;
import java.time.Instant;
public class NamedEntity {
public String id;
......@@ -10,10 +8,14 @@ public class NamedEntity {
public String lastUpDate;
public NamedEntity() {
id = "0";
name = "Missingno";
created = Instant.now().toString();
lastUpDate = Instant.now().toString();
id = null;
name = null;
created = null;
lastUpDate = null;
}
public boolean checkIsValid() {
return id != null && !id.isEmpty();
}
}
package nl.utwente.mod4.pokemon.model;
public class Pokemon extends NamedEntity {
public String trainerId;
public String pokemonType;
public Pokemon() {}
}
......@@ -28,9 +28,9 @@ public class PokemonType extends NamedEntity {
super();
pokedexNumber = 0;
generation = 0;
japaneseName = "";
classification = "";
abilities = new ArrayList<>();
japaneseName = null;
classification = null;
abilities = null;
baseHeight = 0;
baseWeight = 0;
baseHp = 0;
......@@ -41,7 +41,7 @@ public class PokemonType extends NamedEntity {
baseSpeed = 0;
captureRate = 0;
isLegendary = false;
imgUrl = "";
imgUrl = null;
}
}
package nl.utwente.mod4.pokemon.model;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
public class ResourceCollection<T extends NamedEntity> {
public HashMap<String,Object> meta;
// public HashMap<String,Object> links;
public T[] data;
public ResourceCollection() {
data = null;
meta = new HashMap<>();
// links = new HashMap<>();
}
public ResourceCollection(T[] resources, int pageSize, int pageNumber, int total) {
meta = new HashMap<>();
// links = new HashMap<>();
meta.put("total", total);
meta.put("pageNumber", pageNumber);
meta.put("pageSize", pageSize);
data = resources;
}
}
package nl.utwente.mod4.pokemon.model;
public class Trainer extends NamedEntity {
public String profileUrl;
public Trainer() {
super();
profileUrl = null;
}
}
package nl.utwente.mod4.pokemon.routes;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import nl.utwente.mod4.pokemon.model.Pokemon;
import java.util.ArrayList;
import java.util.List;
@Path("/pokemon")
public class PokemonRoute {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Pokemon> getPokemon() {
var list = new ArrayList<Pokemon> ();
var bolt = new Pokemon();
bolt.id = "1";
bolt.name = "Bolt";
bolt.trainerId = "1";
bolt.pokemonType = "513";
list.add(bolt);
return list;
}
@Path("/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Pokemon getPokemon(@PathParam("id") String id) {
var bolt = new Pokemon();
bolt.id = id;
bolt.name = "Bolt";
bolt.trainerId = "1";
bolt.pokemonType = "513";
return bolt;
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Pokemon createPokemon(Pokemon pokemon) {
return pokemon;
}
@Path("/{id}")
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Pokemon updatePokemon(@PathParam("id") String id, Pokemon pokemon) {
return pokemon;
}
@Path("/{id}")
@DELETE
public void deletePokemon(@PathParam("id") String id) {
System.out.println("Pokemon '"+id+"' deleted");
}
}
......@@ -2,41 +2,68 @@ package nl.utwente.mod4.pokemon.routes;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import nl.utwente.mod4.pokemon.dao.PokemonTypeDao;
import nl.utwente.mod4.pokemon.model.PokemonType;
import java.io.IOException;
import java.util.List;
import nl.utwente.mod4.pokemon.model.ResourceCollection;
@Path("/pokemonTypes")
public class PokemonTypeRoute {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public List<PokemonType> getPokemon() {
try {
return new PokemonTypeDao().loadPokemonType();
} catch (IOException e) {
throw new ServerErrorException(Response.status(500).build());
}
public ResourceCollection<PokemonType> getPokemonTypes(
@QueryParam("sortBy") String sortBy,
@QueryParam("pageSize") int pageSize,
@QueryParam("pageNumber") int pageNumber
) {
// initializes page size and page number for request
int ps = pageSize > 0 ? pageSize : Integer.MAX_VALUE;
int pn = pageNumber > 0 ? pageNumber : 1;
// request the desired page and sorting to the Data Access Object
PokemonType[] resources = PokemonTypeDao.INSTANCE.getPokemonTypes(ps, pn, sortBy).toArray(new PokemonType[0]);
// updates the total and page size for the response
int total = PokemonTypeDao.INSTANCE.getTotalPokemonTypes();
ps = resources == null ? 0 : resources.length;
// returns a collection object containing the requested resources
return new ResourceCollection<>(resources, ps, pn, total);
}
// @POST
// @Path("/")
// @Produces(MediaType.APPLICATION_JSON)
// @Consumes(MediaType.APPLICATION_JSON)
// public String createPokemon() {
// return "Returning all pokemon types.";
// }
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public PokemonType createPokemonType(PokemonType pokemonType) {
// creates a resource based on to the JSON object sent by the client
return PokemonTypeDao.INSTANCE.create(pokemonType);
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public nl.utwente.mod4.pokemon.model.PokemonType getPokemon(@PathParam("id") String id) {
var pokemon = new nl.utwente.mod4.pokemon.model.PokemonType();
pokemon.id = id;
return pokemon;
public PokemonType getPokemonType(@PathParam("id") String id) {
// returns to the client the resource with the desired id
return PokemonTypeDao.INSTANCE.getPokemonType(id);
}
@PUT
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public PokemonType updatePokemonType(@PathParam("id") String id, PokemonType toReplace) {
// check if the desired id and the id of the resource to be replaced match
if (id == null || !id.equals(toReplace.id))
throw new BadRequestException("Id mismatch.");
// return the object replaced
return PokemonTypeDao.INSTANCE.replace(toReplace);
}
@DELETE
@Path("/{id}")
public void deletePokemonType(@PathParam("id") String id) {
// delete the resource with the desired id
PokemonTypeDao.INSTANCE.delete(id);
}
}
\ No newline at end of file
package nl.utwente.mod4.pokemon.routes;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import nl.utwente.mod4.pokemon.dao.TrainerDao;
import nl.utwente.mod4.pokemon.model.ResourceCollection;
import nl.utwente.mod4.pokemon.model.Trainer;
@Path("/trainers")
public class TrainerRoute {
@GET
@Produces(MediaType.APPLICATION_JSON)
public ResourceCollection<Trainer> getTrainers(
@QueryParam("pageSize") int pageSize,
@QueryParam("pageNumber") int pageNumber
) {
// initializes page size and page number for request
int ps = pageSize > 0 ? pageSize : Integer.MAX_VALUE;
int pn = pageNumber > 0 ? pageNumber : 1;
// request the desired page to the Data Access Object
Trainer[] resources = TrainerDao.INSTANCE.getTrainers(ps, pn).toArray(new Trainer[0]);
// updates the total and page size for the response
int total = TrainerDao.INSTANCE.getTotalTrainers();
ps = resources == null ? 0 : resources.length;
// returns a collection object containing the requested resources
return new ResourceCollection<>(resources, ps, pn, total);
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Trainer createTrainer(Trainer pokemonType) {
// creates a resource based on to the JSON object sent by the client
return TrainerDao.INSTANCE.create(pokemonType);
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Trainer getTrainer(@PathParam("id") String id) {
// returns to the client the resource with the desired id
return TrainerDao.INSTANCE.getTrainer(id);
}
@PUT
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Trainer updateTrainer(@PathParam("id") String id, Trainer toReplace) {
// check if the desired id and the id of the resource to be replaced match
if (id == null || !id.equals(toReplace.id))
throw new BadRequestException("Id mismatch.");
// return the object replaced
return TrainerDao.INSTANCE.replace(toReplace);
}
@DELETE
@Path("/{id}")
public void deleteTrainer(@PathParam("id") String id) {
// delete the resource with the desired id
TrainerDao.INSTANCE.delete(id);
}
}
[
{
"name": "Red",
"id": "1",
"created": "24/04/2024",
"lastUpDate": "24/04/2024",
"profileUrl": "https://archives.bulbagarden.net/media/upload/thumb/d/d3/Lets_Go_Pikachu_Eevee_Red.png/500px-Lets_Go_Pikachu_Eevee_Red.png"
},
{
"name": "Blue",
"id": "2",
"created": "24/04/2024",
"lastUpDate": "24/04/2024",
"profileUrl": "https://archives.bulbagarden.net/media/upload/thumb/1/1a/Lets_Go_Pikachu_Eevee_Blue.png/320px-Lets_Go_Pikachu_Eevee_Blue.png"
},
{
"name": "Misty",
"id": "3",
"created": "24/04/2024",
"lastUpDate": "24/04/2024",
"profileUrl": "https://archives.bulbagarden.net/media/upload/thumb/f/f6/Lets_Go_Pikachu_Eevee_Misty.png/367px-Lets_Go_Pikachu_Eevee_Misty.png"
},
{
"name": "Brock",
"id": "4",
"created": "24/04/2024",
"lastUpDate": "24/04/2024",
"profileUrl": "https://archives.bulbagarden.net/media/upload/thumb/a/a6/Lets_Go_Pikachu_Eevee_Brock.png/243px-Lets_Go_Pikachu_Eevee_Brock.png"
}
]
\ No newline at end of file
......@@ -6,18 +6,7 @@
version="3.1">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>trainers.html</welcome-file>
<!-- <welcome-file>pokemonTypes.html</welcome-file>-->
</welcome-file-list>
<servlet>
<description>Pokemon Servlet</description>
<display-name>PokemonTrainerDatabase</display-name>
<servlet-name>PokemonTrainerDatabase</servlet-name>
<servlet-class>nl.utwente.mod4.pokemon.PokemonTrainerDatabase</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PokemonTrainerDatabase</servlet-name>
<url-pattern>/pokemon</url-pattern>
</servlet-mapping>
</web-app>
\ No newline at end of file
src/main/webapp/favicon.ico

33.7 KiB

src/main/webapp/images/blue.png

27 KiB

src/main/webapp/images/brock.jpg

138 KiB

src/main/webapp/images/brock.png

279 KiB