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