diff --git a/src/api_data/user_info.rs b/src/api_data/user_info.rs index 942cffb..26ddf11 100644 --- a/src/api_data/user_info.rs +++ b/src/api_data/user_info.rs @@ -39,7 +39,7 @@ struct APIAdvancedInfo { backgroundImage: String, number_friends: usize, pageLikes: usize, - user_page_like: bool, + user_like_page: bool, can_post_texts: bool, } @@ -89,7 +89,7 @@ impl APIUserInfo { backgroundImage: background_image_helper::get_url(&info.id), number_friends, pageLikes: likes_helper::count(info.id.id(), LikeType::USER)?, - user_page_like: likes_page, + user_like_page: likes_page, can_post_texts: user_helper::can_create_posts(&curr_user_id, &info.id)?, }); diff --git a/src/controllers/likes_controller.rs b/src/controllers/likes_controller.rs new file mode 100644 index 0000000..b407d2c --- /dev/null +++ b/src/controllers/likes_controller.rs @@ -0,0 +1,41 @@ +//! # Likes controller +//! +//! @author Pierre Hubert + +use crate::controllers::routes::RequestResult; +use crate::data::http_request_handler::HttpRequestHandler; +use crate::data::error::ExecError; +use crate::helpers::likes_helper::LikeType; +use crate::helpers::{user_helper, likes_helper}; + +struct LikeTarget(u64, LikeType); + +/// Update like status +pub fn update(r: &mut HttpRequestHandler) -> RequestResult { + let req_type = r.post_string("type")?; + let is_liking = r.post_bool("like")?; + + let target = match req_type.as_str() { + + // In case of user + "user" => { + let user_id = r.post_user_id("id")?; + + if !user_helper::can_see_user_page(&r.user_id_or_invalid(), &user_id)? { + r.forbidden("You cannot access this user information!".to_string())?; + } + + LikeTarget(user_id.id(), LikeType::USER) + } + + + _ => { + r.internal_error(ExecError::boxed_new("Unsupported like type!"))?; + unreachable!(); + } + }; + + likes_helper::update(r.user_id_ref()?, is_liking, target.0, target.1)?; + + r.success("") +} \ No newline at end of file diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index d107b94..30ff232 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -10,5 +10,6 @@ pub mod search_controller; pub mod groups_controller; pub mod posts_controller; pub mod comments_controller; +pub mod likes_controller; pub mod movies_controller; pub mod virtual_directory_controller; \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 04cba51..f2ee9ee 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -1,6 +1,6 @@ use std::error::Error; -use crate::controllers::{account_controller, comments_controller, conversations_controller, friends_controller, groups_controller, movies_controller, posts_controller, search_controller, server_controller, user_controller, virtual_directory_controller}; +use crate::controllers::{account_controller, comments_controller, conversations_controller, friends_controller, groups_controller, likes_controller, movies_controller, posts_controller, search_controller, server_controller, user_controller, virtual_directory_controller}; use crate::controllers::routes::Method::{GET, POST}; use crate::data::http_request_handler::HttpRequestHandler; @@ -228,6 +228,9 @@ pub fn get_routes() -> Vec { Route::post("/comments/delete", Box::new(comments_controller::delete)), + // Likes controller + Route::post("/likes/update", Box::new(likes_controller::update)), + // Movies controller Route::post("/movies/get_list", Box::new(movies_controller::get_list)), diff --git a/src/helpers/likes_helper.rs b/src/helpers/likes_helper.rs index 37f55e4..85d98a5 100644 --- a/src/helpers/likes_helper.rs +++ b/src/helpers/likes_helper.rs @@ -7,7 +7,9 @@ use crate::data::error::ResultBoxError; use crate::data::user::UserID; use crate::helpers::database; use crate::helpers::database::QueryInfo; +use crate::utils::date_utils::mysql_date; +#[derive(Copy, Clone)] pub enum LikeType { USER, POST, @@ -48,6 +50,29 @@ pub fn is_liking(user_id: &UserID, id: u64, kind: LikeType) -> ResultBoxError 0) } +/// Update like status +pub fn update(user_id: &UserID, liking: bool, id: u64, kind: LikeType) -> ResultBoxError { + if !liking { + return database::DeleteQuery::new(LIKES_TABLE) + .cond_user_id("ID_personne", user_id) + .cond_u64("ID_type", id) + .cond_str("type", &kind.to_db_type()) + .exec(); + } + + + if is_liking(user_id, id, kind)? { + return Ok(()); + } + + database::InsertQuery::new(LIKES_TABLE) + .add_user_id("ID_personne", user_id) + .add_u64("ID_type", id) + .add_str("Date_envoi", &mysql_date()) + .add_str("type", &kind.to_db_type()) + .insert_drop_result() +} + /// Delete all the likes associated with a post pub fn delete_all(id: u64, kind: LikeType) -> ResultBoxError { database::DeleteQuery::new(LIKES_TABLE)