Skip to content

Commit

Permalink
Add example api resource for baton
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbloch committed Nov 6, 2019
1 parent cc4bb85 commit 75523a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/telraam/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.jdbi.v3.core.Jdbi;
import telraam.api.BatonResource;
import telraam.api.HelloworldResource;
import telraam.database.daos.BatonDAO;
import telraam.database.models.Baton;
Expand Down Expand Up @@ -45,8 +46,10 @@ public void run(AppConfiguration configuration, Environment environment) throws
final BatonDAO dao = database.onDemand(BatonDAO.class);
Id id = dao.insert(new Baton("Heto"));

// TODO By default everything should be logged to stdout (see dropwizard logging docs) but it isn't
List<Baton> batons = dao.listBatons();
if(logger.isLoggable(Level.INFO)) {
logger.info("Baton testing information");
logger.info(batons.stream().map(Baton::getName).collect(Collectors.joining(" : ")));
logger.info(String.valueOf(dao.findBatonById(id.getId())));
}
Expand All @@ -57,6 +60,7 @@ public void run(AppConfiguration configuration, Environment environment) throws
configuration.getDefaultName()
);
environment.jersey().register(resource);
environment.jersey().register(new BatonResource(database.onDemand(BatonDAO.class)));

// Register healthcheck
// environment.healthChecks().register("database", new DatabaseHealthCheck(database));
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/telraam/api/BatonResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package telraam.api;

import telraam.database.daos.BatonDAO;
import telraam.database.models.Baton;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Optional;

@Path("/baton")
@Produces(MediaType.APPLICATION_JSON)
public class BatonResource {

private BatonDAO batonDAO;

public BatonResource(BatonDAO batonDAO) {
this.batonDAO = batonDAO;
}

@GET
public Baton sayHello(@QueryParam("id") Optional<Integer> id) {
if(id.isPresent()){
Optional<Baton> optionalBaton = batonDAO.findBatonById(id.get());
if(optionalBaton.isPresent()){
return optionalBaton.get();
} 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);
}
}

}

0 comments on commit 75523a5

Please sign in to comment.