mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Can create new choices for a survey
This commit is contained in:
		@@ -239,6 +239,9 @@ pub fn get_routes() -> Vec<Route> {
 | 
			
		||||
 | 
			
		||||
        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)),
 | 
			
		||||
 
 | 
			
		||||
@@ -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")
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user