From 94529553ac724a11b6821dd1374c98b0f51b4f14 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 8 Mar 2016 23:22:03 -0500 Subject: initial page rendering --- src/guff/handlers/blog.cr | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'src/guff/handlers') 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 -- cgit v1.2.3