1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Can create new choices for a survey

This commit is contained in:
Pierre HUBERT 2020-07-10 11:59:18 +02:00
parent b76e16294a
commit 715cfb0716
4 changed files with 41 additions and 0 deletions

View File

@ -77,3 +77,6 @@ pub const PATH_COMMENTS_IMAGES: &str = "imgcommentaire";
/// Maximum requests size (50 Mo) /// Maximum requests size (50 Mo)
pub const MAX_REQUEST_SIZE: usize = 50000000; pub const MAX_REQUEST_SIZE: usize = 50000000;
/// Maximum number of choices per survey
pub const MAXIMUM_NUMBER_SURVEY_CHOICES: usize = 20;

View File

@ -239,6 +239,9 @@ pub fn get_routes() -> Vec<Route> {
Route::post("/surveys/cancel_response", Box::new(surveys_controller::cancel_response)), 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 // Movies controller
Route::post("/movies/get_list", Box::new(movies_controller::get_list)), Route::post("/movies/get_list", Box::new(movies_controller::get_list)),

View File

@ -3,6 +3,7 @@
//! @author Pierre Hubert //! @author Pierre Hubert
use crate::api_data::survey_api::SurveyAPI; use crate::api_data::survey_api::SurveyAPI;
use crate::constants::MAXIMUM_NUMBER_SURVEY_CHOICES;
use crate::controllers::routes::RequestResult; use crate::controllers::routes::RequestResult;
use crate::data::error::ResultBoxError; use crate::data::error::ResultBoxError;
use crate::data::http_request_handler::HttpRequestHandler; 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)?; survey_helper::cancel_response(r.user_id_ref()?, survey_id)?;
r.success("Response cancelled") 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")
} }

View File

@ -127,6 +127,14 @@ pub fn send_response(user_id: &UserID, survey_id: u64, choice_id: u64) -> Result
.insert_drop_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 /// Turn a database entry into a row object
fn db_to_survey(row: &database::RowResult) -> ResultBoxError<Survey> { fn db_to_survey(row: &database::RowResult) -> ResultBoxError<Survey> {
let survey_id = row.get_u64("ID")?; let survey_id = row.get_u64("ID")?;