Пример #1
0
  @RouteMapping(method = RouteMethod.GET)
  public Handler<RoutingContext> getAll() {
    return ctx -> {
      JDBCClient client = AppUtil.getJdbcClient(Vertx.vertx());

      client.getConnection(
          conn -> {
            if (conn.failed()) {
              LOGGER.error(conn.cause().getMessage(), conn.cause());
              ctx.fail(400);
            }

            SQLUtil.query(
                conn.result(),
                "select * from patient",
                rs -> {
                  JsonArray patients = new JsonArray();
                  for (JsonObject row : rs.getRows()) {
                    patients.add(row);
                  }

                  SQLUtil.close(conn.result());
                  ctx.response().end(patients.encode());
                });
          });
    };
  }
Пример #2
0
  @RouteMapping(value = "/:id", method = RouteMethod.GET)
  public Handler<RoutingContext> fetch() {
    return ctx -> {
      String id = ctx.request().getParam("id");
      if (StringUtils.isBlank(id)) {
        LOGGER.error("ID is blank");
        JsonObject error = new JsonObject();
        error.put("error", "ID should not be blank");
        ctx.response().setStatusCode(205).end(error.encode());
      }

      JDBCClient client = AppUtil.getJdbcClient(Vertx.vertx());
      client.getConnection(
          conn -> {
            if (conn.failed()) {
              LOGGER.error(conn.cause().getMessage(), conn.cause());
              ctx.fail(400);
            }

            SQLUtil.query(
                conn.result(),
                "select id, title, description from item where id = ?",
                new JsonArray().add(Integer.valueOf(id)),
                rs -> {
                  SQLUtil.close(conn.result());
                  if (rs.getRows().size() == 1) {
                    ctx.response().end(rs.getRows().get(0).encode());
                  } else {
                    JsonObject error = new JsonObject();
                    error.put("error", "Record not found");
                    ctx.response().setStatusCode(205).end(error.encode());
                  }
                });
          });
    };
  }