mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Can create a comment
This commit is contained in:
		@@ -37,3 +37,4 @@ pub mod survey_api;
 | 
			
		||||
pub mod comment_api;
 | 
			
		||||
pub mod res_create_post;
 | 
			
		||||
pub mod posts_targets_api;
 | 
			
		||||
pub mod res_create_comment;
 | 
			
		||||
							
								
								
									
										20
									
								
								src/api_data/res_create_comment.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/api_data/res_create_comment.rs
									
									
									
									
									
										Normal file
									
								
							@@ -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,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
							
								
								
									
										51
									
								
								src/controllers/comments_controller.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								src/controllers/comments_controller.rs
									
									
									
									
									
										Normal file
									
								
							@@ -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))
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
@@ -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> {
 | 
			
		||||
        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)),
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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<u64> {
 | 
			
		||||
    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<Vec<Comment>> {
 | 
			
		||||
    database::QueryInfo::new(COMMENTS_TABLE)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user