From c90f74e782e0e05a18f5bdbbdc2d6e73e7e1b55a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Jul 2020 08:21:40 +0200 Subject: [PATCH] Can create a comment --- src/api_data/mod.rs | 3 +- src/api_data/res_create_comment.rs | 20 ++++++++++ src/constants.rs | 5 ++- src/controllers/comments_controller.rs | 51 ++++++++++++++++++++++++++ src/controllers/mod.rs | 1 + src/controllers/routes.rs | 8 +++- src/helpers/comments_helper.rs | 18 ++++++++- 7 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 src/api_data/res_create_comment.rs create mode 100644 src/controllers/comments_controller.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 0c5a983..c33e174 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -36,4 +36,5 @@ pub mod survey_choice_api; pub mod survey_api; pub mod comment_api; pub mod res_create_post; -pub mod posts_targets_api; \ No newline at end of file +pub mod posts_targets_api; +pub mod res_create_comment; \ No newline at end of file diff --git a/src/api_data/res_create_comment.rs b/src/api_data/res_create_comment.rs new file mode 100644 index 0000000..d4e55f2 --- /dev/null +++ b/src/api_data/res_create_comment.rs @@ -0,0 +1,20 @@ +//! # Comment creation result +//! +//! @author Pierre Hubert +use serde::Serialize; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct ResCreateComment { + success: bool, + commentID: u64, +} + +impl ResCreateComment { + pub fn new(comment_id: u64) -> ResCreateComment { + ResCreateComment { + success: true, + commentID: comment_id, + } + } +} \ No newline at end of file diff --git a/src/constants.rs b/src/constants.rs index 6fd99ef..67985bc 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -72,5 +72,8 @@ pub const PATH_POST_IMAGES: &str = "imgpost"; /// The path were PDF included with posts should be stored pub const PATH_POST_PDF: &str = "post_pdf"; +/// The page where comments images are stored +pub const PATH_COMMENTS_IMAGES: &str = "imgcommentaire"; + /// Maximum requests size (50 Mo) -pub const MAX_REQUEST_SIZE: usize = 50000000; \ No newline at end of file +pub const MAX_REQUEST_SIZE: usize = 50000000; diff --git a/src/controllers/comments_controller.rs b/src/controllers/comments_controller.rs new file mode 100644 index 0000000..c49a89b --- /dev/null +++ b/src/controllers/comments_controller.rs @@ -0,0 +1,51 @@ +//! # Comments controller +//! +//! @author Pierre Hubert + +use crate::api_data::res_create_comment::ResCreateComment; +use crate::constants::PATH_COMMENTS_IMAGES; +use crate::controllers::routes::RequestResult; +use crate::data::comment::Comment; +use crate::data::http_request_handler::HttpRequestHandler; +use crate::data::post::PostAccessLevel; +use crate::helpers::{comments_helper, posts_helper}; +use crate::utils::date_utils::time; +use crate::utils::string_utils::remove_html_nodes; + +/// Create a new comment +pub fn create(r: &mut HttpRequestHandler) -> RequestResult { + let post = r.post_post_with_access("postID", PostAccessLevel::BASIC_ACCESS)?; + + if !posts_helper::allow_comments_on_post(&post)? { + r.forbidden("You can not create comments on this post!".to_string())?; + } + + let (content, image) = if r.has_file("image") { + ( + remove_html_nodes(&r.post_string("content")?), + Some(r.save_post_image("image", PATH_COMMENTS_IMAGES, 700, 700)?) + ) + } else { + ( + r.post_content("content", 3, true)?, + None + ) + }; + + + let comment = Comment { + id: 0, + time_sent: time(), + user_id: r.user_id()?, + post_id: post.id, + content, + image_path: image, + }; + + let comment_id = comments_helper::create(&comment)?; + + // TODO : Create notifications + // TODO : Remove notifications targeting current user about the post + + r.set_response(ResCreateComment::new(comment_id)) +} \ No newline at end of file diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index acf4cce..d107b94 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -9,5 +9,6 @@ pub mod conversations_controller; pub mod search_controller; pub mod groups_controller; pub mod posts_controller; +pub mod comments_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 8c79ff9..f8c9265 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -1,6 +1,6 @@ use std::error::Error; -use crate::controllers::{account_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, conversations_controller, friends_controller, groups_controller, movies_controller, posts_controller, search_controller, server_controller, user_controller, virtual_directory_controller, comments_controller}; use crate::controllers::routes::Method::{GET, POST}; use crate::data::http_request_handler::HttpRequestHandler; @@ -217,6 +217,12 @@ pub fn get_routes() -> Vec { Route::post("/posts/getAvailableTargets", Box::new(posts_controller::get_targets)), + + // Comments controller + Route::post("/comments/create", Box::new(comments_controller::create)), + + + // Movies controller Route::post("/movies/get_list", Box::new(movies_controller::get_list)), diff --git a/src/helpers/comments_helper.rs b/src/helpers/comments_helper.rs index 696d0e9..f9cf99a 100644 --- a/src/helpers/comments_helper.rs +++ b/src/helpers/comments_helper.rs @@ -4,11 +4,27 @@ use crate::constants::database_tables_names::COMMENTS_TABLE; use crate::data::comment::Comment; -use crate::data::error::ResultBoxError; +use crate::data::error::{ExecError, ResultBoxError}; use crate::helpers::{database, likes_helper}; use crate::helpers::likes_helper::LikeType; +use crate::utils::date_utils::mysql_date; use crate::utils::user_data_utils::user_data_path; +/// Create a new comment. In case of success, this function returns the ID of the created comment +pub fn create(c: &Comment) -> ResultBoxError { + let comment_id = database::InsertQuery::new(COMMENTS_TABLE) + .add_u64("ID_texte", c.post_id) + .add_user_id("ID_personne", &c.user_id) + .add_str("date_envoi", &mysql_date()) + .add_u64("time_insert", c.time_sent) + .add_str("commentaire", &c.content) + .add_opt_str("image_commentaire", c.image_path.as_ref()) + .insert()? + .ok_or(ExecError::new("No ID returned after comment creation!"))?; + + Ok(comment_id) +} + /// Get the comments of a post pub fn get(post_id: u64) -> ResultBoxError> { database::QueryInfo::new(COMMENTS_TABLE)