diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/guff/handlers/test.cr | 44 | ||||
-rw-r--r-- | src/guff/post.cr | 2 | ||||
-rw-r--r-- | src/guff/views/ecrs/test.ecr | 167 | ||||
-rw-r--r-- | src/guff/views/html/test.cr | 33 |
4 files changed, 243 insertions, 3 deletions
diff --git a/src/guff/handlers/test.cr b/src/guff/handlers/test.cr index f373630..f3ba35c 100644 --- a/src/guff/handlers/test.cr +++ b/src/guff/handlers/test.cr @@ -1,12 +1,50 @@ require "../handler" +require "../views/html/test" class Guff::Handlers::TestHandler < Guff::Handler def call(context : HTTP::Server::Context) - if ((context.request.path || "").match(/^\/test\//)) - context.response.content_type = "text/html" - context.response.puts "test" + case (context.request.path || "") as String + when /^\/test\/?$/ + draw_page(context) + when /^\/test\/set_state$/ + set_state(context) + when /^\/test\/add_post$/ + add_post(context) else call_next(context) end end + + private def draw_page(context) + TestHTMLView.run(@models, context) + end + + private def add_post(context) + params = HTTP::Params.parse(context.request.body as String) + + @models.post.add_post( + name: params["name"].to_s, + slug: params["slug"].to_s, + body: params["body"].to_s, + tags: ["_blog"] + (params["tags"] || "").to_s.split(" "), + ) + + redirect(context) + end + + private def set_state(context) + params = HTTP::Params.parse(context.request.body as String) + + @models.post.update_post( + post_id: params["post_id"].to_i, + state: params["state"].to_s, + ) + + redirect(context) + end + + private def redirect(context) + context.response.status_code = 302 + context.response.headers["location"] = "/test" + end end diff --git a/src/guff/post.cr b/src/guff/post.cr index 3a63163..5aa7170 100644 --- a/src/guff/post.cr +++ b/src/guff/post.cr @@ -2,6 +2,8 @@ require "html" require "ecr/macros" class Guff::Post + getter :row + def initialize(@row : Hash(String, ::SQLite3::Value)) end diff --git a/src/guff/views/ecrs/test.ecr b/src/guff/views/ecrs/test.ecr new file mode 100644 index 0000000..d36b62c --- /dev/null +++ b/src/guff/views/ecrs/test.ecr @@ -0,0 +1,167 @@ +<h1><%= h(TITLE) %></h1> + +<% + [{ + id: "draft", + name: "Draft Posts", + + actions: [{ + id: "posted", + name: "Post", + }, { + id: "deleted", + name: "Delete", + }], + }, { + id: "posted", + name: "Posted", + + actions: [{ + id: "draft", + name: "Draft", + }, { + id: "deleted", + name: "Delete", + }], + }, { + id: "deleted", + name: "Deleted Posts", + + actions: [{ + id: "draft", + name: "Draft", + }], + }].each do |kind| +%> + <h2><%= + h(kind[:name] as String) + %></h2> + + <table> + <thead> + <tr> + <th>ID</th> + <th>Created At</th> + <th>Name</th> + <th>Body</th> + <th colspan='2'>Actions</th> + </tr> + </thead> + + <tbody><% posts(kind[:id] as String).rows.each do |post| %> + <tr> + <td><%= + h(post.row["post_id"].to_s) + %></td> + + <td><%= + h(post.row["created_at"].to_s) + %></td> + + <td><%= + h(post.row["name"].to_s) + %></td> + + <td><%= + post.row["body"].to_s + %></td> + + <% (kind[:actions] as Array(Hash(Symbol, String))).each do |a| %> + <td> + <form + method='post' + action='/test/set_state' + > + <input + type='hidden' + name='post_id' + value='<%= h(post.row["post_id"].to_s) %>' + /> + + <input + type='hidden' + name='state' + value='<%= a[:id] %>' + </input> + + <input + type='submit' + title='Set post to <%= h(a[:id]) %>..' + value='<%= h(a[:name]) %>' + /> + </form> + </td> + <% end %> + </tr> + <% end %></tbody> + </table> +<% end %> + +<div class='section'> + <form + method='post' + action='/test/add_post' + > + <label for='post-name'> + Name + </label><br/> + + <input + type='text' + id='post-name' + name='name' + title='Enter post title' + placeholder='Post Title' + value='' + /> + + <br/> + + <label for='post-slug'> + Slug + </label><br/> + + <input + type='text' + id='post-slug' + name='slug' + title='Enter post slug' + placeholder='post-slug' + value='' + /> + + <br/> + + <label for='post-body'> + Post Body + </label><br/> + + <textarea + id='post-body' + name='body' + ></textarea> + + <br/> + + <label for='post-tags'> + Tags (space-delimited) + </label><br/> + + <input + type='text' + id='post-tags' + name='tags' + title='Enter post tags' + placeholder='some post tags' + value='' + /> + + <br/> + + <input + type='submit' + title='Create new post.' + value='Add Post' + /> + </form> +</div> diff --git a/src/guff/views/html/test.cr b/src/guff/views/html/test.cr new file mode 100644 index 0000000..e31ebdb --- /dev/null +++ b/src/guff/views/html/test.cr @@ -0,0 +1,33 @@ +require "html" +require "ecr/macros" +require "./page" + +class Guff::TestHTMLView + TITLE = "Guff Tests" + + def self.run(models, context : HTTP::Server::Context) + new(models).run(context) + end + + def initialize(@models : Models) + end + + def run(context) + page = PageHTMLView.new(TITLE, self.to_s) + context.response.content_type = page.content_type + context.response.puts page + end + + private def posts(state : String) + @models.post.get_posts( + tags: [["_blog"]], + filters: { state: state } + ) + end + + def h(s : String) + HTML.escape(s || "") + end + + ECR.def_to_s("./src/guff/views/ecrs/test.ecr") +end |