1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-07-15 03:38:07 +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

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