From 2dde756d5d5b715f05ec759e95b57dcbf9678c66 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 2 Jul 2020 19:05:05 +0200 Subject: [PATCH] Complete information --- src/api_data/post_api.rs | 15 ++++++++++++- src/data/post.rs | 45 ++++++++++++++++++++++++++++++++++++- src/helpers/posts_helper.rs | 33 +++++++++++++++++++++++++-- 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/api_data/post_api.rs b/src/api_data/post_api.rs index ce25381..11b164e 100644 --- a/src/api_data/post_api.rs +++ b/src/api_data/post_api.rs @@ -4,18 +4,31 @@ use serde::Serialize; use crate::data::post::Post; +use crate::data::user::UserID; #[derive(Serialize)] #[allow(non_snake_case)] pub struct PostAPI { ID: u64, + userID: u64, + user_page_id: u64, + group_id: u64, + post_time: u64, + content: Option, + visibility_level: String, } impl PostAPI { /// Turn a `Post` entry into an API entry pub fn new(p: &Post) -> PostAPI { PostAPI { - ID: p.id + ID: p.id, + userID: p.user_id.id(), + user_page_id: p.user_page_id().unwrap_or(&UserID::invalid()).id(), + group_id: p.group_id().map(|f| f.id()).unwrap_or(0), + post_time: p.time_create, + content: p.content.clone(), + visibility_level: p.visibility.to_api(), } } diff --git a/src/data/post.rs b/src/data/post.rs index 7609a89..5de1e8e 100644 --- a/src/data/post.rs +++ b/src/data/post.rs @@ -2,6 +2,9 @@ //! //! @author Pierre Hubert +use crate::data::group_id::GroupID; +use crate::data::user::UserID; + #[allow(non_camel_case_types)] pub enum PostVisibilityLevel { //Posts that can be seen by anyone @@ -17,6 +20,46 @@ pub enum PostVisibilityLevel { VISIBILITY_GROUP_MEMBERS = 50, } +impl PostVisibilityLevel { + pub fn to_api(&self) -> String { + match self { + PostVisibilityLevel::VISIBILITY_PUBLIC => "public", + PostVisibilityLevel::VISIBILITY_FRIENDS => "friends", + PostVisibilityLevel::VISIBILITY_USER => "private", + PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS => "members", + }.to_string() + } +} + +#[allow(non_camel_case_types)] +pub enum PostPageKind { + PAGE_KIND_USER(UserID), + PAGE_KIND_GROUP(GroupID), +} + pub struct Post { - pub id: u64 + pub id: u64, + pub user_id: UserID, + pub time_create: u64, + pub target_page: PostPageKind, + pub content: Option, + pub visibility: PostVisibilityLevel, +} + +impl Post { + /// Get the ID of the target user page, if any + pub fn user_page_id(&self) -> Option<&UserID> { + match &self.target_page { + PostPageKind::PAGE_KIND_USER(id) => Some(id), + _ => None, + } + } + + /// Get the ID of the target group page, if any + pub fn group_id(&self) -> Option<&GroupID> { + match &self.target_page { + PostPageKind::PAGE_KIND_GROUP(id) => Some(id), + _ => None, + } + } } \ No newline at end of file diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 1108a9b..7258771 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -4,9 +4,10 @@ use crate::constants::database_tables_names::POSTS_TABLE; use crate::data::error::ResultBoxError; -use crate::data::post::{Post, PostVisibilityLevel}; +use crate::data::post::{Post, PostPageKind, PostVisibilityLevel}; use crate::data::user::UserID; use crate::helpers::{database, friends_helper}; +use crate::utils::date_utils::time; impl PostVisibilityLevel { pub fn to_db(&self) -> u32 { @@ -17,6 +18,16 @@ impl PostVisibilityLevel { PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS => 50, } } + + pub fn from_db(val: u32) -> PostVisibilityLevel { + match val { + 1 => PostVisibilityLevel::VISIBILITY_PUBLIC, + 2 => PostVisibilityLevel::VISIBILITY_FRIENDS, + 3 => PostVisibilityLevel::VISIBILITY_USER, + 50 => PostVisibilityLevel::VISIBILITY_GROUP_MEMBERS, + _ => PostVisibilityLevel::VISIBILITY_PUBLIC, + } + } } @@ -112,7 +123,25 @@ fn get_user(query: &PostsQuery, target_id: &UserID) -> ResultBoxError> /// Turn a post into a database entry fn db_to_post(res: &database::RowResult) -> ResultBoxError { + let user_id = if res.get_u64("ID_amis")? == 0 { + res.get_user_id("ID_personne") + } else { + res.get_user_id("ID_amis") + }?; + + let target_page = if res.get_u64("group_id")? == 0 { + PostPageKind::PAGE_KIND_USER(res.get_user_id("ID_personne")?) + } else { + PostPageKind::PAGE_KIND_GROUP(res.get_group_id("group_id")?) + }; + Ok(Post { - id: res.get_u64("ID")? + // General information + id: res.get_u64("ID")?, + user_id: user_id.clone(), + time_create: res.get_u64("time_insert").unwrap_or(time()), + target_page, + content: res.get_optional_str("texte")?, + visibility: PostVisibilityLevel::from_db(res.get_u32("niveau_visibilite")?), }) } \ No newline at end of file