1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Add responses to surveys to export

This commit is contained in:
Pierre HUBERT 2020-07-13 19:38:51 +02:00
parent 70d5facf4c
commit 88c45f05ac
9 changed files with 80 additions and 6 deletions

View File

@ -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<PostAPI>,
comments: Vec<CommentAPI>,
likes: Vec<UserLikeAPI>,
survey_responses: Vec<SurveyResponseAPI>,
}
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)

View File

@ -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;
pub mod user_like_api;
pub mod survey_response_api;

View File

@ -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<SurveyResponse>) -> Vec<Self> {
l.iter().map(Self::new).collect()
}
}

View File

@ -26,7 +26,7 @@ impl UserLikeAPI {
}
}
pub fn for_list(l: &Vec<UserLike>) -> Vec<UserLikeAPI> {
pub fn for_list(l: &Vec<UserLike>) -> Vec<Self> {
l.iter().map(Self::new).collect()
}
}

View File

@ -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<Post>,
pub comments: Vec<Comment>,
pub likes: Vec<UserLike>,
pub survey_responses: Vec<SurveyResponse>,
}

View File

@ -28,4 +28,5 @@ pub mod notification;
pub mod user_membership;
pub mod new_account;
pub mod account_export;
pub mod user_like;
pub mod user_like;
pub mod survey_response;

View File

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

View File

@ -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<AccountExport> {
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)

View File

@ -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<Vec<SurveyResponse>> {
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<Survey> {
let survey_id = row.get_u64("ID")?;
@ -157,4 +165,15 @@ fn db_to_survey_choice(row: &database::RowResult) -> ResultBoxError<SurveyChoice
name: row.get_str("Choix")?,
count: row.get_u64("count_choice")?,
})
}
/// Turn a database row into a SurveyResponse object
fn db_to_survey_response(row: &database::RowResult) -> ResultBoxError<SurveyResponse> {
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")?,
})
}