|
@@ -1,19 +1,26 @@
|
|
|
package com.techempower;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+
|
|
|
+import io.requery.EntityStore;
|
|
|
+import io.requery.Persistable;
|
|
|
+
|
|
|
import org.jooby.Jooby;
|
|
|
import org.jooby.MediaType;
|
|
|
+
|
|
|
+import static org.jooby.MediaType.*;
|
|
|
import org.jooby.Result;
|
|
|
import org.jooby.Results;
|
|
|
-import org.jooby.hbs.Hbs;
|
|
|
-import org.jooby.jdbi.Jdbi;
|
|
|
+import org.jooby.jdbc.Jdbc;
|
|
|
import org.jooby.json.Jackson;
|
|
|
-import org.skife.jdbi.v2.Handle;
|
|
|
+import org.jooby.requery.Requery;
|
|
|
+import org.jooby.rocker.Rockerby;
|
|
|
|
|
|
import java.time.Instant;
|
|
|
import java.time.ZoneId;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
@@ -21,8 +28,9 @@ import java.util.concurrent.ThreadLocalRandom;
|
|
|
/**
|
|
|
* @author jooby generator
|
|
|
*/
|
|
|
+@SuppressWarnings("unchecked")
|
|
|
public class App extends Jooby {
|
|
|
-
|
|
|
+
|
|
|
static final DateTimeFormatter fmt = DateTimeFormatter
|
|
|
.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH)
|
|
|
.withZone(ZoneId.of("GMT"));
|
|
@@ -37,53 +45,55 @@ public class App extends Jooby {
|
|
|
static final String HELLO_WORLD = "Hello, World!";
|
|
|
|
|
|
{
|
|
|
- /** json via jackson .*/
|
|
|
+ /** templates via rocker. */
|
|
|
+ use(new Rockerby());
|
|
|
+
|
|
|
+ /** json via jackson . */
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
use(new Jackson(mapper));
|
|
|
|
|
|
- /** database via jdbi .*/
|
|
|
- use(new Jdbi());
|
|
|
-
|
|
|
- /** templates via handlebars .*/
|
|
|
- use(new Hbs());
|
|
|
+ /** database via requery. */
|
|
|
+ use(new Jdbc());
|
|
|
+ use(new Requery(Models.DEFAULT));
|
|
|
|
|
|
- get("/plaintext", () -> result(HELLO_WORLD, MediaType.text))
|
|
|
+ get("/plaintext", () -> result(HELLO_WORLD, text))
|
|
|
.renderer("text");
|
|
|
|
|
|
- get("/json", () -> result(mapper.createObjectNode().put("message", HELLO_WORLD), MediaType.json))
|
|
|
+ get("/json", () -> result(mapper.createObjectNode().put("message", HELLO_WORLD), json))
|
|
|
.renderer("json");
|
|
|
|
|
|
get("/db", req -> {
|
|
|
- try (Handle handle = req.require(Handle.class)) {
|
|
|
- int id = ThreadLocalRandom.current().nextInt(DB_ROWS + 1);
|
|
|
- return result(
|
|
|
- handle.createQuery("select * from World where id = :id")
|
|
|
- .bind("id", id)
|
|
|
- .map((idx, rs, ctx) -> new World(rs.getInt("id"), rs.getInt("randomNumber")))
|
|
|
- .first(),
|
|
|
- MediaType.json);
|
|
|
- }
|
|
|
+ int id = ThreadLocalRandom.current().nextInt(DB_ROWS) + 1;
|
|
|
+ EntityStore<Persistable, World> store = require(EntityStore.class);
|
|
|
+ World world = store.select(World.class)
|
|
|
+ .where(World.ID.eq(id))
|
|
|
+ .get()
|
|
|
+ .first();
|
|
|
+ return result(world, json);
|
|
|
}).renderer("json");
|
|
|
|
|
|
get("/fortunes", req -> {
|
|
|
- try (Handle handle = req.require(Handle.class)) {
|
|
|
- List<Fortune> fortunes = handle.createQuery("select * from fortune")
|
|
|
- .map((idx,rs,ctx) -> new Fortune(rs.getInt("id"), rs.getString("message")))
|
|
|
- .list();
|
|
|
- fortunes.add(new Fortune(0, "Additional fortune added at request time."));
|
|
|
- Collections.sort(fortunes);
|
|
|
- return result(Results.html("fortunes").put("fortunes", fortunes), MediaType.html);
|
|
|
- }
|
|
|
- }).renderer("html");
|
|
|
+ EntityStore<Persistable, Fortune> store = require(EntityStore.class);
|
|
|
+ List<Fortune> fortunes = store.select(Fortune.class)
|
|
|
+ .get()
|
|
|
+ .collect(new LinkedList<>());
|
|
|
+ Fortune fortune0 = new Fortune();
|
|
|
+ fortune0.setMessage("Additional fortune added at request time.");
|
|
|
+ fortune0.setId(0);
|
|
|
+ fortunes.add(fortune0);
|
|
|
+ Collections.sort(fortunes);
|
|
|
+ return views.fortunes.template(fortunes);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
private Result result(final Object value, final MediaType type) {
|
|
|
- return Results.ok(value).type(type)
|
|
|
+ return Results.ok(value)
|
|
|
+ .type(type)
|
|
|
.header(H_SERVER, SERVER)
|
|
|
.header(H_DATE, fmt.format(Instant.ofEpochMilli(System.currentTimeMillis())));
|
|
|
}
|
|
|
|
|
|
- public static void main(final String[] args) throws Exception {
|
|
|
+ public static void main(final String[] args) {
|
|
|
run(App::new, args);
|
|
|
}
|
|
|
|