Updated restapi queries to properly sort and offset results

This commit is contained in:
Farhan Khan 2021-06-30 15:00:08 +00:00
parent 10ad5339a4
commit 395280c600

View File

@ -26,7 +26,6 @@ func search(w http.ResponseWriter, r *http.Request) {
log.Println("Treat as HTML") log.Println("Treat as HTML")
} }
var err error var err error
var rows pgx.Rows var rows pgx.Rows
var searchKey string var searchKey string
@ -42,15 +41,15 @@ func search(w http.ResponseWriter, r *http.Request) {
if exists_search && searchKey != "" { if exists_search && searchKey != "" {
if offsetKey == -1 { 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 { } 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 { } else {
if offsetKey == -1 { 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 { } 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() defer rows.Close()
var lastid int var earliestid int
lastid = 0 earliestid = 0
var activitiesJson []map[string]json.RawMessage var activitiesJson []map[string]json.RawMessage
for rows.Next() { for rows.Next() {
var id int var id int
@ -77,10 +76,10 @@ func search(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
if lastid == 0 { if earliestid == 0 {
lastid = id earliestid = id
} else if lastid > id { } else if earliestid > id {
lastid = id earliestid = id
} }
activityJson["actor"] = json.RawMessage(actorRaw) activityJson["actor"] = json.RawMessage(actorRaw)
@ -88,7 +87,7 @@ func search(w http.ResponseWriter, r *http.Request) {
} }
requestData := make(map[string]int) requestData := make(map[string]int)
requestData["lastid"] = lastid requestData["earliestid"] = earliestid
requestData["total_results"] = 9999 requestData["total_results"] = 9999
totalJson := make(map[string]interface{} ) totalJson := make(map[string]interface{} )
@ -106,6 +105,6 @@ func main() {
pool = getDbPool() pool = getDbPool()
http.HandleFunc("/search", search) http.HandleFunc("/search", search)
log.Print("Starting HTTP inbox on port 6432") log.Print("Starting HTTP inbox on port 6431")
log.Fatal(http.ListenAndServe("0.0.0.0:6432", nil)) log.Fatal(http.ListenAndServe("0.0.0.0:6431", nil))
} }