From 395280c600afc684e42b4cf82b27375047ebcac4 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Wed, 30 Jun 2021 15:00:08 +0000 Subject: [PATCH] Updated restapi queries to properly sort and offset results --- restapi/restapi.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/restapi/restapi.go b/restapi/restapi.go index 64aad09..f54485f 100644 --- a/restapi/restapi.go +++ b/restapi/restapi.go @@ -26,7 +26,6 @@ func search(w http.ResponseWriter, r *http.Request) { log.Println("Treat as HTML") } - var err error var rows pgx.Rows var searchKey string @@ -42,15 +41,15 @@ func search(w http.ResponseWriter, r *http.Request) { if exists_search && searchKey != "" { if offsetKey == -1 { - rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) ORDER BY activities.identifiedat DESC LIMIT 10", searchKey) + rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) ORDER BY activities.id DESC LIMIT 10", searchKey) } else { - rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) ORDER BY activities.identifiedat DESC LIMIT 10 OFFSET $2", searchKey, offsetKey) + rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) AND activities.id < $2 ORDER BY activities.id DESC LIMIT 10", searchKey, offsetKey) } } else { if offsetKey == -1 { - rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' ORDER BY activities.identifiedat DESC LIMIT 10") + rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' ORDER BY activities.id DESC LIMIT 10") } else { - rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' ORDER BY activities.identifiedat DESC OFFSET $2 LIMIT 10 FETCH FIRST", offsetKey) + rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' AND activities.id < $1 ORDER BY activities.id DESC LIMIT 10", offsetKey) } } @@ -59,8 +58,8 @@ func search(w http.ResponseWriter, r *http.Request) { } defer rows.Close() - var lastid int - lastid = 0 + var earliestid int + earliestid = 0 var activitiesJson []map[string]json.RawMessage for rows.Next() { var id int @@ -77,10 +76,10 @@ func search(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Println(err) } - if lastid == 0 { - lastid = id - } else if lastid > id { - lastid = id + if earliestid == 0 { + earliestid = id + } else if earliestid > id { + earliestid = id } activityJson["actor"] = json.RawMessage(actorRaw) @@ -88,7 +87,7 @@ func search(w http.ResponseWriter, r *http.Request) { } requestData := make(map[string]int) - requestData["lastid"] = lastid + requestData["earliestid"] = earliestid requestData["total_results"] = 9999 totalJson := make(map[string]interface{} ) @@ -106,6 +105,6 @@ func main() { pool = getDbPool() http.HandleFunc("/search", search) - log.Print("Starting HTTP inbox on port 6432") - log.Fatal(http.ListenAndServe("0.0.0.0:6432", nil)) + log.Print("Starting HTTP inbox on port 6431") + log.Fatal(http.ListenAndServe("0.0.0.0:6431", nil)) }