Skip to content

Instantly share code, notes, and snippets.

@taywils
Created December 9, 2013 01:55
Show Gist options
  • Save taywils/7866367 to your computer and use it in GitHub Desktop.
Save taywils/7866367 to your computer and use it in GitHub Desktop.
spark_storage_step_1_3
import static spark.Spark.*;
import spark.ModelAndView;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.template.freemarker.FreeMarkerRoute;
import java.util.*;
public class HelloSpark {
/* Refactor */
// Create a new ArticleDbService using the ArticleServletDao as the implmentation
// Since all of our DAO classes will implement the same interface we can just swap
// them out as we'll see later on
public static ArticleDbService<Article> articleDbService = new ArticleServletDao<Article>();
public static void main(String[] args) {
get(new FreeMarkerRoute("/") {
@Override
public ModelAndView handle(Request request, Response response) {
Map<String, Object> viewObjects = new HashMap<String, Object>();
/* Refactor */
ArrayList<Article> articles = articleDbService.readAll();
if (articles.isEmpty()) {
viewObjects.put("hasNoArticles", "Welcome, please click \"Write Article\" to begin.");
} else {
Deque<Article> showArticles = new ArrayDeque<Article>();
for (Article article : articles) {
if (article.readable()) {
showArticles.addFirst(article);
}
}
viewObjects.put("articles", showArticles);
}
viewObjects.put("templateName", "articleList.ftl");
return modelAndView(viewObjects, "layout.ftl");
}
});
get(new FreeMarkerRoute("/article/create") {
@Override
public Object handle(Request request, Response response) {
Map<String, Object> viewObjects = new HashMap<String, Object>();
viewObjects.put("templateName", "articleForm.ftl");
return modelAndView(viewObjects, "layout.ftl");
}
});
post(new Route("/article/create") {
@Override
public Object handle(Request request, Response response) {
String title = request.queryParams("article-title");
String summary = request.queryParams("article-summary");
String content = request.queryParams("article-content");
/* Refactor */
// Use the articleDbService.create method
Article article = new Article(title, summary, content, articleDbService.readAll().size());
articleDbService.create(article);
response.status(201);
response.redirect("/");
return "";
}
});
get(new FreeMarkerRoute("/article/read/:id") {
@Override
public Object handle(Request request, Response response) {
Integer id = Integer.parseInt(request.params(":id"));
Map<String, Object> viewObjects = new HashMap<String, Object>();
viewObjects.put("templateName", "articleRead.ftl");
viewObjects.put("article", articleDbService.readOne(id));
return modelAndView(viewObjects, "layout.ftl");
}
});
get(new FreeMarkerRoute("/article/update/:id") {
@Override
public Object handle(Request request, Response response) {
Integer id = Integer.parseInt(request.params(":id"));
Map<String, Object> viewObjects = new HashMap<String, Object>();
viewObjects.put("templateName", "articleForm.ftl");
/* Refactor */
// Use the readOne to fetch an article by its unique id
viewObjects.put("article", articleDbService.readOne(id));
return modelAndView(viewObjects, "layout.ftl");
}
});
post(new Route("/article/update/:id") {
@Override
public Object handle(Request request, Response response) {
Integer id = Integer.parseInt(request.queryParams("article-id"));
String title = request.queryParams("article-title");
String summary = request.queryParams("article-summary");
String content = request.queryParams("article-content");
/* Refactor */
// The articleDbService handles all the updating once it has
// be provided with the correct data
articleDbService.update(id, title, summary, content);
response.status(200);
response.redirect("/");
return "";
}
});
get(new Route("/article/delete/:id") {
@Override
public Object handle(Request request, Response response) {
Integer id = Integer.parseInt(request.params(":id"));
/* Refactor */
// Provide the unique article Id and then its deleted
articleDbService.delete(id);
response.status(200);
response.redirect("/");
return "";
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment