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")
}
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))
}