public static void main(String[] args) throws SQLException {
    // open database
    Connection conn = DriverManager.getConnection("jdbc:h2:./main");
    createTables(conn);

    // serve external files
    Spark.externalStaticFileLocation("public");
    Spark.init();

    // insert test data
    if (selectCountries(conn).size() == 0) {
      insertCountry(conn, "United States", "US");
      insertCountry(conn, "Canada", "CA");
      insertCountry(conn, "Mexico", "MX");
    }

    // create routes for AJAX
    Spark.get(
        "/get-countries",
        ((request, response) -> {
          JsonSerializer serializer = new JsonSerializer();
          String json = serializer.serialize(selectCountries(conn));
          return json;
        }));
    Spark.get(
        "/get-country",
        ((request, response) -> {
          String id = request.queryParams("id");
          try {
            int idNum = Integer.valueOf(id);
            JsonSerializer serializer = new JsonSerializer();
            String json = serializer.serialize(selectCountry(conn, idNum));
            return json;
          } catch (Exception e) {

          }
          return "";
        }));
    Spark.post(
        "/add-country",
        ((request, response) -> {
          String name = request.queryParams("name");
          String abbrev = request.queryParams("abbrev");
          if (name == null || abbrev == null) {
            Spark.halt(403);
          }
          insertCountry(conn, name, abbrev);
          return "";
        }));
  }
예제 #2
0
  public static void main(String[] args) {
    // writes your code here
    HashMap<String, User> users = new HashMap(); // creating list to hold users
    Spark.init();
    Spark.post(
        "/create-account",
        ((request, response) -> {
          String name = request.queryParams("username");
          String password = request.queryParams("password");

          Session session = request.session();
          session.attribute("username", name);

          if (users.get(name) == null) {
            User user = new User(); // creating new user
            user.name = name;
            user.password = password;
            users.put(name, user);
          }
          response.redirect("/");
          return "";
        }));
    Spark.get(
        "/",
        ((request, response) -> {
          Session session = request.session();
          String name = session.attribute("username");
          if (name != null) {
            HashMap m = new HashMap();
            m.put("count", users.size());
            m.put("accounts", users.values());
            return new ModelAndView(m, "logged-in.html");
          }
          HashMap m = new HashMap();
          return new ModelAndView(m, "not-logged-in.html");
        }),
        new MustacheTemplateEngine());
    Spark.post(
        "/logout",
        ((request, response) -> {
          Session session = request.session();
          session.invalidate();
          response.redirect("/");
          return "";
        }));
  }