aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-03-08 13:07:57 -0500
committerPaul Duncan <pabs@pablotron.org>2016-03-08 13:07:57 -0500
commit46418b2237f86dc7b41c8c2a3dd5725634dceffe (patch)
tree1874bc0416d3f1530d91f249a487779ff95d4914
parent1cbbb00e3c6bcf4255fee485de3f2828ff2d1d4c (diff)
downloadold-guff-46418b2237f86dc7b41c8c2a3dd5725634dceffe.tar.bz2
old-guff-46418b2237f86dc7b41c8c2a3dd5725634dceffe.zip
add date filters
-rw-r--r--src/guff/api/methods.cr27
-rw-r--r--src/guff/api/post.cr16
-rw-r--r--src/guff/post-model.cr64
3 files changed, 89 insertions, 18 deletions
diff --git a/src/guff/api/methods.cr b/src/guff/api/methods.cr
index 758cca9..f3a7f3c 100644
--- a/src/guff/api/methods.cr
+++ b/src/guff/api/methods.cr
@@ -16,6 +16,24 @@ module Guff
default: "",
},
+ "year": {
+ text: "Year filter",
+ type: :int,
+ required: false,
+ },
+
+ "month": {
+ text: "Month filter",
+ type: :int,
+ required: false,
+ },
+
+ "day": {
+ text: "Day filter",
+ type: :int,
+ required: false,
+ },
+
"page": {
text: "Page number",
type: :int,
@@ -338,14 +356,7 @@ module Guff
end
# get value
- val = if arg_data[:required]
- params.fetch(arg_name)
- # elsif arg_data.has_key?(:default)
- elsif arg_data[:default]?
- params.fetch(arg_name, arg_data[:default] as String)
- else
- nil
- end
+ val = params.fetch(arg_name, arg_data[:default]?) as String?
if val
# check value
diff --git a/src/guff/api/post.cr b/src/guff/api/post.cr
index 7141530..55abd86 100644
--- a/src/guff/api/post.cr
+++ b/src/guff/api/post.cr
@@ -3,13 +3,27 @@ require "json"
module Guff
module API
module PostAPI
+ GET_POSTS_FILTERS = {
+ q: "q",
+ year: "year",
+ month: "month",
+ day: "day",
+ }
+
private def do_post_get_posts(
context : HTTP::Server::Context,
args : Hash(String, String)
)
+ # build filters
+ filters = GET_POSTS_FILTERS.reduce({} of Symbol => String) do |r, k, s|
+ r[k] = args[s] if args.has_key?(s)
+ r
+ end
+
+ # get and return posts
@models.post.get_posts(
site_id: get_site(context),
- q: args["q"]? || "",
+ filters: filters,
tags: get_posts_tags(args["tags"]),
page: args.has_key?("page") ? args["page"].to_i : 1,
)
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