From 9f8c1fce2c94c9fb040586740e28b3e83a3cc41f Mon Sep 17 00:00:00 2001
From: Paul Duncan <pabs@pablotron.org>
Date: Wed, 9 Mar 2016 15:49:25 -0500
Subject: add created_at sorting and use json for sort fields

---
 src/guff/api/methods.cr | 33 +++++++++++++++++++++++++--------
 src/guff/api/post.cr    | 18 +++++++++++-------
 src/guff/api/util.cr    |  8 ++++++++
 3 files changed, 44 insertions(+), 15 deletions(-)

(limited to 'src/guff/api')

diff --git a/src/guff/api/methods.cr b/src/guff/api/methods.cr
index 6c38eb3..9b35596 100644
--- a/src/guff/api/methods.cr
+++ b/src/guff/api/methods.cr
@@ -15,20 +15,38 @@ module Guff
                 required: false,
               },
 
-              "year": {
-                text:     "Year filter",
+              "posted_year": {
+                text:     "Post date year filter",
                 type:     :int,
                 required: false,
               },
 
-              "month": {
-                text:     "Month filter",
+              "posted_month": {
+                text:     "Post date month filter",
                 type:     :int,
                 required: false,
               },
 
-              "day": {
-                text:     "Day filter",
+              "posted_day": {
+                text:     "Post date day filter",
+                type:     :int,
+                required: false,
+              },
+
+              "created_year": {
+                text:     "Creation date year filter",
+                type:     :int,
+                required: false,
+              },
+
+              "created_month": {
+                text:     "Creation date month filter",
+                type:     :int,
+                required: false,
+              },
+
+              "created_day": {
+                text:     "Creation date day filter",
                 type:     :int,
                 required: false,
               },
@@ -48,7 +66,7 @@ module Guff
 
               "sort": {
                 text:     "Sort order of results",
-                type:     :sort,
+                type:     :json,
                 required: false,
               },
 
@@ -340,7 +358,6 @@ module Guff
         slug:     /^[a-z0-9\.-]+$/,
         int:      /^\d+$/,
         int_list: /^\d+(?:,\d+)*$/,
-        sort:     /^[a-z0-9_]+,(?:asc|desc)$/,
         state:    /^(?:draft|posted|deleted)$/,
 
         # FIXME: lock these down more
diff --git a/src/guff/api/post.cr b/src/guff/api/post.cr
index 499e7c0..f856d5b 100644
--- a/src/guff/api/post.cr
+++ b/src/guff/api/post.cr
@@ -4,12 +4,15 @@ module Guff
   module API
     module PostAPI
       GET_POSTS_FILTERS = {
-        q:      "q",
-        year:   "year",
-        month:  "month",
-        day:    "day",
-        slug:   "slug",
-        state:  "state",
+        q:              "q",
+        posted_year:    "posted_year",
+        posted_month:   "posted_month",
+        posted_day:     "posted_day",
+        created_year:   "created_year",
+        created_month:  "created_month",
+        created_day:    "created_day",
+        slug:           "slug",
+        state:          "state",
       }
 
       private def do_post_get_posts(
@@ -26,8 +29,9 @@ module Guff
         @models.post.get_posts(
           site_id:  get_site(context),
           filters:  filters,
-          tags:     get_posts_tags(args["tags"]),
+          tags:     get_posts_tags(args["tags"]?),
           page:     args.has_key?("page") ? args["page"].to_i : 1,
+          sort:     get_posts_sort(args["sort"]?),
         )
       end
 
diff --git a/src/guff/api/util.cr b/src/guff/api/util.cr
index 525f098..72e7e5c 100644
--- a/src/guff/api/util.cr
+++ b/src/guff/api/util.cr
@@ -26,6 +26,14 @@ module Guff
           [] of Array(String)
         end
       end
+
+      private def get_posts_sort(
+        s : String?
+      ) : Array(Hash(String, String))?
+        if s && s.size > 0
+          Array(Hash(String, String)).from_json(s)
+        end
+      end
     end
   end
 end
-- 
cgit v1.2.3