diff --git a/DB/friendships.sql b/DB/friendships.sql index 5be07d3..4fd56e0 100644 --- a/DB/friendships.sql +++ b/DB/friendships.sql @@ -137,6 +137,20 @@ BEGIN END; $$ LANGUAGE plpgsql; +-- 2nd version with id +CREATE OR REPLACE FUNCTION remove_friend_by_id(friendship_id_param INT) RETURNS void AS +$$ +BEGIN + DELETE + FROM friendships + WHERE friendship_id = friendship_id_param + AND status = 'accepted'; + + IF NOT FOUND THEN + RAISE EXCEPTION 'No active friendship found with this ID'; + END IF; +END; +$$ LANGUAGE plpgsql; -- retrieve all friends, incoming and outgoing friend requests CREATE OR REPLACE FUNCTION get_friends(user_id UUID) @@ -147,7 +161,8 @@ CREATE OR REPLACE FUNCTION get_friends(user_id UUID) status friendship_status, action_user_id UUID, created_at TIMESTAMP WITH TIME ZONE, - updated_at TIMESTAMP WITH TIME ZONE + updated_at TIMESTAMP WITH TIME ZONE, + request_type TEXT ) AS $$ @@ -161,7 +176,11 @@ BEGIN f.status, f.action_user_id, f.created_at, - f.updated_at + f.updated_at, + CASE + WHEN f.action_user_id = user_id THEN 'outgoing' + ELSE 'incoming' + END AS request_type FROM friendships f WHERE (f.user1_id = user_id OR f.user2_id = user_id) AND (f.status != 'declined'); diff --git a/pages/testfriends.vue b/pages/testfriends.vue index ba40d00..aa615e0 100644 --- a/pages/testfriends.vue +++ b/pages/testfriends.vue @@ -3,10 +3,11 @@ import { type FriendActionRequest, FriendshipAction, FriendshipStatus, + FriendshipType, type GetFriendsResponse } from "@/types/api/user.friends"; -const requests: Ref = useState("all_friendships", ()=> []) +const requests: Ref = useState("incoming_friendships", ()=> []) const friends: Ref = useState("accepted_friendships", () => []) const session = useSupabaseSession() @@ -20,7 +21,7 @@ onMounted(async () => { async function getFriendships() { $fetch('http://localhost:3000/api/v1/user/friends') .then((data) => { - requests.value = data.filter((item) => item.status === FriendshipStatus.PENDING) + requests.value = data.filter((item) =>item.request_type == FriendshipType.INCOMING && item.status === FriendshipStatus.PENDING) friends.value = data.filter((item) => item.status === FriendshipStatus.ACCEPTED) }); } @@ -36,6 +37,10 @@ function handleFriendRequestAction(action: FriendshipAction, friendship_id: numb // optimistically update the UI instantly requests.value = requests.value.filter((item) => item.friendship_id !== friendship_id) getFriendships() // lazily refresh the list + } else if (action === FriendshipAction.REMOVE) { + // optimistically update the UI instantly + friends.value = friends.value.filter((item) => item.friendship_id !== friendship_id) + getFriendships() // lazily refresh the list } } @@ -68,6 +73,20 @@ function decline(friendship_id: number) { }) } +function remove(friendship_id: number) { + const actionRequest: FriendActionRequest = { + friendship_id: friendship_id, + action: FriendshipAction.REMOVE + } + + $fetch('http://localhost:3000/api/v1/user/friends/action', { + method: 'POST', + body: JSON.stringify(actionRequest) + }).then(() => { + handleFriendRequestAction(FriendshipAction.REMOVE, friendship_id) + }) +} +