package examples.flash;


import com.vtence.molecule.WebServer;
import com.vtence.molecule.lib.FlashHash;
import com.vtence.molecule.middlewares.CookieSessionTracker;
import com.vtence.molecule.middlewares.Cookies;
import com.vtence.molecule.middlewares.Flash;
import com.vtence.molecule.routing.DynamicRoutes;
import com.vtence.molecule.session.SessionPool;

import java.io.IOException;

public class FlashExample {

    public void run(WebServer server) throws IOException {
        server.add(new Cookies())
              // We'll use an in memory session pool in this example
              .add(new CookieSessionTracker(SessionPool.secure()))
              .add(new Flash())
              .start(new DynamicRoutes() {{
                  post("/accounts").to((request, response) -> {
                      FlashHash flash = FlashHash.get(request);

                      String email = request.parameter("email");
                        if (email != null) {
                          flash.notice("Account '" + email + "' successfully created");
                      } else {
                          flash.alert("An email is required");
                      }
                      response.redirectTo("/account").done();
                  });

                  get("/account").to((request, response) -> {
                      FlashHash flash = FlashHash.get(request);

                      if (flash.notice() != null) {
                          response.done(flash.notice());
                      } else if (flash.alert() != null) {
                          response.done(flash.alert());
                      } else {
                          response.done();
                      }
                  });
              }});
    }


    public static void main(String[] args) throws IOException {
        FlashExample example = new FlashExample();
        // Run the default web server
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri());
    }
}

@@ -382,16 +382,504 @@
 ption;%0A%0A
+/**%0A * %3Cp%3E%0A * In this example we demonstrate how to use the %7B@link FlashHash%7D.%0A  * %3Cp%3E%0A * The flash is a special part of the session which is cleared with each request.%0A * This means that values stored in the flash will only be available in the next request.%0A * %3C/p%3E%0A * %3Cp%3E%0A * When a new user posts to /accounts to create an account, we add a message to the flash%0A * and redirect to /account. A get to /account reads the message from the flash and displays%0A * it to our user.%0A * %3C/p%3E%0A */%0A
 public c
@@ -1128,16 +1128,62 @@
 ure()))%0A
+              // We need the Flash middleware%0A
         
@@ -1250,16 +1250,109 @@
 es() %7B%7B%0A
+                  // a post to /accounts creates a new account if email is not already taken%0A
         
@@ -1533,26 +1533,24 @@
 r(%22email%22);%0A
-  
             
@@ -1572,32 +1572,120 @@
 mail != null) %7B%0A
+                          // Add a flash notice if the account was successfully created%0A
                 
@@ -1779,32 +1779,98 @@
        %7D else %7B%0A
+                          // Add a flash alert if creation failed%0A
                 
@@ -2021,24 +2021,87 @@
        %7D);%0A%0A
+                  // a get /account displays the flash message%0A
             
@@ -2207,32 +2207,109 @@
 .get(request);%0A%0A
+                      // Display either the notice or alert to the user  ...%0A
                 
@@ -2511,24 +2511,24 @@
 h.alert());%0A
-
             
@@ -2538,32 +2538,76 @@
        %7D else %7B%0A
+                          // ... or nothing%0A
                 

Add javadoc to the Flash example