Skip to content

Commit

Permalink
Minor code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsomai committed Mar 24, 2016
1 parent 8952e06 commit 9a9dcd1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Launcher</Main-Class>
<Main-Verticle>io.vertx.todo.TodoREST</Main-Verticle>
<Main-Verticle>io.vertx.todo.Server</Main-Verticle>
</manifestEntries>
</transformer>
<transformer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,28 @@
/*
* @author <a href="mailto:somai.alexandru@gmail.com">Alexandru Somai</a>
*/
public class TodoREST extends AbstractVerticle {
public class Server extends AbstractVerticle {

private static final String TODOS = "todos";
private static final String TODOS_COLLECTION = "todos";

private Router router;
private MongoClient mongo;

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Launcher.main(new String[]{"run", TodoREST.class.getName()});
Launcher.main(new String[]{"run", Server.class.getName()});
}

@Override
public void start(Future<Void> future) {
router = Router.router(vertx);
mongo = MongoClient.createShared(vertx, config());
setUpCors();
setUpRoutes();
createHttpServer(future);
}

Router router = Router.router(vertx);

private void setUpCors() {
router.route().handler(BodyHandler.create());
router.route().handler((routingContext) -> {
routingContext.response()
Expand All @@ -40,7 +45,9 @@ public void start(Future<Void> future) {
.putHeader("Access-Control-Max-Age", "3600");
routingContext.next();
});
}

private void setUpRoutes() {
router.get("/").handler((context) -> context.reroute("/todos"));
router.get("/todos").handler(this::getAllTodos);
router.get("/todos/:todoId").handler(this::getTodo);
Expand All @@ -50,7 +57,9 @@ public void start(Future<Void> future) {
router.delete("/todos/").handler(this::deleteAllTodos);
router.options("/todos").handler((handler) -> handler.response().end());
router.options("/todos/:todoId").handler((handler) -> handler.response().end());
}

private void createHttpServer(Future<Void> future) {
Integer port = 8080;
String host = "localhost";

Expand All @@ -71,7 +80,7 @@ public void start(Future<Void> future) {
}

private void getAllTodos(RoutingContext routingContext) {
mongo.find(TODOS, new JsonObject(), results -> {
mongo.find(TODOS_COLLECTION, new JsonObject(), results -> {
JsonArray arr = new JsonArray();
results.result().forEach(arr::add);
routingContext.response()
Expand All @@ -85,7 +94,7 @@ private void getTodo(RoutingContext routingContext) {
if (id == null) {
sendError(400, routingContext.response());
} else {
mongo.findOne(TODOS, new JsonObject().put("_id", id), new JsonObject(), result -> {
mongo.findOne(TODOS_COLLECTION, new JsonObject().put("_id", id), new JsonObject(), result -> {
if (result.succeeded()) {
routingContext.response()
.putHeader("content-type", "application/json; charset=utf-8")
Expand All @@ -104,23 +113,26 @@ private void addTodo(RoutingContext routingContext) {
if (todo == null) {
sendError(400, response);
} else {
mongo.insert(TODOS, todo, insertResult -> {
mongo.insert(TODOS_COLLECTION, todo, insertResult -> {
if (insertResult.succeeded()) {
String id = insertResult.result();
todo.put("_id", id);
todo.put("url", routingContext.request().absoluteURI() + "/" + id);
todo.put("completed", false);

mongo.update(TODOS, new JsonObject().put("_id", id), new JsonObject().put("$set", todo), updateResult -> {
if (updateResult.succeeded()) {
response.setStatusCode(201)
.putHeader("content-type", "application/json; charset=utf-8")
.end(todo.encodePrettily());
}
if (updateResult.failed()) {
sendError(500, routingContext.response());
}
});
mongo.update(TODOS_COLLECTION,
new JsonObject().put("_id", id),
new JsonObject().put("$set", todo),
updateResult -> {
if (updateResult.succeeded()) {
response.setStatusCode(201)
.putHeader("content-type", "application/json; charset=utf-8")
.end(todo.encodePrettily());
}
if (updateResult.failed()) {
sendError(500, routingContext.response());
}
});
}
if (insertResult.failed()) {
sendError(500, routingContext.response());
Expand All @@ -135,10 +147,10 @@ private void updateTodo(RoutingContext routingContext) {
if (id == null || jsonObject == null) {
sendError(400, routingContext.response());
} else {
mongo.update(TODOS,
mongo.update(TODOS_COLLECTION,
new JsonObject().put("_id", id),
new JsonObject().put("$set", jsonObject)
, result -> {
new JsonObject().put("$set", jsonObject),
result -> {
if (result.succeeded()) {
getTodo(routingContext);
}
Expand All @@ -154,7 +166,7 @@ private void deleteTodo(RoutingContext routingContext) {
if (id == null) {
sendError(400, routingContext.response());
} else {
mongo.removeOne(TODOS, new JsonObject().put("_id", id), result -> {
mongo.removeOne(TODOS_COLLECTION, new JsonObject().put("_id", id), result -> {
if (result.succeeded()) {
routingContext.response().end();
}
Expand All @@ -166,7 +178,7 @@ private void deleteTodo(RoutingContext routingContext) {
}

private void deleteAllTodos(RoutingContext routingContext) {
mongo.dropCollection(TODOS, result -> {
mongo.dropCollection(TODOS_COLLECTION, result -> {
if (result.succeeded()) {
routingContext.response().end();
}
Expand All @@ -175,6 +187,7 @@ private void deleteAllTodos(RoutingContext routingContext) {
}
});
}

private static void sendError(int statusCode, HttpServerResponse response) {
response.setStatusCode(statusCode).end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
@RunWith(VertxUnitRunner.class)
@Ignore
public class TodoRESTTest {
public class ServerTest {

private Vertx vertx;

@Before
public void setUp(TestContext context) {
vertx = Vertx.vertx();
vertx.deployVerticle(TodoREST.class.getName(), context.asyncAssertSuccess());
vertx.deployVerticle(Server.class.getName(), context.asyncAssertSuccess());
}

@After
Expand Down

0 comments on commit 9a9dcd1

Please sign in to comment.