diff --git a/src/api_data/account_export_api.rs b/src/api_data/account_export_api.rs new file mode 100644 index 0000000..0b9ed42 --- /dev/null +++ b/src/api_data/account_export_api.rs @@ -0,0 +1,33 @@ +//! # Export account API entry +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::api_data::comment_api::CommentAPI; +use crate::api_data::post_api::PostAPI; +use crate::api_data::user_info::APIUserInfo; +use crate::data::account_export::AccountExport; +use crate::data::error::ResultBoxError; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct AccountExportAPI { + userID: u64, + advanced_info: APIUserInfo, + posts: Vec, + comments: Vec, +} + +impl AccountExportAPI { + pub fn new(export: &AccountExport) -> ResultBoxError { + let curr_user_id = &export.user.id; + let export = AccountExportAPI { + userID: curr_user_id.id(), + advanced_info: APIUserInfo::new_advanced_info(&curr_user_id.as_option(), &export.user)?, + posts: PostAPI::for_list(&export.posts, curr_user_id.as_option())?, + comments: CommentAPI::for_list(&export.comments, &curr_user_id.as_option())?, + }; + + Ok(export) + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 209644d..e654697 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -46,4 +46,5 @@ mod type_container_api; pub mod res_check_email_exists; pub mod res_check_security_questions_exists; pub mod res_get_security_questions; -pub mod res_check_security_answers; \ No newline at end of file +pub mod res_check_security_answers; +pub mod account_export_api; \ No newline at end of file diff --git a/src/controllers/account_controller.rs b/src/controllers/account_controller.rs index 2507bb3..0ee462e 100644 --- a/src/controllers/account_controller.rs +++ b/src/controllers/account_controller.rs @@ -1,5 +1,6 @@ use percent_encoding::percent_decode_str; +use crate::api_data::account_export_api::AccountExportAPI; use crate::api_data::current_user_id::CurrentUserID; use crate::api_data::login_success::LoginSuccess; use crate::api_data::res_check_email_exists::ResCheckEmailExists; @@ -184,5 +185,7 @@ pub fn reset_user_password(r: &mut HttpRequestHandler) -> RequestResult { pub fn export_data(r: &mut HttpRequestHandler) -> RequestResult { r.need_user_password("password")?; - r.success("Go on") + let data = account_helper::export(r.user_id_ref()?)?; + + r.set_response(AccountExportAPI::new(&data)?) } \ No newline at end of file diff --git a/src/data/account_export.rs b/src/data/account_export.rs new file mode 100644 index 0000000..86c93d5 --- /dev/null +++ b/src/data/account_export.rs @@ -0,0 +1,13 @@ +//! # Export of all account's data +//! +//! @author Pierre Hubert + +use crate::data::comment::Comment; +use crate::data::post::Post; +use crate::data::user::User; + +pub struct AccountExport { + pub user: User, + pub posts: Vec, + pub comments: Vec, +} \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 2921e3e..8c452d9 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -26,4 +26,5 @@ pub mod comment; pub mod new_survey; pub mod notification; pub mod user_membership; -pub mod new_account; \ No newline at end of file +pub mod new_account; +pub mod account_export; \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 8bf1cd2..507fca8 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -5,10 +5,11 @@ 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}; +use crate::helpers::{database, user_helper, posts_helper, comments_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 /// @@ -188,4 +189,15 @@ pub fn update_last_activity(user_id: &UserID) -> ResultBoxError { .cond_user_id("ID", user_id) .set_u64("last_activity", time()) .exec() +} + +/// Export an account's data +pub fn export(user_id: &UserID) -> ResultBoxError { + let data = 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)? + }; + + Ok(data) } \ No newline at end of file diff --git a/src/helpers/comments_helper.rs b/src/helpers/comments_helper.rs index 1b53502..ed7a733 100644 --- a/src/helpers/comments_helper.rs +++ b/src/helpers/comments_helper.rs @@ -5,6 +5,7 @@ use crate::constants::database_tables_names::COMMENTS_TABLE; use crate::data::comment::Comment; use crate::data::error::{ExecError, ResultBoxError}; +use crate::data::user::UserID; use crate::helpers::{database, likes_helper}; use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::mysql_date; @@ -40,6 +41,13 @@ pub fn get_single(comment_id: u64) -> ResultBoxError { .query_row(db_to_comment) } +/// Export all the comments of a given user +pub fn export_all_user(user_id: &UserID) -> ResultBoxError> { + database::QueryInfo::new(COMMENTS_TABLE) + .cond_user_id("ID_personne", user_id) + .exec(db_to_comment) +} + /// Turn a database entry into a comment object fn db_to_comment(row: &database::RowResult) -> ResultBoxError { diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 0f20114..a04dc03 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -325,6 +325,15 @@ pub fn get_single(post_id: u64) -> ResultBoxError { .query_row(db_to_post) } +/// Get the entire list of posts of a given user + the posts created on user's page +pub fn export_all_posts_user(user_id: &UserID) -> ResultBoxError> { + database::QueryInfo::new(POSTS_TABLE) + .set_custom_where("ID_personne = ? OR ID_amis = ?") + .add_custom_where_argument_user_id(user_id) + .add_custom_where_argument_user_id(user_id) + .exec(db_to_post) +} + /// Get the access level of a user over a post pub fn get_access_level(p: &Post, user_id: &Option) -> ResultBoxError { if user_id == &p.user_id.as_option() {