diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-12 14:11:50 -0500 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-12 14:11:50 -0500 |
commit | 3d71da490947aacc52a3b77efdc13d5f0458c57f (patch) | |
tree | 8047eb6966655cffc772cbde4d73982fb7064a28 /src/types/post.rs | |
parent | docs is ssr'd (diff) | |
download | xssbook-3d71da490947aacc52a3b77efdc13d5f0458c57f.tar.gz xssbook-3d71da490947aacc52a3b77efdc13d5f0458c57f.tar.bz2 xssbook-3d71da490947aacc52a3b77efdc13d5f0458c57f.zip |
refactor
Diffstat (limited to 'src/types/post.rs')
-rw-r--r-- | src/types/post.rs | 38 |
1 files changed, 5 insertions, 33 deletions
diff --git a/src/types/post.rs b/src/types/post.rs index ea7cbbf..a067512 100644 --- a/src/types/post.rs +++ b/src/types/post.rs @@ -1,19 +1,21 @@ use core::fmt; use serde::Serialize; -use std::collections::HashSet; use tracing::instrument; use crate::database; use crate::types::http::{ResponseCode, Result}; +use super::comment::Comment; + #[derive(Serialize)] pub struct Post { pub post_id: u64, pub user_id: u64, pub content: String, - pub likes: HashSet<u64>, - pub comments: Vec<(u64, String)>, pub date: u64, + pub likes: u64, + pub liked: bool, + pub comments: Vec<Comment>, } impl fmt::Debug for Post { @@ -67,34 +69,4 @@ impl Post { Ok(post) } - - #[instrument()] - pub fn comment(&mut self, user_id: u64, content: String) -> Result<()> { - self.comments.push((user_id, content)); - - if database::posts::update_post(self.post_id, &self.likes, &self.comments).is_err() { - tracing::error!("Failed to comment on post"); - return Err(ResponseCode::InternalServerError.text("Failed to comment on post")); - } - - Ok(()) - } - - #[instrument()] - pub fn like(&mut self, user_id: u64, state: bool) -> Result<()> { - if state { - self.likes.insert(user_id); - } else { - self.likes.remove(&user_id); - } - - if database::posts::update_post(self.post_id, &self.likes, &self.comments).is_err() { - tracing::error!("Failed to change like state on post"); - return Err( - ResponseCode::InternalServerError.text("Failed to change like state on post") - ); - } - - Ok(()) - } } |