From baf568142d5acfb0a90df1783f4ab5223339ea75 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 30 Jun 2020 10:06:53 +0200 Subject: [PATCH] Can send friendship request --- src/controllers/friends_controller.rs | 25 +++++++++++++++++++++++++ src/controllers/routes.rs | 2 ++ src/helpers/friends_helper.rs | 11 ++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/controllers/friends_controller.rs b/src/controllers/friends_controller.rs index 120d51d..abe5761 100644 --- a/src/controllers/friends_controller.rs +++ b/src/controllers/friends_controller.rs @@ -60,4 +60,29 @@ pub fn get_status(r: &mut HttpRequestHandler) -> RequestResult { let status = friends_helper::get_status(&curr_user_id, &friend_id)?; r.set_response(FriendshipStatusAPI::new(&status)) +} + +/// Send a new friendship request +pub fn send_request(r: &mut HttpRequestHandler) -> RequestResult { + let friend_id = r.post_user_id("friendID")?; + + if friend_id == r.user_id()? { + r.forbidden("You can not sent friendship request to yourself!".to_string())?; + } + + let status = friends_helper::get_status(&r.user_id()?, &friend_id)?; + + if status.are_friend { + r.forbidden("You are already a friend of this person!".to_string())?; + } + + if status.sent_request || status.received_request { + r.forbidden("A friendship request is already in progress!".to_string())?; + } + + friends_helper::send_request(&r.user_id()?, &friend_id)?; + + // TODO : create a notification + + r.success("The friendship request was successfully sent!") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 1ee2817..80747ef 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -97,6 +97,8 @@ pub fn get_routes() -> Vec { Route::post("/friends/getStatus", Box::new(friends_controller::get_status)), + Route::post("/friends/sendRequest", Box::new(friends_controller::send_request)), + // 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 678abea..e1bb0c2 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -61,7 +61,16 @@ fn get_list(friend_query: &GetFriendsQuery) -> ResultBoxError> { query.exec(db_to_friend) } -/// Check out wheterher a user has sent a request to another user +/// Send a new friendship request +pub fn send_request(user_id: &UserID, target_user: &UserID) -> ResultBoxError { + database::InsertQuery::new(FRIENDS_TABLE) + .add_user_id("ID_personne", target_user) + .add_user_id("ID_amis", user_id) + .add_legacy_bool("actif", false) + .insert_drop_result() +} + +/// Check out whether a user has sent a request to another user pub fn sent_request(user_id: &UserID, target_user: &UserID) -> ResultBoxError { database::QueryInfo::new(FRIENDS_TABLE) .cond_user_id("ID_personne", target_user)