Skip to content

Commit

Permalink
feat(friend): friend endpoint now presents basic user info
Browse files Browse the repository at this point in the history
  • Loading branch information
Likqez committed Dec 4, 2024
1 parent c3498a9 commit 8e4cff3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
38 changes: 20 additions & 18 deletions DB/friendships.sql
Original file line number Diff line number Diff line change
Expand Up @@ -156,37 +156,39 @@ $$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION get_friends(user_id UUID)
RETURNS TABLE
(
friendship_id INT,
friend_id UUID,
status friendship_status,
action_user_id UUID,
created_at TIMESTAMP WITH TIME ZONE,
updated_at TIMESTAMP WITH TIME ZONE,
request_type TEXT
friendship_id INT,
friend_id UUID,
friend_username TEXT,
friend_avatar TEXT,
friend_spotify_id TEXT,
friend_spotify_visibility BOOLEAN,
status friendship_status,
action_user_id UUID,
created_at TIMESTAMP WITH TIME ZONE,
updated_at TIMESTAMP WITH TIME ZONE,
request_type TEXT
)
AS
$$
BEGIN
RETURN QUERY
SELECT f.friendship_id,
CASE
WHEN f.user1_id = user_id THEN f.user2_id
ELSE f.user1_id
END AS friend_id,
CASE WHEN f.user1_id = user_id THEN f.user2_id ELSE f.user1_id END AS friend_id,
u.username AS friend_username,
u.avatar_url AS friend_avatar,
u.spotify_id AS friend_spotify_id,
u.spotify_visibility AS friend_spotify_visibility,
f.status,
f.action_user_id,
f.created_at,
f.updated_at,
CASE
WHEN f.action_user_id = user_id THEN 'outgoing'
ELSE 'incoming'
END AS request_type
FROM friendships f
CASE WHEN f.action_user_id = user_id THEN 'outgoing' ELSE 'incoming' END AS request_type
FROM friendships f, users u
WHERE (f.user1_id = user_id OR f.user2_id = user_id)
AND (f.status != 'declined');
AND (f.status != 'declined')
AND (CASE WHEN f.user1_id = user_id THEN f.user2_id ELSE f.user1_id END = u.id);
END;
$$ LANGUAGE plpgsql;

-- examples
-- SELECT send_friend_request('sender', 'receiver');
-- SELECT accept_friend_request_by_id(4);
Expand Down
6 changes: 6 additions & 0 deletions types/api/user.friends.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// ----- Internal Types -----
import type * as url from "node:url";

export interface FriendError {
message: string
}
Expand All @@ -20,6 +22,10 @@ export interface FriendActionParam {
export interface GetFriendsResponse {
friendship_id: number,
friend_id: string,
friend_username: string,
friend_avatar: url.URL | null,
friend_spotify_id: string,
friend_spotify_visibility: boolean,
created_at: string
updated_at: string
status: FriendshipStatus,
Expand Down

0 comments on commit 8e4cff3

Please sign in to comment.