1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 00:45:18 +00:00

Can create surveys

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

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