This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mongodb.*; | |
import java.sql.Date; | |
import java.util.ArrayList; | |
public class ArticleMongoDao<T extends Article> implements ArticleDbService<T> { | |
// A collection in Mongo can be thought of as a table in a relational DB | |
private DBCollection collection; | |
public ArticleMongoDao() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Article(String title, String summary, String content, Integer id, Date createdAt, Boolean deleted) { | |
this.title = title; | |
this.summary = summary; | |
this.content = content; | |
this.createdAt = createdAt; | |
this.id = id; | |
this.deleted = deleted; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
import java.util.ArrayList; | |
public class ArticlePostgresDao<T extends Article> implements ArticleDbService<T> { | |
// PostgreSQL connection to the database | |
private Connection conn; | |
// A raw SQL query used without parameters | |
private Statement stmt; | |
public ArticlePostgresDao() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static spark.Spark.*; | |
import spark.ModelAndView; | |
import spark.Request; | |
import spark.Response; | |
import spark.Route; | |
import spark.template.freemarker.FreeMarkerRoute; | |
import java.util.*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class ArticleServletDao<T extends Article> implements ArticleDbService<T> { | |
ArrayList<T> storage; | |
public ArticleServletDao() { | |
storage = new ArrayList<T>(); | |
} | |
@Override |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public interface ArticleDbService<T> { | |
public Boolean create(T entity); | |
public T readOne(int id); | |
public ArrayList<T> readAll(); | |
public Boolean update(int id, String title, String summary, String content); | |
public Boolean delete(int id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="starter-template"> | |
<form class="form-horizontal" role="form" id='article-create-form' method='POST' <#if article??>action="/article/update/:id"<#else>action="/article/create"</#if>> | |
<div class="form-group"> | |
<label class="col-sm-3 control-label" for="title">Title: </label> | |
<div class="col-sm-5"> | |
<input class="form-control" type='text' id="title" name='article-title' placeholder="Enter a new title" <#if article??>value="${article.getTitle()}"</#if> /> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label class="col-sm-3 control-label" for="summary">Summary: </label> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="starter-template"> | |
<h3>${article.getTitle()}</h3> | |
<h4>${article.getCreatedAt()}</h4> | |
<h4>${article.getEditLink()} | ${article.getDeleteLink()}</h4> | |
<h5>${article.getContent()}</h5> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
for(Article article : HelloSpark.articles) { | |
if(id.equals(article.getId())) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="starter-template"> | |
<form class="form-horizontal" role="form" id='article-create-form' method='POST' action='/article/create'> | |
<div class="form-group"> | |
<label class="col-sm-3 control-label" for="title">Title: </label> | |
<div class="col-sm-5"> | |
<input class="form-control" type='text' id="title" name='article-title' placeholder="Enter a new title" /> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label class="col-sm-3 control-label" for="summary">Summary: </label> |