xssbook/src/types/post.rs

93 lines
2.5 KiB
Rust
Raw Normal View History

2023-01-28 16:52:32 +00:00
use core::fmt;
2023-01-26 22:29:16 +00:00
use serde::Serialize;
2023-01-28 16:52:32 +00:00
use tracing::instrument;
2023-01-26 22:29:16 +00:00
2023-02-15 05:01:44 +00:00
use crate::database::Database;
2023-01-28 23:04:00 +00:00
use crate::types::http::{ResponseCode, Result};
2023-01-26 22:29:16 +00:00
2023-02-12 19:11:50 +00:00
use super::comment::Comment;
2023-01-26 22:29:16 +00:00
#[derive(Serialize)]
pub struct Post {
pub post_id: u64,
pub user_id: u64,
pub content: String,
2023-01-28 23:04:00 +00:00
pub date: u64,
2023-02-12 19:11:50 +00:00
pub likes: u64,
pub liked: bool,
pub comments: Vec<Comment>,
2023-01-26 22:29:16 +00:00
}
2023-01-28 16:52:32 +00:00
impl fmt::Debug for Post {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Post")
2023-01-28 23:04:00 +00:00
.field("post_id", &self.post_id)
.finish()
2023-01-28 16:52:32 +00:00
}
}
2023-01-26 22:29:16 +00:00
impl Post {
2023-02-15 05:01:44 +00:00
#[instrument(skip(db))]
pub fn from_post_id(db: &Database, self_id: u64, post_id: u64) -> Result<Self> {
let Ok(Some(mut post)) = db.get_post(post_id) else {
return Err(ResponseCode::BadRequest.text("Post does not exist"))
2023-01-26 22:29:16 +00:00
};
2023-02-15 05:01:44 +00:00
let liked = db.get_liked(self_id, post.post_id).unwrap_or(false);
2023-02-15 03:16:29 +00:00
post.liked = liked;
2023-01-26 22:29:16 +00:00
Ok(post)
}
2023-02-15 05:01:44 +00:00
#[instrument(skip(db))]
pub fn from_post_page(db: &Database, self_id: u64, page: u64) -> Result<Vec<Self>> {
let Ok(mut posts) = db.get_post_page(page) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts"))
2023-01-26 22:29:16 +00:00
};
2023-02-15 03:16:29 +00:00
for post in &mut posts {
2023-02-15 05:01:44 +00:00
let liked = db.get_liked(self_id, post.post_id).unwrap_or(false);
2023-02-15 03:16:29 +00:00
post.liked = liked;
}
2023-01-26 22:29:16 +00:00
Ok(posts)
}
2023-02-15 05:01:44 +00:00
#[instrument(skip(db))]
pub fn from_user_post_page(
db: &Database,
self_id: u64,
user_id: u64,
page: u64,
) -> Result<Vec<Self>> {
let Ok(mut posts) = db.get_users_post_page(user_id, page) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts"))
2023-01-26 22:29:16 +00:00
};
2023-02-15 03:16:29 +00:00
for post in &mut posts {
2023-02-15 05:01:44 +00:00
let liked = db.get_liked(self_id, post.post_id).unwrap_or(false);
2023-02-15 03:16:29 +00:00
post.liked = liked;
}
2023-01-26 22:29:16 +00:00
Ok(posts)
}
2023-02-15 05:01:44 +00:00
#[instrument(skip(db))]
pub fn reterieve_all(db: &Database) -> Result<Vec<Self>> {
let Ok(posts) = db.get_all_posts() else {
2023-01-30 00:28:48 +00:00
return Err(ResponseCode::InternalServerError.text("Failed to fetch posts"))
};
Ok(posts)
}
2023-02-15 05:01:44 +00:00
#[instrument(skip(db))]
pub fn new(db: &Database, user_id: u64, content: String) -> Result<Self> {
let Ok(post) = db.add_post(user_id, &content) else {
2023-01-28 16:52:32 +00:00
tracing::error!("Failed to create post");
return Err(ResponseCode::InternalServerError.text("Failed to create post"))
2023-01-26 22:29:16 +00:00
};
Ok(post)
}
2023-01-28 23:04:00 +00:00
}