Example #1
0
  public static void main(final String[] args) {
    setPort(50845);
    externalStaticFileLocation("public"); // Static files

    new RecoController();
    new UserController();
    new TagController();

    get(
        "/",
        (request, response) -> {
          response.redirect("/index.html");
          return "";
        });

    before(
        (request, response) -> {
          HibernateUtil.getSession().beginTransaction();
        });

    after(
        (request, response) -> {
          HibernateUtil.getSession().getTransaction().commit();
          HibernateUtil.closeSession();
        });

    exception(
        Exception.class,
        (e, request, response) -> {
          HibernateUtil.getSession().getTransaction().rollback();
          HibernateUtil.closeSession();
          response.status(500);
        });
  }
Example #2
0
  public UserController() {

    get(
        "dbeb/users",
        "application/json",
        (request, response) -> {
          String email = request.queryParams("facebookId");

          List<User> objs =
              HibernateUtil.getSession()
                  .createCriteria(User.class)
                  .add(Restrictions.eq("facebookId", email))
                  .list();

          return objs;
        },
        new JsonTransformer());

    get(
        "dbeb/users/:id",
        "application/json",
        (request, response) -> {
          long id = Long.parseLong(request.params(":id"));
          User obj = (User) HibernateUtil.getSession().get(User.class, id);
          if (obj == null) halt(404);
          return obj;
        },
        new JsonTransformer());

    post(
        "dbeb/users",
        "application/json",
        (request, response) -> {
          User obj = JacksonUtil.readValue(request.body(), User.class);
          HibernateUtil.getSession().saveOrUpdate(obj);
          response.status(201);
          return obj;
        },
        new JsonTransformer());

    put(
        "dbeb/users/:id",
        "application/json",
        (request, response) -> {
          long id = Long.parseLong(request.params(":id"));
          User obj = (User) HibernateUtil.getSession().get(User.class, id);
          if (obj == null) halt(404);
          obj = JacksonUtil.readValue(request.body(), User.class);
          obj = (User) HibernateUtil.getSession().merge(obj);
          return obj;
        },
        new JsonTransformer());

    delete(
        "dbeb/users/:id",
        (request, response) -> {
          long id = Long.parseLong(request.params(":id"));
          User obj = (User) HibernateUtil.getSession().get(User.class, id);
          if (obj == null) halt(404);
          HibernateUtil.getSession().delete(obj);
          response.status(204);
          return "";
        });
  }