From 88c45f05ac6cc174c629f4ca69e2b1382d3ae046 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 13 Jul 2020 19:38:51 +0200 Subject: [PATCH] Add responses to surveys to export --- src/api_data/account_export_api.rs | 3 +++ src/api_data/mod.rs | 3 ++- src/api_data/survey_response_api.rs | 32 +++++++++++++++++++++++++++++ src/api_data/user_like_api.rs | 2 +- src/data/account_export.rs | 2 ++ src/data/mod.rs | 3 ++- src/data/survey_response.rs | 15 ++++++++++++++ src/helpers/account_helper.rs | 7 ++++--- src/helpers/survey_helper.rs | 19 +++++++++++++++++ 9 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 src/api_data/survey_response_api.rs create mode 100644 src/data/survey_response.rs diff --git a/src/api_data/account_export_api.rs b/src/api_data/account_export_api.rs index 09950f5..0efe903 100644 --- a/src/api_data/account_export_api.rs +++ b/src/api_data/account_export_api.rs @@ -5,6 +5,7 @@ use serde::Serialize; use crate::api_data::comment_api::CommentAPI; use crate::api_data::post_api::PostAPI; +use crate::api_data::survey_response_api::SurveyResponseAPI; use crate::api_data::user_info::APIUserInfo; use crate::api_data::user_like_api::UserLikeAPI; use crate::data::account_export::AccountExport; @@ -18,6 +19,7 @@ pub struct AccountExportAPI { posts: Vec, comments: Vec, likes: Vec, + survey_responses: Vec, } impl AccountExportAPI { @@ -29,6 +31,7 @@ impl AccountExportAPI { posts: PostAPI::for_list(&export.posts, curr_user_id.as_option())?, comments: CommentAPI::for_list(&export.comments, &curr_user_id.as_option())?, likes: UserLikeAPI::for_list(&export.likes), + survey_responses: SurveyResponseAPI::for_list(&export.survey_responses), }; Ok(export) diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index a2b94df..4d25b0c 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -48,4 +48,5 @@ pub mod res_check_security_questions_exists; pub mod res_get_security_questions; pub mod res_check_security_answers; pub mod account_export_api; -pub mod user_like_api; \ No newline at end of file +pub mod user_like_api; +pub mod survey_response_api; \ No newline at end of file diff --git a/src/api_data/survey_response_api.rs b/src/api_data/survey_response_api.rs new file mode 100644 index 0000000..930eb9c --- /dev/null +++ b/src/api_data/survey_response_api.rs @@ -0,0 +1,32 @@ +//! # Survey response API entry +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::data::survey_response::SurveyResponse; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct SurveyResponseAPI { + id: u64, + time_sent: u64, + userID: u64, + surveyID: u64, + choiceID: u64, +} + +impl SurveyResponseAPI { + pub fn new(r: &SurveyResponse) -> SurveyResponseAPI { + SurveyResponseAPI { + id: r.id, + time_sent: r.time_sent, + userID: r.user_id.id(), + surveyID: r.survey_id, + choiceID: r.choice_id, + } + } + + pub fn for_list(l: &Vec) -> Vec { + l.iter().map(Self::new).collect() + } +} \ No newline at end of file diff --git a/src/api_data/user_like_api.rs b/src/api_data/user_like_api.rs index 4f06648..e82621d 100644 --- a/src/api_data/user_like_api.rs +++ b/src/api_data/user_like_api.rs @@ -26,7 +26,7 @@ impl UserLikeAPI { } } - pub fn for_list(l: &Vec) -> Vec { + pub fn for_list(l: &Vec) -> Vec { l.iter().map(Self::new).collect() } } \ No newline at end of file diff --git a/src/data/account_export.rs b/src/data/account_export.rs index 27d9ee3..3da6e08 100644 --- a/src/data/account_export.rs +++ b/src/data/account_export.rs @@ -4,6 +4,7 @@ use crate::data::comment::Comment; use crate::data::post::Post; +use crate::data::survey_response::SurveyResponse; use crate::data::user::User; use crate::data::user_like::UserLike; @@ -12,4 +13,5 @@ pub struct AccountExport { pub posts: Vec, pub comments: Vec, pub likes: Vec, + pub survey_responses: Vec, } \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 0bcf77b..712f53e 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -28,4 +28,5 @@ pub mod notification; pub mod user_membership; pub mod new_account; pub mod account_export; -pub mod user_like; \ No newline at end of file +pub mod user_like; +pub mod survey_response; \ No newline at end of file diff --git a/src/data/survey_response.rs b/src/data/survey_response.rs new file mode 100644 index 0000000..93b04fe --- /dev/null +++ b/src/data/survey_response.rs @@ -0,0 +1,15 @@ +//! # Survey response +//! +//! This structure contains information about a single response to a survey +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; + +pub struct SurveyResponse { + pub id: u64, + pub time_sent: u64, + pub user_id: UserID, + pub survey_id: u64, + pub choice_id: u64 +} \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index b9e7265..fc042f3 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -1,15 +1,15 @@ use crate::constants::{PASSWORD_RESET_TOKEN_LENGTH, PASSWORD_RESET_TOKEN_LIFETIME}; use crate::constants::database_tables_names::{USER_ACCESS_TOKENS_TABLE, USERS_TABLE}; +use crate::data::account_export::AccountExport; use crate::data::api_client::APIClient; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::new_account::NewAccount; use crate::data::user::UserID; use crate::data::user_token::UserAccessToken; -use crate::helpers::{database, user_helper, posts_helper, comments_helper, likes_helper}; +use crate::helpers::{comments_helper, database, likes_helper, posts_helper, survey_helper, user_helper}; use crate::helpers::database::{DeleteQuery, InsertQuery, QueryInfo}; use crate::utils::crypt_utils::{crypt_pass, rand_str}; use crate::utils::date_utils::{mysql_date, time}; -use crate::data::account_export::AccountExport; /// Account helper /// @@ -197,7 +197,8 @@ pub fn export(user_id: &UserID) -> ResultBoxError { user: user_helper::find_user_by_id(user_id)?, posts: posts_helper::export_all_posts_user(user_id)?, comments: comments_helper::export_all_user(user_id)?, - likes: likes_helper::export_all_user(user_id)? + likes: likes_helper::export_all_user(user_id)?, + survey_responses: survey_helper::export_all_user_responses(user_id)?, }; Ok(data) diff --git a/src/helpers/survey_helper.rs b/src/helpers/survey_helper.rs index c6bce87..1c2b8f7 100644 --- a/src/helpers/survey_helper.rs +++ b/src/helpers/survey_helper.rs @@ -6,6 +6,7 @@ use crate::constants::database_tables_names::{SURVEY_CHOICES_TABLE, SURVEY_INFO_ use crate::data::error::{ExecError, ResultBoxError}; use crate::data::new_survey::NewSurvey; use crate::data::survey::{Survey, SurveyChoice}; +use crate::data::survey_response::SurveyResponse; use crate::data::user::UserID; use crate::helpers::database; use crate::utils::date_utils::mysql_date; @@ -135,6 +136,13 @@ pub fn block_new_choices_creation(survey_id: u64) -> ResultBoxError { .exec() } +/// Export all the responses of a given user +pub fn export_all_user_responses(user_id: &UserID) -> ResultBoxError> { + database::QueryInfo::new(SURVEY_RESPONSE_TABLE) + .cond_user_id("ID_utilisateurs", user_id) + .exec(db_to_survey_response) +} + /// Turn a database entry into a row object fn db_to_survey(row: &database::RowResult) -> ResultBoxError { let survey_id = row.get_u64("ID")?; @@ -157,4 +165,15 @@ fn db_to_survey_choice(row: &database::RowResult) -> ResultBoxError ResultBoxError { + Ok(SurveyResponse { + id: row.get_u64("ID")?, + time_sent: row.get_date_as_time("date_envoi")?, + user_id: row.get_user_id("ID_utilisateurs")?, + survey_id: row.get_u64("ID_sondage")?, + choice_id: row.get_u64("ID_sondage_choix")?, + }) } \ No newline at end of file