diff options
Diffstat (limited to 'src/guff/api')
-rw-r--r-- | src/guff/api/methods.cr | 27 | ||||
-rw-r--r-- | src/guff/api/post.cr | 16 |
2 files changed, 34 insertions, 9 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, ) |