import static spark.Spark.*;

import spark.Request;
import spark.Response;
import spark.Route;

import java.util.ArrayDeque;
import java.util.Deque;

public class HelloSpark {
    public static Deque<Article> articles = new ArrayDeque<Article>();

    public static void main(String[] args) {
        get(new Route("/") {
            @Override
            public Object handle(Request request, Response response) {
                String title = "My Blog";
                String createArticleLink = "<a href='/article/create'>Write Article</a>";
                StringBuilder html = new StringBuilder();

                html.append("<h1>").append(title).append("</h1>").append(createArticleLink);
                html.append("<hr>");

                if(HelloSpark.articles.isEmpty()) {
                    html.append("<b>No articles have been posted</b>");
                } else {
                    for(Article article : HelloSpark.articles) {
                        html.append("Title: ").append(article.getTitle())
                            .append("<br/>")
                            .append(article.getCreatedAt())
                            .append("<br/>")
                            .append("Summary: ").append(article.getSummaryLink())
                            .append("<br/>")
                            .append(article.getEditLink()).append(" | ").append(article.getDeleteLink())
                            .append("</p>");
                    }
                }

                return html.toString();
            }
        });

        get(new Route("/article/create") {
            @Override
            public Object handle(Request request, Response response) {
                StringBuilder form = new StringBuilder();

                form.append("<form id='article-create-form' method='POST' action='/article/create'>")
                        .append("Title: <input type='text' name='article-title' />")
                        .append("<br/>")
                        .append("Summary: <input type='text' name='article-summary' />")
                        .append("<br/>")
                    .append("</form>")
                    .append("<textarea name='article-content' rows='4' cols='50' form='article-create-form'></textarea>")
                    .append("<br/>")
                    .append("<input type='submit' value='Publish' form='article-create-form' />");

                return form.toString();
            }
        });

        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");

                Article article = new Article(title, summary, content, HelloSpark.articles.size() + 1);

                HelloSpark.articles.addFirst(article);

                response.status(201);
                response.redirect("/");
                return "";
            }
        });

        get(new Route("/article/read/:id") {
            @Override
            public Object handle(Request request, Response response) {
                Integer id = Integer.parseInt(request.params(":id"));
                StringBuilder html = new StringBuilder();

                for(Article article : HelloSpark.articles) {
                    if(id.equals(article.getId())) {
                        html.append("<a href='/'>Home</a>").append("<p />")
                            .append("Title: ").append(article.getTitle()).append("<br />")
                            .append(article.getCreatedAt())
                            .append("<p>").append(article.getContent()).append("</p>");
                        break;
                    }
                }
                return html.toString();
            }
        });

        get(new Route("/article/update/:id") {
            @Override
            public Object handle(Request request, Response response) {
                Integer id = Integer.parseInt(request.params(":id"));
                StringBuilder form = new StringBuilder();

                for(Article article : HelloSpark.articles) {
                    if(id.equals(article.getId())) {
                        form.append("<form id='article-create-form' method='POST' action='/article/update/:id'>")
                            .append("Title: <input type='text' name='article-title' value='").append(article.getTitle()).append("' />")
                            .append("<br/>")
                            .append("Summary: <input type='text' name='article-summary' value='").append(article.getSummary()).append("' />")
                            .append("<input type='hidden' name='article-id' value='").append(article.getId().toString()).append("' />")
                            .append("<br/>")
                            .append("</form>")
                            .append("<textarea name='article-content' rows='4' cols='50' form='article-create-form'>").append(article.getContent())
                            .append("</textarea>")
                            .append("<br/>")
                            .append("<input type='submit' value='Update' form='article-create-form' />");
                        break;
                    }
                }

                return form.toString();
            }
        });

        post(new Route("/article/update/:id") {
            @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");
                Integer id      = Integer.parseInt(request.queryParams("article-id"));

                for(Article article : HelloSpark.articles) {
                    if(id.equals(article.getId())) {
                        article.setTitle(title);
                        article.setContent(content);
                        article.setSummary(summary);
                    }
                }

                response.status(200);
                response.redirect("/");
                return "";
            }
        });
    }
}
@@ -936,32 +936,85 @@
 ark.articles) %7B%0A
+                        if(article.readable()) %7B%0A    
                 
@@ -1091,32 +1091,36 @@
                 
+ 
+   
 .append(%22%3Cbr/%3E%22)
@@ -1112,32 +1112,36 @@
 append(%22%3Cbr/%3E%22)%0A
+    
                 
@@ -1204,32 +1204,36 @@
                 
+    
 .append(%22%3Cbr/%3E%22)
@@ -1253,32 +1253,36 @@
                 
+   
+ 
 .append(%22Summary
@@ -1339,32 +1339,36 @@
                 
+    
 .append(%22%3Cbr/%3E%22)
@@ -1388,32 +1388,36 @@
                 
+  
+  
 .append(article.
@@ -1469,32 +1469,36 @@
 etDeleteLink())%0A
+    
                 
@@ -1518,32 +1518,58 @@
 append(%22%3C/p%3E%22);%0A
+                        %7D%0A
                 
@@ -6346,32 +6346,572 @@
              %7D%0A%0A
+                response.status(200);%0A                response.redirect(%22/%22);%0A                return %22%22;%0A            %7D%0A        %7D);%0A%0A        get(new Route(%22/article/delete/:id%22) %7B%0A            @Override%0A            public Object handle(Request request, Response response) %7B%0A                Integer id = Integer.parseInt(request.params(%22:id%22));%0A%0A                for(Article article : HelloSpark.articles) %7B%0A                    if(id.equals(article.getId())) %7B%0A                        article.delete();%0A                    %7D%0A                %7D%0A%0A
                 

Add spark_blog_step_4 CRUD Delete action