From 09adc06e1889806027af035509bce914268d3f22 Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Tue, 22 Nov 2022 17:48:15 +0100 Subject: [PATCH] Add follow status to GetUserProfile --- service/api/get-userprofile.go | 2 +- service/database/database.go | 2 +- service/database/db-profile.go | 7 ++++++- service/structures/api-structures.go | 1 + 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/service/api/get-userprofile.go b/service/api/get-userprofile.go index b0c393e..2ae5d75 100644 --- a/service/api/get-userprofile.go +++ b/service/api/get-userprofile.go @@ -22,7 +22,7 @@ func (rt *_router) GetUserProfile(w http.ResponseWriter, r *http.Request, ps htt } // Get user profile - status, profile, err := rt.db.GetUserProfile(uid) + status, profile, err := rt.db.GetUserProfile(uid, ctx.Auth.GetUserID()) if err != nil { helpers.SendInternalError(err, "Database error: GetUserProfile", w, rt.baseLogger) diff --git a/service/database/database.go b/service/database/database.go index 688d3f3..28c027f 100644 --- a/service/database/database.go +++ b/service/database/database.go @@ -65,7 +65,7 @@ type AppDatabase interface { LikePhoto(uid string, photo int64, liker_uid string) (QueryResult, error) UnlikePhoto(uid string, photo int64, liker_uid string) (QueryResult, error) - GetUserProfile(uid string) (QueryResult, *structures.UserProfile, error) + GetUserProfile(uid string, requesting_uid string) (QueryResult, *structures.UserProfile, error) GetUserPhotos(uid string, start_index int, limit int) (*[]structures.UserPhoto, error) GetUserStream(uid string, start_index int, limit int) (*[]structures.Photo, error) diff --git a/service/database/db-profile.go b/service/database/db-profile.go index b1e34e5..659383c 100644 --- a/service/database/db-profile.go +++ b/service/database/db-profile.go @@ -8,7 +8,7 @@ import ( //this should be changed, but we need to change OpenAPI first // Get user profile, including username, followers, following, and photos -func (db *appdbimpl) GetUserProfile(uid string) (QueryResult, *structures.UserProfile, error) { +func (db *appdbimpl) GetUserProfile(uid string, requesting_uid string) (QueryResult, *structures.UserProfile, error) { // Get user info var name string err := db.c.QueryRow(`SELECT "name" FROM "users" WHERE "uid" = ?`, uid).Scan(&name) @@ -46,11 +46,16 @@ func (db *appdbimpl) GetUserProfile(uid string) (QueryResult, *structures.UserPr return ERR_INTERNAL, nil, err } + // Get follow status + var follow_status bool + err = db.c.QueryRow(`SELECT EXISTS (SELECT * FROM "follows" WHERE "follower" = ? AND "followed" = ?)`, requesting_uid, uid).Scan(&follow_status) + return SUCCESS, &structures.UserProfile{ UID: uid, Name: name, Following: following, Followers: followers, + Followed: follow_status, Photos: photos, }, nil } diff --git a/service/structures/api-structures.go b/service/structures/api-structures.go index e255fa1..72d4e08 100644 --- a/service/structures/api-structures.go +++ b/service/structures/api-structures.go @@ -43,5 +43,6 @@ type UserProfile struct { Name string `json:"name"` Following int64 `json:"following"` Followers int64 `json:"followers"` + Followed bool `json:"followed"` Photos int64 `json:"photos"` //todo: check json names }