From 70d5facf4c0b40fa02074cd4940dd37f9daf4729 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 13 Jul 2020 19:26:19 +0200 Subject: [PATCH] Add likes to export --- src/api_data/account_export_api.rs | 3 +++ src/api_data/mod.rs | 3 ++- src/api_data/user_like_api.rs | 32 ++++++++++++++++++++++++++++++ src/data/account_export.rs | 2 ++ src/data/mod.rs | 3 ++- src/data/user_like.rs | 13 ++++++++++++ src/helpers/account_helper.rs | 5 +++-- src/helpers/likes_helper.rs | 19 ++++++++++++++++++ 8 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/api_data/user_like_api.rs create mode 100644 src/data/user_like.rs diff --git a/src/api_data/account_export_api.rs b/src/api_data/account_export_api.rs index 0b9ed42..09950f5 100644 --- a/src/api_data/account_export_api.rs +++ b/src/api_data/account_export_api.rs @@ -6,6 +6,7 @@ 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::api_data::user_like_api::UserLikeAPI; use crate::data::account_export::AccountExport; use crate::data::error::ResultBoxError; @@ -16,6 +17,7 @@ pub struct AccountExportAPI { advanced_info: APIUserInfo, posts: Vec, comments: Vec, + likes: Vec, } impl AccountExportAPI { @@ -26,6 +28,7 @@ impl AccountExportAPI { 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())?, + likes: UserLikeAPI::for_list(&export.likes), }; Ok(export) diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index e654697..a2b94df 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -47,4 +47,5 @@ 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; -pub mod account_export_api; \ No newline at end of file +pub mod account_export_api; +pub mod user_like_api; \ No newline at end of file diff --git a/src/api_data/user_like_api.rs b/src/api_data/user_like_api.rs new file mode 100644 index 0000000..4f06648 --- /dev/null +++ b/src/api_data/user_like_api.rs @@ -0,0 +1,32 @@ +//! # User like API entry +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::data::user_like::UserLike; + +#[derive(Serialize)] +pub struct UserLikeAPI { + id: u64, + user_id: u64, + time_sent: u64, + elem_type: String, + elem_id: u64, +} + +impl UserLikeAPI { + pub fn new(l: &UserLike) -> UserLikeAPI { + UserLikeAPI { + id: l.id, + user_id: l.user_id.id(), + time_sent: l.time_sent, + elem_type: l.elem_type.clone(), + elem_id: l.elem_id, + } + } + + 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 86c93d5..27d9ee3 100644 --- a/src/data/account_export.rs +++ b/src/data/account_export.rs @@ -5,9 +5,11 @@ use crate::data::comment::Comment; use crate::data::post::Post; use crate::data::user::User; +use crate::data::user_like::UserLike; pub struct AccountExport { pub user: User, pub posts: Vec, pub comments: Vec, + pub likes: Vec, } \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 8c452d9..0bcf77b 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -27,4 +27,5 @@ pub mod new_survey; pub mod notification; pub mod user_membership; pub mod new_account; -pub mod account_export; \ No newline at end of file +pub mod account_export; +pub mod user_like; \ No newline at end of file diff --git a/src/data/user_like.rs b/src/data/user_like.rs new file mode 100644 index 0000000..13439ea --- /dev/null +++ b/src/data/user_like.rs @@ -0,0 +1,13 @@ +//! User like +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; + +pub struct UserLike { + pub id: u64, + pub user_id: UserID, + pub time_sent: u64, + pub elem_type: String, + pub elem_id: u64, +} \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 507fca8..b9e7265 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -5,7 +5,7 @@ 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}; +use crate::helpers::{database, user_helper, posts_helper, comments_helper, likes_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}; @@ -196,7 +196,8 @@ 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)? + comments: comments_helper::export_all_user(user_id)?, + likes: likes_helper::export_all_user(user_id)? }; Ok(data) diff --git a/src/helpers/likes_helper.rs b/src/helpers/likes_helper.rs index 85d98a5..7ebc855 100644 --- a/src/helpers/likes_helper.rs +++ b/src/helpers/likes_helper.rs @@ -5,6 +5,7 @@ use crate::constants::database_tables_names::LIKES_TABLE; use crate::data::error::ResultBoxError; use crate::data::user::UserID; +use crate::data::user_like::UserLike; use crate::helpers::database; use crate::helpers::database::QueryInfo; use crate::utils::date_utils::mysql_date; @@ -79,4 +80,22 @@ pub fn delete_all(id: u64, kind: LikeType) -> ResultBoxError { .cond_u64("ID_type", id) .cond_str("type", &kind.to_db_type()) .exec() +} + +/// Export all the likes mention of the user +pub fn export_all_user(user_id: &UserID) -> ResultBoxError> { + database::QueryInfo::new(LIKES_TABLE) + .cond_user_id("ID_personne", user_id) + .exec(db_to_user_like) +} + +/// Turn a database entry into a like entry +fn db_to_user_like(r: &database::RowResult) -> ResultBoxError { + Ok(UserLike { + id: r.get_u64("ID")?, + user_id: r.get_user_id("ID_personne")?, + time_sent: r.get_date_as_time("Date_envoi")?, + elem_type: r.get_str("type")?, + elem_id: r.get_u64("ID_type")?, + }) } \ No newline at end of file