Exemple #1
0
  public static void main(String[] args) {
    staticFileLocation("/public");
    String layout = "templates/layout.vtl";

    get(
        "/",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("stylists", Stylist.all());
          model.put("template", "templates/index.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/add-stylist",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("template", "templates/add-stylist.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    post(
        "/stylists",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          String stylist = request.queryParams("stylist_name");
          Stylist newStylist = new Stylist(stylist);
          newStylist.save();
          model.put("stylists", Stylist.all());
          model.put("template", "templates/index.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/delete/:id",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          int stylist_id = Integer.parseInt(request.params(":id"));
          Stylist.deleteStylistById(stylist_id);
          model.put("stylists", Stylist.all());
          model.put("template", "templates/index.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/stylist/:id",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          int stylist_id = Integer.parseInt(request.params(":id"));
          model.put("clients", Client.getClientsByStylistId(stylist_id));
          model.put("stylist", Stylist.find(stylist_id));
          model.put("template", "templates/stylist.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    post(
        "/client/:id",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          String client_name = request.queryParams("client_name");
          int stylist_id = Integer.parseInt(request.queryParams("stylistId"));
          Client newClient = new Client(client_name, stylist_id);
          if (client_name != null) {
            newClient.save();
          }
          model.put("stylist", Stylist.find(Integer.parseInt(request.params(":id"))));
          model.put("clients", Client.getClientsByStylistId(stylist_id));
          model.put("template", "templates/stylist.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/delete-client/:id/stylist/:stylist_id",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          int client_id = Integer.parseInt(request.params(":id"));
          Client.deleteClientById(client_id);
          Integer stylist_id = Integer.parseInt(request.params(":stylist_id"));
          model.put("stylist", Stylist.find(stylist_id));
          model.put("clients", Client.getClientsByStylistId(stylist_id));
          model.put("template", "templates/stylist.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());
  }
Exemple #2
0
  public static void main(String[] args) {
    staticFileLocation("/public");
    String layout = "templates/layout.vtl";

    get(
        "/",
        (request, response) -> {
          Map<String, Object> model = new HashMap<String, Object>();

          model.put("clients", request.session().attribute("clients"));
          model.put("template", "templates/index.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/stylists/new",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("template", "templates/stylist-form.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/stylists",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("stylists", Stylist.all());
          model.put("template", "templates/stylists.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    post(
        "/stylists",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();

          String styler = request.queryParams("styler");
          Stylist newStylist = new Stylist(styler);
          newStylist.save(); // *** ADDED FOR DB VERSION ***
          model.put("template", "templates/success.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/stylists/:id",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          Stylist stylist = Stylist.find(Integer.parseInt(request.params(":id")));
          model.put("stylist", stylist);
          model.put("template", "templates/stylist.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/stylists/:id/clients/new",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          Stylist stylist = Stylist.find(Integer.parseInt(request.params(":id")));
          model.put("stylist", stylist);
          model.put("template", "templates/client-form.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/clients",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("client", Client.all());
          model.put("template", "templates/client.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    post(
        "/clients",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          Stylist stylist = Stylist.find(Integer.parseInt(request.queryParams("stylist_id")));
          String name = request.queryParams("name");
          // ** THIS SECTION UPDATED FOR DB VERSION ***
          Client newClient = new Client(name, stylist.getId());
          newClient.save();
          model.put("stylist", stylist);
          model.put("template", "templates/client-success.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/stylists/:id/delete",
        (request, response) -> {
          Map model = new HashMap();
          Stylist stylist = Stylist.find(Integer.parseInt(request.params(":id")));
          Stylist.delete(stylist);

          model.put("template", "templates/delete-success.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());
  }