1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-29 16:56:28 +00:00

Can create surveys

This commit is contained in:
Pierre HUBERT 2020-07-08 20:16:09 +02:00
parent 3be1c7f203
commit 1af26f6d84
5 changed files with 77 additions and 8 deletions

View File

@ -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))
}

View File

@ -23,3 +23,4 @@ pub mod post;
pub mod movie;
pub mod survey;
pub mod comment;
pub mod new_survey;

13
src/data/new_survey.rs Normal file
View File

@ -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<String>,
pub allow_new_choices: bool,
}

View File

@ -109,10 +109,8 @@ pub fn create(p: &Post) -> ResultBoxError<u64> {
insert_query = insert_query.add_u64("time_end", *count_down);
}
_ => unimplemented!()
/*
POST_KIND_SURVEY => {},
*/
// Survey
POST_KIND_SURVEY => { /* Nothing to be done */ }
}
// Execute insertion

View File

@ -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<Survey> {