From 964a7d1c8c2c8d7e139ba65300d26531c1259a76 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Jul 2020 11:14:59 +0200 Subject: [PATCH] Can cancel the response to a survey --- src/controllers/routes.rs | 2 ++ src/controllers/surveys_controller.rs | 28 ++++++++++++++++++++++----- src/helpers/survey_helper.rs | 27 ++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 2aa377d..fa10f73 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -234,6 +234,8 @@ pub fn get_routes() -> Vec { // 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)), diff --git a/src/controllers/surveys_controller.rs b/src/controllers/surveys_controller.rs index 8cd54cc..af6a335 100644 --- a/src/controllers/surveys_controller.rs +++ b/src/controllers/surveys_controller.rs @@ -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 { + 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 { - let post = r.post_post_with_access(name, min_level)?; - survey_helper::get_id(post.id) -} \ No newline at end of file +/// 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!") +} diff --git a/src/helpers/survey_helper.rs b/src/helpers/survey_helper.rs index ca17ca1..5ae6450 100644 --- a/src/helpers/survey_helper.rs +++ b/src/helpers/survey_helper.rs @@ -100,6 +100,33 @@ pub fn exists(post_id: u64) -> ResultBoxError { .map(|r| r > 0) } +/// Cancel the response of a user to a survey +pub fn cancel_response(user_id: &UserID, survey_id: u64) -> ResultBoxError { + database::DeleteQuery::new(SURVEY_RESPONSE_TABLE) + .cond_u64("ID_sondage", survey_id) + .cond_user_id("ID_utilisateurs", user_id) + .exec() +} + +/// Check out whether a choice exists or not +pub fn choice_exists(survey_id: u64, choice_id: u64) -> ResultBoxError { + database::QueryInfo::new(SURVEY_CHOICES_TABLE) + .cond_u64("ID_sondage", survey_id) + .cond_u64("ID", choice_id) + .exec_count() + .map(|f| f > 0) +} + +/// Save the new response of a user to a survey +pub fn send_response(user_id: &UserID, survey_id: u64, choice_id: u64) -> ResultBoxError { + database::InsertQuery::new(SURVEY_RESPONSE_TABLE) + .add_user_id("ID_utilisateurs", user_id) + .add_u64("ID_sondage", survey_id) + .add_u64("ID_sondage_choix", choice_id) + .add_str("date_envoi", &mysql_date()) + .insert_drop_result() +} + /// Turn a database entry into a row object fn db_to_survey(row: &database::RowResult) -> ResultBoxError { let survey_id = row.get_u64("ID")?;