diff --git a/src/controllers/friends_controller.rs b/src/controllers/friends_controller.rs index 3037f16..c044479 100644 --- a/src/controllers/friends_controller.rs +++ b/src/controllers/friends_controller.rs @@ -116,4 +116,15 @@ pub fn respond_request(r: &mut HttpRequestHandler) -> RequestResult { // TODO : create a notification r.set_response("Response to the friendship request successfully saved!") +} + +/// Remove a friend from the list +pub fn remove_friend(r: &mut HttpRequestHandler) -> RequestResult { + let friend_id = r.post_user_id("friendID")?; + + friends_helper::remove_friendship(r.user_id_ref()?, &friend_id)?; + + // TODO : Delete any related notification + + r.success("The friend was removed from the list!") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index e7537fa..abc67b8 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -103,6 +103,8 @@ pub fn get_routes() -> Vec { Route::post("/friends/respondRequest", Box::new(friends_controller::respond_request)), + Route::post("/friends/remove", Box::new(friends_controller::remove_friend)), + // Conversations controller Route::post("/conversations/create", Box::new(conversations_controller::create)), diff --git a/src/helpers/friends_helper.rs b/src/helpers/friends_helper.rs index dee3a50..e817505 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -119,6 +119,21 @@ pub fn are_friend(user_one: &UserID, user_two: &UserID) -> ResultBoxError .cond_i64("actif", 1))? > 0) } +/// Delete a friendship +pub fn remove_friendship(user_one: &UserID, user_two: &UserID) -> ResultBoxError { + database::DeleteQuery::new(FRIENDS_TABLE) + .cond_user_id("ID_personne", user_one) + .cond_user_id("ID_amis", user_two) + .exec()?; + + database::DeleteQuery::new(FRIENDS_TABLE) + .cond_user_id("ID_personne", user_two) + .cond_user_id("ID_amis", user_one) + .exec()?; + + Ok(()) +} + /// Count the number of friends of a user pub fn count_friends(user_id: &UserID) -> ResultBoxError { QueryInfo::new(FRIENDS_TABLE)