// GET a list of all decks @GET @Produces({MediaType.APPLICATION_JSON}) public String getAll() { List<Deck> deckList = deckService.getAll(); Gson gson = new Gson(); return gson.toJson(deckList); }
// PUT - create a new deck in sorted order @PUT @Produces({MediaType.APPLICATION_JSON}) public String createNewDeck() { Deck deck = deckService.createNewDeck(); // move to return statement Gson gson = new Gson(); return gson.toJson(deck); }
// TODO Make this exercise work without this initialization // TESTING initialize tempDatabase @POST public String initDatabase() { List<Deck> deckList = deckService.initDatabase(); Gson gson = new Gson(); return gson.toJson(deckList); }
// DELETE - delete an existing deck @DELETE @Produces({MediaType.APPLICATION_JSON}) @Path("/{param}") public String deleteDeckById(@PathParam("param") int id) { Gson gson = new Gson(); if (deckService.deleteDeckById(id)) { return gson.toJson("Deck #" + id + " has been deleted."); } else { return gson.toJson("Delete of deck #" + id + " failed"); } }
// POST - shuffle an existing deck @POST @Path("/{param}") @Produces({MediaType.APPLICATION_JSON}) public String shuffleDeckById(@PathParam("param") int id) { Deck deck = deckService.shuffleDeckById(id); if (deck == null) { System.out.println("Deck with id:" + id + " is not found"); throw new NotFoundException("Deck with id:" + id + " is not found"); } Gson gson = new Gson(); return gson.toJson(deck); }