diff options
Diffstat (limited to 'src/types/post.rs')
-rw-r--r-- | src/types/post.rs | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/types/post.rs b/src/types/post.rs index 95aed0e..90eada2 100644 --- a/src/types/post.rs +++ b/src/types/post.rs @@ -1,10 +1,10 @@ use core::fmt; -use std::collections::HashSet; use serde::Serialize; +use std::collections::HashSet; use tracing::instrument; use crate::database; -use crate::types::http::{Result, ResponseCode}; +use crate::types::http::{ResponseCode, Result}; #[derive(Serialize)] pub struct Post { @@ -13,19 +13,18 @@ pub struct Post { pub content: String, pub likes: HashSet<u64>, pub comments: Vec<(u64, String)>, - pub date: u64 + pub date: u64, } impl fmt::Debug for Post { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Post") - .field("post_id", &self.post_id) - .finish() + .field("post_id", &self.post_id) + .finish() } } impl Post { - #[instrument()] pub fn from_post_id(post_id: u64) -> Result<Self> { let Ok(Some(post)) = database::posts::get_post(post_id) else { @@ -64,10 +63,10 @@ impl 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")) + return Err(ResponseCode::InternalServerError.text("Failed to comment on post")); } Ok(()) @@ -75,19 +74,19 @@ impl Post { #[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")) + return Err( + ResponseCode::InternalServerError.text("Failed to change like state on post") + ); } Ok(()) } - -}
\ No newline at end of file +} |