1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-27 22:18:51 +00:00

Add likes to export

This commit is contained in:
Pierre HUBERT 2020-07-13 19:26:19 +02:00
parent 67dd29fe2d
commit 70d5facf4c
8 changed files with 76 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -27,4 +27,5 @@ pub mod new_survey;
pub mod notification;
pub mod user_membership;
pub mod new_account;
pub mod account_export;
pub mod account_export;
pub mod user_like;

13
src/data/user_like.rs Normal file
View File

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

View File

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

View File

@ -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<Vec<UserLike>> {
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<UserLike> {
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")?,
})
}