1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-20 16:35:17 +00:00

Can cancel the response to a survey

This commit is contained in:
2020-07-10 11:14:59 +02:00
parent 151b69451f
commit 964a7d1c8c
3 changed files with 52 additions and 5 deletions

View File

@ -234,6 +234,8 @@ pub fn get_routes() -> Vec<Route> {
// Surveys controller
Route::post("/surveys/get_info", Box::new(surveys_controller::get_info_single)),
Route::post("/surveys/send_response", Box::new(surveys_controller::send_response)),
// Movies controller
Route::post("/movies/get_list", Box::new(movies_controller::get_list)),

View File

@ -9,6 +9,14 @@ use crate::data::http_request_handler::HttpRequestHandler;
use crate::data::post::PostAccessLevel;
use crate::helpers::survey_helper;
impl HttpRequestHandler {
/// Get the ID of a survey associated to a post whose ID was specified in the request
fn post_survey_id_from_post_id(&mut self, name: &str, min_level: PostAccessLevel) -> ResultBoxError<u64> {
let post = self.post_post_with_access(name, min_level)?;
survey_helper::get_id(post.id)
}
}
/// Get information about a single survey
pub fn get_info_single(r: &mut HttpRequestHandler) -> RequestResult {
let post = r.post_post_with_access("postID", PostAccessLevel::BASIC_ACCESS)?;
@ -20,8 +28,18 @@ pub fn get_info_single(r: &mut HttpRequestHandler) -> RequestResult {
r.set_response(SurveyAPI::new(&survey, r.user_id_opt())?)
}
/// Get the ID of a survey associated to a post whose ID was specified in the request
fn post_survey_id_from_post_id(r: &mut HttpRequestHandler, name: &str, min_level: PostAccessLevel) -> ResultBoxError<u64> {
let post = r.post_post_with_access(name, min_level)?;
survey_helper::get_id(post.id)
}
/// Respond to a survey
pub fn send_response(r: &mut HttpRequestHandler) -> RequestResult {
let survey_id = r.post_survey_id_from_post_id("postID", PostAccessLevel::BASIC_ACCESS)?;
let choice_id = r.post_u64("choiceID")?;
survey_helper::cancel_response(r.user_id_ref()?, survey_id)?;
if !survey_helper::choice_exists(survey_id, choice_id)? {
r.not_found("Choice not found for this survey!".to_string())?;
}
survey_helper::send_response(r.user_id_ref()?, survey_id, choice_id)?;
r.success("Choice saved!")
}