diff --git a/service/database/database.go b/service/database/database.go index efaf06d..f70b464 100644 --- a/service/database/database.go +++ b/service/database/database.go @@ -45,7 +45,7 @@ type AppDatabase interface { UserExistsNotBanned(uid string, requesting_uid string) (bool, error) GetUserID(name string) (string, error) - SearchByName(name string, requesting_uid string, start_index int, limit int) (*[]structures.UIDName, error) + SearchByName(name string, requesting_uid string, start_index int, limit int) (*[]structures.SearchResult, error) UpdateUsername(uid string, name string) (QueryResult, error) diff --git a/service/database/db-users.go b/service/database/db-users.go index 9f9f497..ab518ac 100644 --- a/service/database/db-users.go +++ b/service/database/db-users.go @@ -284,9 +284,25 @@ func (db *appdbimpl) GetUserBans(uid string, start_index int, limit int) (*[]str } // Search by name -func (db *appdbimpl) SearchByName(name string, requesting_uid string, start_index int, limit int) (*[]structures.UIDName, error) { +func (db *appdbimpl) SearchByName(name string, requesting_uid string, start_index int, limit int) (*[]structures.SearchResult, error) { - rows, err := db.c.Query(`SELECT "uid", "name" FROM "users" + rows, err := db.c.Query(`SELECT "uid", "name", + ( + SELECT EXISTS( + SELECT * FROM "follows" AS "f" + WHERE "f"."follower" = "users"."uid" + AND "f"."followed" = ? + ) + ), + ( + SELECT EXISTS( + SELECT * FROM "bans" AS "b" + WHERE "b"."user" = "users"."uid" + AND "b"."ban" = ? + ) + ) + + FROM "users" WHERE "name" LIKE '%' || ? || '%' AND "uid" NOT IN ( @@ -295,13 +311,28 @@ func (db *appdbimpl) SearchByName(name string, requesting_uid string, start_inde AND "bans"."ban" = ? ) LIMIT ? - OFFSET ?`, name, requesting_uid, limit, start_index) - - users, err := db.uidNameQuery(rows, err) + OFFSET ?`, requesting_uid, requesting_uid, name, requesting_uid, limit, start_index) if err != nil { return nil, err } - return users, nil + var search_data []structures.SearchResult = make([]structures.SearchResult, 0) + + defer rows.Close() + + for rows.Next() { + var search_entry structures.SearchResult + err = rows.Scan(&search_entry.UID, &search_entry.Name, &search_entry.Followed, &search_entry.Banned) + if err != nil { + return nil, err + } + search_data = append(search_data, search_entry) + } + // We check if the iteration ended prematurely + if err = rows.Err(); err != nil { + return nil, err + } + + return &search_data, nil } diff --git a/service/structures/api-structures.go b/service/structures/api-structures.go index 74a923c..fca721f 100644 --- a/service/structures/api-structures.go +++ b/service/structures/api-structures.go @@ -9,6 +9,13 @@ type UIDName struct { Name string `json:"name"` } +type SearchResult struct { + UID string `json:"user_id"` + Name string `json:"name"` + Followed bool `json:"followed"` + Banned bool `json:"banned"` +} + type GenericResponse struct { Status string `json:"status"` }