コード例 #1
0
ファイル: App.java プロジェクト: Russspruce/hair_salon
  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());
  }