From 29dfb05782888f8ea358d9376b976dbb6fe2f038 Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Wed, 6 Nov 2019 19:07:13 +0100 Subject: [PATCH] Add 'get all', 'update specific' and 'delete' baton endpoints --- src/main/java/telraam/api/BatonResource.java | 64 +++++++++++++++++++- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/src/main/java/telraam/api/BatonResource.java b/src/main/java/telraam/api/BatonResource.java index 311d2e8..1954d22 100644 --- a/src/main/java/telraam/api/BatonResource.java +++ b/src/main/java/telraam/api/BatonResource.java @@ -1,16 +1,23 @@ package telraam.api; +import telraam.App; import telraam.database.daos.BatonDAO; import telraam.database.models.Baton; +import telraam.database.models.Id; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.List; import java.util.Optional; @Path("/baton") @Produces(MediaType.APPLICATION_JSON) public class BatonResource { + private static java.util.logging.Logger logger = java.util.logging.Logger.getLogger(App.class.getName()); + + private static final String ID_NAME = "batonId"; + private static final String ENTITY_PATH = "/{batonId: [0-9]*}"; private BatonDAO batonDAO; @@ -18,11 +25,33 @@ public BatonResource(BatonDAO batonDAO) { this.batonDAO = batonDAO; } + /** + * @return All the baton's in the database + */ @GET - public Baton sayHello(@QueryParam("id") Optional id) { - if(id.isPresent()){ + public List getListOfBatons() { + return batonDAO.listBatons(); + } + + /** + * Create a new baton + * + * @param baton Passed as json via the request body + * @return The generated id of the baton + */ + @POST + public Id createBaton(Baton baton) { + return batonDAO.insert(baton); + } + + /** + * @return a specific baton on the id + */ + @GET @Path(ENTITY_PATH) + public Baton getBaton(@PathParam(ID_NAME) Optional id) { + if (id.isPresent()) { Optional optionalBaton = batonDAO.findBatonById(id.get()); - if(optionalBaton.isPresent()){ + if (optionalBaton.isPresent()) { return optionalBaton.get(); } else { throw new WebApplicationException(String.format("Baton with id: %d not found", id.get()), Response.Status.NOT_FOUND); @@ -32,4 +61,33 @@ public Baton sayHello(@QueryParam("id") Optional id) { } } + /** + * Update a specific baton with the specified information + */ + @PUT @Path(ENTITY_PATH) + public Response updateBaton(@PathParam(ID_NAME) Optional id) { + if (id.isPresent()) { + Optional optionalBaton = batonDAO.findBatonById(id.get()); + if (optionalBaton.isPresent()) { + Baton baton = optionalBaton.get(); + // TODO update the baton in the database + // batonDAO.update(baton); + // TODO return the updated baton + return Response.noContent().build(); + } else { + throw new WebApplicationException(String.format("Baton with id: %d not found", id.get()), Response.Status.NOT_FOUND); + } + } else { + throw new WebApplicationException("You did not pass an id", Response.Status.BAD_REQUEST); + } + } + + + + @DELETE @Path(ENTITY_PATH) + public boolean deleteBaton(@PathParam(ID_NAME) Optional id) { + // TODO delete the baton + return true; + } } +