summaryrefslogtreecommitdiff
path: root/src/types/post.rs
diff options
context:
space:
mode:
authorTyler Murphy <tylermurphy534@gmail.com>2023-01-28 11:52:32 -0500
committerTyler Murphy <tylermurphy534@gmail.com>2023-01-28 11:52:32 -0500
commit487d97cb019ef1a37d3ef90c6b051ba0389c6d15 (patch)
tree16833c40f95c917a48ddb7f97c1d75330a8222be /src/types/post.rs
parentfix rerendering logout button, console page (diff)
downloadxssbook-487d97cb019ef1a37d3ef90c6b051ba0389c6d15.tar.gz
xssbook-487d97cb019ef1a37d3ef90c6b051ba0389c6d15.tar.bz2
xssbook-487d97cb019ef1a37d3ef90c6b051ba0389c6d15.zip
tracing
Diffstat (limited to 'src/types/post.rs')
-rw-r--r--src/types/post.rs30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/types/post.rs b/src/types/post.rs
index 7805a4e..7ca0a3c 100644
--- a/src/types/post.rs
+++ b/src/types/post.rs
@@ -1,5 +1,7 @@
+use core::fmt;
use std::collections::HashSet;
use serde::Serialize;
+use tracing::instrument;
use crate::database;
use crate::types::response::{Result, ResponseCode};
@@ -14,8 +16,17 @@ pub struct Post {
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()
+ }
+}
+
impl Post {
+ #[instrument()]
pub fn from_post_id(post_id: u64) -> Result<Self> {
let Ok(Some(post)) = database::posts::get_post(post_id) else {
return Err(ResponseCode::BadRequest.text("Post does not exist"))
@@ -24,15 +35,7 @@ impl Post {
Ok(post)
}
- // pub fn from_post_ids(post_ids: Vec<u64>) -> Vec<Self> {
- // post_ids.iter().map(|id| {
- // let Ok(post) = Post::from_post_id(*id) else {
- // return None;
- // };
- // Some(post)
- // }).flatten().collect()
- // }
-
+ #[instrument()]
pub fn from_post_page(page: u64) -> Result<Vec<Self>> {
let Ok(posts) = database::posts::get_post_page(page) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts"))
@@ -40,6 +43,7 @@ impl Post {
Ok(posts)
}
+ #[instrument()]
pub fn from_user_id(user_id: u64) -> Result<Vec<Self>> {
let Ok(posts) = database::posts::get_users_posts(user_id) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts"))
@@ -47,24 +51,29 @@ impl Post {
Ok(posts)
}
+ #[instrument()]
pub fn new(user_id: u64, content: String) -> Result<Self> {
let Ok(post) = database::posts::add_post(user_id, &content) else {
+ tracing::error!("Failed to create post");
return Err(ResponseCode::InternalServerError.text("Failed to create 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 {
@@ -74,7 +83,8 @@ impl Post {
}
if database::posts::update_post(self.post_id, &self.likes, &self.comments).is_err() {
- return Err(ResponseCode::InternalServerError.text("Failed to comment on post"))
+ tracing::error!("Failed to change like state on post");
+ return Err(ResponseCode::InternalServerError.text("Failed to change like state on post"))
}
Ok(())