diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-03-08 13:07:57 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-03-08 13:07:57 -0500 |
commit | 46418b2237f86dc7b41c8c2a3dd5725634dceffe (patch) | |
tree | 1874bc0416d3f1530d91f249a487779ff95d4914 /src/guff/post-model.cr | |
parent | 1cbbb00e3c6bcf4255fee485de3f2828ff2d1d4c (diff) | |
download | old-guff-46418b2237f86dc7b41c8c2a3dd5725634dceffe.tar.bz2 old-guff-46418b2237f86dc7b41c8c2a3dd5725634dceffe.zip |
add date filters
Diffstat (limited to 'src/guff/post-model.cr')
-rw-r--r-- | src/guff/post-model.cr | 64 |
1 files changed, 55 insertions, 9 deletions
diff --git a/src/guff/post-model.cr b/src/guff/post-model.cr index 9200ccb..580a9b7 100644 --- a/src/guff/post-model.cr +++ b/src/guff/post-model.cr @@ -146,14 +146,14 @@ module Guff ##################### def get_posts( - cols = nil : Array(String)?, - site_id = nil : Int?, - q = "" : String, - tags = [] of Array(String) : Array(Array(String)), - page = 1 : Int, - limit = 20 : Int, - sort = "posted_at" : String, - dir = "desc" : String, + cols = nil : Array(String)?, + site_id = nil : Int?, + filters = {} of Symbol => String : Hash(Symbol, String), + tags = [] of Array(String) : Array(Array(String)), + page = 1 : Int, + limit = 20 : Int, + sort = "posted_at" : String, + dir = "desc" : String, ) # build sql args sql_args = { @@ -163,7 +163,7 @@ module Guff # build tmpl args tmpl_args = { # TODO - "filter": "1 = 1", # true + "filter": get_filter_clause(filters), "sort": get_sort_clause(sort), "dir": dir, "tags": get_tags_filter(tags), @@ -194,6 +194,52 @@ module Guff ) end + DATE_FILTERS = { + :year => { + :re => /^\d{4}$/, + :fmt => "%Y", + }, + + :month => { + :re => /^\d{1,2}$/, + :fmt => "%m", + }, + + :day => { + :re => /^\d{1,2}$/, + :fmt => "%d", + }, + } + + DATE_FILTER_CLAUSE = "( + strftime('%s', a.posted_at) + 0 = %d + )" + + private def get_filter_clause( + filters : Hash(Symbol, String) + ) : String + r = [ + "1 = 1" # true + ] + + DATE_FILTERS.each do |key, f| + if val = filters[key]? + # check value format + raise "invalid #{key} filter" unless val =~ (f[:re] as Regex) + + # add to filters + r << DATE_FILTER_CLAUSE % [f[:fmt] as String, val.to_i] + end + end + + if filters.has_key?(:q) + # TODO + end + + # return result + r.join(" AND ") + end + private def get_sort_clause( sort : String ) : String |