diff options
Diffstat (limited to 'src/guff')
-rw-r--r-- | src/guff/handlers/blog.cr | 44 | ||||
-rw-r--r-- | src/guff/models/post.cr | 17 | ||||
-rw-r--r-- | src/guff/post.cr | 10 | ||||
-rw-r--r-- | src/guff/results.cr | 4 | ||||
-rw-r--r-- | src/guff/views/ecrs/post.ecr | 17 |
5 files changed, 87 insertions, 5 deletions
diff --git a/src/guff/handlers/blog.cr b/src/guff/handlers/blog.cr index c95938c..2394354 100644 --- a/src/guff/handlers/blog.cr +++ b/src/guff/handlers/blog.cr @@ -1,4 +1,5 @@ require "../handler" +require "../views/html/page" class Guff::Handlers::BlogHandler < Guff::Handler ROUTES = [{ @@ -88,17 +89,52 @@ class Guff::Handlers::BlogHandler < Guff::Handler call_next(context) unless ROUTES.reduce(false) do |matched, route| unless matched if md = (route[:re] as Regex).match(path) - # matched route - matched = true - - context.response.puts "blog: route = %s, md = %s" % [ + # log blog route match + puts "blog: route = %s, md = %s" % [ route.to_s, md.to_s ] + + # search for matching posts + posts = @models.post.get_posts( + filters: get_filters(md), + tags: [["foo"]], + ) + + if posts.size > 0 + # mark as matched + matched = true + + # draw result + draw(context, route, posts) + end end end matched end end + + FILTERS = { + year: "year", + month: "month", + day: "day", + slug: "slug", + } + + private def get_filters(md) : Hash(Symbol, String) + FILTERS.reduce({} of Symbol => String) do |r, k, s| + r[k] = md[s] if md[s]? + r + end + end + + private def draw(context, route, posts) + # create page + page = PageHTMLView.new("Posts", posts.rows.map { |post| post.to_s }.join) + + # render page + context.response.content_type = page.content_type + context.response.puts page + end end diff --git a/src/guff/models/post.cr b/src/guff/models/post.cr index 580a9b7..3c4212b 100644 --- a/src/guff/models/post.cr +++ b/src/guff/models/post.cr @@ -96,6 +96,12 @@ module Guff clause: "a.post_id", }, + "slug": { + default: true, + sortable: true, + clause: "a.slug", + }, + "name": { default: true, sortable: true, @@ -103,7 +109,7 @@ module Guff }, "body": { - default: false, + default: true, sortable: true, clause: "a.body", }, @@ -215,6 +221,10 @@ module Guff strftime('%s', a.posted_at) + 0 = %d )" + SLUG_FILTER_CLAUSE = "( + a.slug = '%s' + )" + private def get_filter_clause( filters : Hash(Symbol, String) ) : String @@ -236,6 +246,11 @@ module Guff # TODO end + # add slug filter + if filters.has_key?(:slug) + r << SLUG_FILTER_CLAUSE % [@db.quote(filters[:slug])] + end + # return result r.join(" AND ") end diff --git a/src/guff/post.cr b/src/guff/post.cr index 735682c..703ed55 100644 --- a/src/guff/post.cr +++ b/src/guff/post.cr @@ -1,3 +1,6 @@ +require "html" +require "ecr/macros" + class Guff::Post def initialize(@row : Hash(String, ::SQLite3::Value)) end @@ -18,4 +21,11 @@ class Guff::Post [] of String end end + + private def h(s) : String + HTML.escape(s) + end + + # FIXME: does this belong elsewhere? + ECR.def_to_s("./src/guff/views/ecrs/post.ecr") end diff --git a/src/guff/results.cr b/src/guff/results.cr index 3fdc6e4..61d5ce5 100644 --- a/src/guff/results.cr +++ b/src/guff/results.cr @@ -25,5 +25,9 @@ module Guff rows: @rows, }.to_json end + + def size + @num_rows + end end end diff --git a/src/guff/views/ecrs/post.ecr b/src/guff/views/ecrs/post.ecr new file mode 100644 index 0000000..6a0f079 --- /dev/null +++ b/src/guff/views/ecrs/post.ecr @@ -0,0 +1,17 @@ +<div + id='p-<%= + # id + @row["post_id"] + %>' + class='post' +> + <div class='title'>t: <%= + # title + @row["name"] + %></div> + + <div class='body'>b: <%= + # body + @row["body"] + %> +</div> |