diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index bc34eaa..802a282 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -9,8 +9,9 @@ use crate::controllers::routes::RequestResult; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::group::GroupAccessLevel; use crate::data::http_request_handler::HttpRequestHandler; +use crate::data::new_survey::NewSurvey; use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink}; -use crate::helpers::{groups_helper, posts_helper, user_helper}; +use crate::helpers::{groups_helper, posts_helper, survey_helper, user_helper}; use crate::utils::date_utils::time; use crate::utils::string_utils::{check_string_before_insert, check_youtube_id}; use crate::utils::user_data_utils::user_data_path; @@ -117,6 +118,8 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { kind: PostKind::POST_KIND_TEXT, }; + let mut new_survey = None; + // Handle different post types post.kind = match r.post_string("kind")?.as_str() { @@ -194,7 +197,26 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { PostKind::POST_KIND_COUNTDOWN(time_end) } - // TODO : add support for next types + "survey" => { + let survey = NewSurvey { + post_id: 0, + user_id: r.user_id()?, + question: r.post_string("question")?, + choices: r.post_string("answers")? + .split("<>") + .filter(|a| a.len() > 0) + .map(|a| a.to_string()) + .collect(), + allow_new_choices: r.post_bool_opt("allowNewAnswers", false), + }; + + if survey.choices.len() < 2 { + r.bad_request("A survey must have at least two choices!".to_string())?; + } + + new_survey = Some(survey); + PostKind::POST_KIND_SURVEY + } _ => { r.internal_error(ExecError::boxed_new("Unsupported kind of post!"))?; @@ -205,6 +227,12 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { // Create the post let post_id = posts_helper::create(&post)?; + // Create associated survey, if required + if let Some(mut survey) = new_survey { + survey.post_id = post_id; + survey_helper::create(&survey)?; + } + // TODO : create a notification r.set_response(ResCreatePost::new(post_id)) } \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index d5c9d83..8b40982 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -22,4 +22,5 @@ pub mod friendship_status; pub mod post; pub mod movie; pub mod survey; -pub mod comment; \ No newline at end of file +pub mod comment; +pub mod new_survey; \ No newline at end of file diff --git a/src/data/new_survey.rs b/src/data/new_survey.rs new file mode 100644 index 0000000..edb0e37 --- /dev/null +++ b/src/data/new_survey.rs @@ -0,0 +1,13 @@ +//! # New survey information +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; + +pub struct NewSurvey { + pub post_id: u64, + pub user_id: UserID, + pub question: String, + pub choices: Vec, + pub allow_new_choices: bool, +} \ No newline at end of file diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 2011fb4..9bd0ad6 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -109,10 +109,8 @@ pub fn create(p: &Post) -> ResultBoxError { insert_query = insert_query.add_u64("time_end", *count_down); } - _ => unimplemented!() - /* - POST_KIND_SURVEY => {}, - */ + // Survey + POST_KIND_SURVEY => { /* Nothing to be done */ } } // Execute insertion diff --git a/src/helpers/survey_helper.rs b/src/helpers/survey_helper.rs index b5b34a6..2f85b29 100644 --- a/src/helpers/survey_helper.rs +++ b/src/helpers/survey_helper.rs @@ -3,10 +3,39 @@ //! @author Pierre Hubert use crate::constants::database_tables_names::{SURVEY_CHOICES_TABLE, SURVEY_INFO_TABLE, SURVEY_RESPONSE_TABLE}; -use crate::data::error::ResultBoxError; +use crate::data::error::{ExecError, ResultBoxError}; +use crate::data::new_survey::NewSurvey; use crate::data::survey::{Survey, SurveyChoice}; use crate::data::user::UserID; use crate::helpers::database; +use crate::utils::date_utils::{mysql_date, time}; + +/// Create a new survey +pub fn create(survey: &NewSurvey) -> ResultBoxError { + let survey_id = database::InsertQuery::new(SURVEY_INFO_TABLE) + .add_user_id("ID_utilisateurs", &survey.user_id) + .add_u64("ID_texte", survey.post_id) + .add_u64("date_creation", time()) + .add_str("question", survey.question.as_str()) + .add_legacy_bool("allow_new_choices", survey.allow_new_choices) + .insert()? + .ok_or(ExecError::new("Survey was created but no ID was returned!"))?; + + for choice in &survey.choices { + create_choice(survey_id, choice)?; + } + + Ok(()) +} + +/// Insert a new choice for a survey +pub fn create_choice(survey_id: u64, choice: &str) -> ResultBoxError { + database::InsertQuery::new(SURVEY_CHOICES_TABLE) + .add_u64("ID_sondage", survey_id) + .add_str("date_creation", &mysql_date()) + .add_str("Choix", choice) + .insert_drop_result() +} /// Get information about a survey pub fn get_info(post_id: u64) -> ResultBoxError {