Skip to content

Commit

Permalink
Implement tests: new, get all, delete all
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya authored and Ilya committed Apr 11, 2017
1 parent 7525b69 commit 8c18d81
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
38 changes: 31 additions & 7 deletions src/main/java/controllers/ApplicationController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package controllers;

import com.google.inject.Inject;
import com.jayway.jsonpath.ReadContext;
import io.mangoo.routing.Response;
import io.mangoo.routing.bindings.Session;
import io.mangoo.routing.bindings.Request;
import jooq.tables.records.TodoRecord;
import model.Todo;
import org.jooq.DSLContext;

Expand All @@ -13,24 +15,46 @@
public class ApplicationController {
@Inject private DSLContext create;

public Response index() {
create.insertInto(TODO).columns(TODO.TITLE).values("NewTitle").execute();

public Response getAll(Request r) {
List<Todo> result = create.selectFrom(TODO).fetchInto(Todo.class);

result.stream().forEach(todo -> todo.url = composeUrl(r, todo.id));

return Response.withOk().andJsonBody(result);
}

public Response todo(Session session) {
session.put("1", "2");
public Response create(Request r) {

ReadContext ctx = r.getBodyAsJsonPath();

String title = ctx.read("$.title");


TodoRecord tr = create.insertInto(TODO).columns(TODO.TITLE).values(title).returning(TODO.ID).fetchOne();

Todo t = new Todo();
t.id = tr.get(TODO.ID);
t.title = title;
t.url = composeUrl(r, t.id);

return Response.withOk().andJsonBody("TODO");
return Response.withOk().andJsonBody(t);
}

public Response deleteAll() {
create.deleteFrom(TODO).execute();

return Response.withOk();
}

public Response options() {
return Response.withOk().andEmptyBody();
}


private String composeUrl(Request r, long id) {
return (r.getURL().endsWith("/")) ? r.getURL() + id : r.getURL() + "/" + id;
}

//POST: {"title":"a todo"}
//{"title":"a todo","completed":false,"url":"http://todobackend-spring.herokuapp.com//28","order":null}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/model/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class Todo {
public long id;
public String title;
public boolean completed;
public String url;
}
17 changes: 11 additions & 6 deletions src/main/resources/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ routes:
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- method: GET
- method: OPTIONS
url: /
mapping: ApplicationController.index
mapping: ApplicationController.options

- method: GET
url: /todo
mapping: ApplicationController.todo
url: /
mapping: ApplicationController.getAll

- method: OPTIONS
- method: POST
url: /
mapping: ApplicationController.options
mapping: ApplicationController.create

- method: DELETE
url: /
mapping: ApplicationController.deleteAll

0 comments on commit 8c18d81

Please sign in to comment.