From 7f1e563feb8efd97a54d943cb4ec4d58a454d7e8 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Fri, 15 Jul 2016 18:48:01 -0400 Subject: add blog id paging and point index at blog --- src/guff.cr | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'src/guff.cr') diff --git a/src/guff.cr b/src/guff.cr index 8ea29fa..87e58ad 100644 --- a/src/guff.cr +++ b/src/guff.cr @@ -858,6 +858,8 @@ module Guff -- TODO: handle posted_at and expired_at ORDER BY COALESCE(b.posted_at, b.created_at) DESC + + LIMIT ? OFFSET ? ", add: " @@ -896,14 +898,18 @@ module Guff (ids.size > 0) ? ids.first : nil end + # TODO: make this configurable + LIMIT = 50 + def get_ids( site_id : Int64, year : Int32? = nil, month : Int32? = nil, day : Int32? = nil, - slug : String? = nil + slug : String? = nil, + page : Int32? = nil ) : Array(Int64) - sql = [] of String + sql = ["1"] args = [site_id.to_s] # STDERR.puts "DEBUG: site_id = #{site_id}, year = #{year}, month = #{month}, day = #{day}, slug = \"#{slug}\"" @@ -932,6 +938,12 @@ module Guff args << slug end + page ||= 1 + raise "invalid page: #{page}" if page < 1 + + args << LIMIT.to_s + args << ((page - 1) * LIMIT).to_s + # STDERR.puts "DEBUG: args = #{args.to_json}" # exec query, build result @@ -2109,6 +2121,10 @@ module Guff class LogoutPageView < HTMLView ECR.def_to_s("src/views/logout-page.ecr") end + + class BlogListView < HTMLView + ECR.def_to_s("src/views/blog-list.ecr") + end end module Handlers @@ -2522,6 +2538,7 @@ module Guff class BlogPostHandler < Handler PATH_RE = %r{^ + (/blog)? /(?\d{4}) /(?\d{2}) /(?\d{2}) @@ -2564,11 +2581,15 @@ module Guff end class BlogListHandler < Handler - PATH_RE = %r{^ - /(?\d{4})/ + # TODO: make index page configurable + PATH_RE = %r{^/ + (blog/?)? ( - (?\d{2})/ - ((?\d{2})/)? + (?\d{4})/ + ( + (?\d{2})/ + ((?\d{2})/)? + )? )? $}x @@ -2593,9 +2614,10 @@ module Guff if site_id = get_site_id(request.headers["host"]?) r = @context.models.blog.get_ids( site_id: site_id, - year: md["year"].to_i, + year: md["year"]? ? md["year"].to_i : nil, month: md["month"]? ? md["month"].to_i : nil, day: md["day"]? ? md["day"].to_i : nil, + page: md["page"]? ? md["page"].to_i : nil, ) end end @@ -2685,12 +2707,12 @@ module Guff LogoutPageHandler.new(context) when :page PageHandler.new(context) - when :project - ProjectHandler.new(context) when :blog_post BlogPostHandler.new(context) when :blog_list BlogListHandler.new(context) + when :project + ProjectHandler.new(context) else raise "unknown handler id: #{handler_id}" end -- cgit v1.2.3