diff --git a/src/constants.rs b/src/constants.rs index 67985bc..d71508c 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -77,3 +77,6 @@ pub const PATH_COMMENTS_IMAGES: &str = "imgcommentaire"; /// Maximum requests size (50 Mo) pub const MAX_REQUEST_SIZE: usize = 50000000; + +/// Maximum number of choices per survey +pub const MAXIMUM_NUMBER_SURVEY_CHOICES: usize = 20; \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index e43d548..e90107e 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -239,6 +239,9 @@ pub fn get_routes() -> Vec { Route::post("/surveys/cancel_response", Box::new(surveys_controller::cancel_response)), + Route::post("/surveys/create_new_choice", Box::new(surveys_controller::create_new_choice)), + + // Movies controller Route::post("/movies/get_list", Box::new(movies_controller::get_list)), diff --git a/src/controllers/surveys_controller.rs b/src/controllers/surveys_controller.rs index c82c641..879d2f2 100644 --- a/src/controllers/surveys_controller.rs +++ b/src/controllers/surveys_controller.rs @@ -3,6 +3,7 @@ //! @author Pierre Hubert use crate::api_data::survey_api::SurveyAPI; +use crate::constants::MAXIMUM_NUMBER_SURVEY_CHOICES; use crate::controllers::routes::RequestResult; use crate::data::error::ResultBoxError; use crate::data::http_request_handler::HttpRequestHandler; @@ -51,4 +52,30 @@ pub fn cancel_response(r: &mut HttpRequestHandler) -> RequestResult { survey_helper::cancel_response(r.user_id_ref()?, survey_id)?; r.success("Response cancelled") +} + +/// Create a new choice for a survey +pub fn create_new_choice(r: &mut HttpRequestHandler) -> RequestResult { + let post = r.post_post_with_access("postID", PostAccessLevel::BASIC_ACCESS)?; + let new_choice = r.post_string("choice")?; + + let survey = survey_helper::get_info(post.id)?; + if !survey.allow_new_choices { + r.forbidden("It is not possible to create new choices for this survey!".to_string())?; + } + + // Check for similar choices + if survey.choices.iter().find( + |c| c.name.to_lowercase().eq(&new_choice.to_lowercase())).is_some() { + r.forbidden("This choice already exists!".to_string())?; + } + + survey_helper::create_choice(survey.id, &new_choice)?; + + // Auto-block creation of new choices if limit is reached + if survey.choices.len() + 1 >= MAXIMUM_NUMBER_SURVEY_CHOICES { + survey_helper::block_new_choices_creation(survey.id)?; + } + + r.success("Choice created") } \ No newline at end of file diff --git a/src/helpers/survey_helper.rs b/src/helpers/survey_helper.rs index 5ae6450..c6bce87 100644 --- a/src/helpers/survey_helper.rs +++ b/src/helpers/survey_helper.rs @@ -127,6 +127,14 @@ pub fn send_response(user_id: &UserID, survey_id: u64, choice_id: u64) -> Result .insert_drop_result() } +/// Block new survey choices from being created +pub fn block_new_choices_creation(survey_id: u64) -> ResultBoxError { + database::UpdateInfo::new(SURVEY_INFO_TABLE) + .cond_u64("ID", survey_id) + .set_legacy_bool("allow_new_choices", false) + .exec() +} + /// Turn a database entry into a row object fn db_to_survey(row: &database::RowResult) -> ResultBoxError { let survey_id = row.get_u64("ID")?;