From f46d87def568171d25eccc71fe441c6f30663f6b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 1 Jan 2020 12:17:24 +0100 Subject: [PATCH] Can remove a friend from the list --- src/controllers/FriendsController.ts | 15 +++++++++++++++ src/controllers/Routes.ts | 2 ++ src/helpers/FriendsHelper.ts | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/controllers/FriendsController.ts b/src/controllers/FriendsController.ts index 8555d81..61db57b 100644 --- a/src/controllers/FriendsController.ts +++ b/src/controllers/FriendsController.ts @@ -152,6 +152,21 @@ export class FriendsController { h.success("Response to the friendship request successfully saved!"); } + /** + * Remove a friend from the list + * + * @param h Request handler + */ + public static async RemoveFriend(h: RequestHandler) { + const friendID = await h.postUserId("friendID"); + + await FriendsHelper.RemoveFriendship(h.getUserId(), friendID); + + // TODO : delete any related notification + + h.success("The friend was removed from the list!"); + } + /** * Turn a friend object into an API entry * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 6a898fd..8d383a1 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -89,6 +89,8 @@ export const Routes : Route[] = [ {path: "/friends/respondRequest", cb: (h) => FriendsController.RespondRequest(h)}, + {path: "/friends/remove", cb: (h) => FriendsController.RemoveFriend(h)}, + // Conversations controller {path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)}, diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts index 57d5234..d96fdf8 100644 --- a/src/helpers/FriendsHelper.ts +++ b/src/helpers/FriendsHelper.ts @@ -161,6 +161,24 @@ export class FriendsHelper { }); } + /** + * Destroy a friendship + * + * @param userOne The ID of the first user + * @param userTwo The ID of the second user + */ + public static async RemoveFriendship(userOne: number, userTwo: number) { + await DatabaseHelper.DeleteRows(FRIENDS_TABLE, { + ID_personne: userOne, + ID_amis: userTwo + }); + + await DatabaseHelper.DeleteRows(FRIENDS_TABLE, { + ID_personne: userTwo, + ID_amis: userOne + }); + } + /** * Count the number of friendship requests a user * received