From 6c7905e647166e5ffbdfe44dad870b7c7d2f86d1 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 24 Oct 2016 17:11:44 -0700 Subject: initial implementation of sort results in API --- system/api/handlers.go | 66 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/system/api/handlers.go b/system/api/handlers.go index 0c9139f..aa3b936 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -5,6 +5,8 @@ import ( "encoding/json" "log" "net/http" + "strconv" + "strings" "github.com/bosssauce/ponzu/content" "github.com/bosssauce/ponzu/system/db" @@ -28,24 +30,72 @@ func typesHandler(res http.ResponseWriter, req *http.Request) { func postsHandler(res http.ResponseWriter, req *http.Request) { q := req.URL.Query() t := q.Get("type") - // TODO: implement pagination - // num := q.Get("num") - // page := q.Get("page") - - // TODO: inplement time-based ?after=time.Time, ?before=time.Time between=time.Time|time.Time - if t == "" { res.WriteHeader(http.StatusBadRequest) return } - posts := db.ContentAll(t) + count, err := strconv.Atoi(q.Get("count")) // int: determines number of posts to return (10 default, -1 is all) + if err != nil { + if q.Get("count") == "" { + count = 10 + } else { + res.WriteHeader(http.StatusInternalServerError) + return + } + } + + offset, err := strconv.Atoi(q.Get("offset")) // int: multiplier of count for pagination (0 default) + if err != nil { + if q.Get("offset") == "" { + count = 0 + } else { + res.WriteHeader(http.StatusInternalServerError) + return + } + } + + order := strings.ToUpper(q.Get("order")) // string: sort order of posts by timestamp ASC / DESC (DESC default) + if order != "ASC" || order == "" { + order = "DESC" + } + + // TODO: time-based ?after=time.Time, ?before=time.Time between=time.Time|time.Time + + posts := db.ContentAll(t + "_sorted") var all = []json.RawMessage{} for _, post := range posts { all = append(all, post) } - j, err := fmtJSON(all...) + var start, end int + switch count { + case -1: + start = 0 + end = len(posts) + + default: + start = count * offset + end = start + count + } + + // bounds check on posts given the start & end count + if start > len(posts) { + start = len(posts) - count + } + if end > len(posts) { + end = len(posts) + } + + // reverse the sorted order if ASC + if order == "ASC" { + all = []json.RawMessage{} + for i := len(posts) - 1; i >= 0; i-- { + all = append(all, posts[i]) + } + } + + j, err := fmtJSON(all[start:end]...) if err != nil { res.WriteHeader(http.StatusInternalServerError) return -- cgit v1.2.3 From 89e9d1ff9933c78be9719f881d98fce7de06f446 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 24 Oct 2016 17:18:31 -0700 Subject: debugging params for post queries --- system/api/handlers.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/system/api/handlers.go b/system/api/handlers.go index aa3b936..bbddc09 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -3,6 +3,7 @@ package api import ( "bytes" "encoding/json" + "fmt" "log" "net/http" "strconv" @@ -101,6 +102,12 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { return } + fmt.Println("count:", count) + fmt.Println("offset:", offset) + fmt.Println("order:", order) + fmt.Println("start:", start) + fmt.Println("end:", end) + sendData(res, j, http.StatusOK) } -- cgit v1.2.3 From e6e322b1f6c1b5d3ade5da2e1ca5cec608f72517 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 24 Oct 2016 17:21:49 -0700 Subject: debugging params for post queries --- system/api/handlers.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/system/api/handlers.go b/system/api/handlers.go index bbddc09..9e9fbb6 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -56,9 +56,9 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { } } - order := strings.ToUpper(q.Get("order")) // string: sort order of posts by timestamp ASC / DESC (DESC default) - if order != "ASC" || order == "" { - order = "DESC" + order := strings.ToLower(q.Get("order")) // string: sort order of posts by timestamp ASC / DESC (DESC default) + if order != "asc" || order == "" { + order = "desc" } // TODO: time-based ?after=time.Time, ?before=time.Time between=time.Time|time.Time @@ -69,6 +69,8 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { all = append(all, post) } + fmt.Println(len(posts)) + var start, end int switch count { case -1: @@ -89,7 +91,7 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { } // reverse the sorted order if ASC - if order == "ASC" { + if order == "asc" { all = []json.RawMessage{} for i := len(posts) - 1; i >= 0; i-- { all = append(all, posts[i]) -- cgit v1.2.3 From 954034af23f3855320b88a61fb61dbc9e65c464f Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 24 Oct 2016 17:24:16 -0700 Subject: fixing mistyped variable name --- system/api/handlers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/api/handlers.go b/system/api/handlers.go index 9e9fbb6..a63e649 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -49,7 +49,7 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { offset, err := strconv.Atoi(q.Get("offset")) // int: multiplier of count for pagination (0 default) if err != nil { if q.Get("offset") == "" { - count = 0 + offset = 0 } else { res.WriteHeader(http.StatusInternalServerError) return -- cgit v1.2.3 From 17ed6c7c2f06aa8f946e89d14381aa6fb40bc474 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 24 Oct 2016 17:26:36 -0700 Subject: remove debug printlns --- system/api/handlers.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/system/api/handlers.go b/system/api/handlers.go index a63e649..2ec82bb 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -104,12 +104,6 @@ func postsHandler(res http.ResponseWriter, req *http.Request) { return } - fmt.Println("count:", count) - fmt.Println("offset:", offset) - fmt.Println("order:", order) - fmt.Println("start:", start) - fmt.Println("end:", end) - sendData(res, j, http.StatusOK) } -- cgit v1.2.3