package com.chengjf.sparkdemo;

import static spark.Spark.after;
import static spark.Spark.before;
import static spark.Spark.get;
import static spark.Spark.post;
import static spark.Spark.staticFileLocation;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chengjf.sparkdemo.dao.DaoFactory;
import com.chengjf.sparkdemo.dao.TodoDao;
import com.chengjf.sparkdemo.dao.impl.TodoMybatisDao;
import com.chengjf.sparkdemo.model.Todo;

import spark.Filter;
import spark.ModelAndView;
import spark.Request;
import spark.Response;
import spark.Route;

/**
 * @author chengjf
 * @date 2015-08-27
 */
public class Hello {

	private static List<String> todoList = new ArrayList<String>();

	private static final Logger logger = LoggerFactory.getLogger(Hello.class);

	public static void main(String[] args) {

		// 设置静态文件路径
		staticFileLocation("/static");

		Bootstrap.boot();
		
//		before(new Filter() {
//
//			@Override
//			public void handle(Request req, Response res) {
//				logger.debug("req: " + req.pathInfo());
//			}
//		});
//
//		before(new Filter() {
//
//			@Override
//			public void handle(Request req, Response res) {
//				logger.debug("req2: " + req.pathInfo());
//			}
//		});
//		after(new Filter() {
//
//			@Override
//			public void handle(Request req, Response res) {
//				logger.debug("res: " + res.body());
//
//			}
//		});

		
		
		
		get(new Route("/") {

			@Override
			public Object handle(Request req, Response res) {
				res.redirect("/todo");
				return null;
			}
		});

		get(new Route("/hello/:name") {

			@Override
			public Object handle(Request req, Response res) {
				String name = req.params(":name");
				return "hello, " + name;
			}
		});

		get(new MyFreeMarkerRoute("/todo") {

			@Override
			public ModelAndView handle(Request arg0, Response arg1) {

				try {
					@SuppressWarnings({ "rawtypes", "unused" })
					Class clazz = Class.forName("org.sqlite.JDBC");
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				List<Todo> todos =DaoFactory.getInstance().getTodoDao().getAllTodos();
				Map<String, Object> model = new HashMap<String, Object>();
				model.put("todoList", todos);
				return modelAndView(model, "template/todoList.ftl");
			}
		});

		post(new Route("/todo/add") {

			@Override
			public Object handle(Request req, Response res) {
				String content = req.queryParams("content");
				// todoList.add(todo);
				// Map<String, Object> model = new HashMap<String, Object>();
				// model.put("todoList", todoList);

				Todo todo = new Todo();
				UUID uuid = UUID.randomUUID();
				todo.setId(uuid.toString());
				todo.setContent(content);
				todo.setCompleted(false);
				todo.setCreatedDate(new Date());
				DaoFactory.getInstance().getTodoDao().addTodo(todo);

				// redirect到查看页
				res.redirect("/todo");
				return null;
			}
		});

		get(new MyFreeMarkerRoute("/todo/add") {

			@Override
			public ModelAndView handle(Request arg0, Response arg1) {

				return modelAndView(null, "template/addTodo.ftl");
			}

		});

	}
}
@@ -29,75 +29,8 @@
 o;%0A%0A
-import static spark.Spark.after;%0Aimport static spark.Spark.before;%0A
 impo
@@ -139,36 +139,8 @@
 n;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -389,19 +389,18 @@
 emo.
-dao
+model
 .Todo
-Dao
 ;%0Aim
@@ -430,95 +430,33 @@
 emo.
-dao.impl.TodoMybatisDao;%0Aimport com.chengjf.sparkdemo.model.Todo;%0A%0Aimport spark.Filter;
+route.MyFreeMarkerRoute;%0A
 %0Aimp
@@ -618,74 +618,8 @@
  %7B%0A%0A
-%09private static List%3CString%3E todoList = new ArrayList%3CString%3E();%0A%0A
 %09pri
@@ -806,496 +806,8 @@
 ();%0A
-%09%09%0A//%09%09before(new Filter() %7B%0A//%0A//%09%09%09@Override%0A//%09%09%09public void handle(Request req, Response res) %7B%0A//%09%09%09%09logger.debug(%22req: %22 + req.pathInfo());%0A//%09%09%09%7D%0A//%09%09%7D);%0A//%0A//%09%09before(new Filter() %7B%0A//%0A//%09%09%09@Override%0A//%09%09%09public void handle(Request req, Response res) %7B%0A//%09%09%09%09logger.debug(%22req2: %22 + req.pathInfo());%0A//%09%09%09%7D%0A//%09%09%7D);%0A//%09%09after(new Filter() %7B%0A//%0A//%09%09%09@Override%0A//%09%09%09public void handle(Request req, Response res) %7B%0A//%09%09%09%09logger.debug(%22res: %22 + res.body());%0A//%0A//%09%09%09%7D%0A//%09%09%7D);%0A%0A%09%09%0A%09%09%0A%09%09
 %0A%09%09g
@@ -1249,233 +1249,8 @@
  %7B%0A%0A
-%09%09%09%09try %7B%0A%09%09%09%09%09@SuppressWarnings(%7B %22rawtypes%22, %22unused%22 %7D)%0A%09%09%09%09%09Class clazz = Class.forName(%22org.sqlite.JDBC%22);%0A%09%09%09%09%7D catch (ClassNotFoundException e) %7B%0A%09%09%09%09%09// TODO Auto-generated catch block%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%7D%0A
 %09%09%09%09
@@ -1267,16 +1267,17 @@
  todos =
+ 
 DaoFacto
@@ -1638,141 +1638,8 @@
 t%22);
-%0A%09%09%09%09// todoList.add(todo);%0A%09%09%09%09// Map%3CString, Object%3E model = new HashMap%3CString, Object%3E();%0A%09%09%09%09// model.put(%22todoList%22, todoList);
 %0A%0A%09%09

删除旧的Filter配置代码。