use serde::Serialize; use tracing::instrument; use crate::{ database::{self, comments}, types::http::{ResponseCode, Result}, }; #[derive(Serialize)] pub struct Comment { pub comment_id: u64, pub user_id: u64, pub post_id: u64, pub date: u64, pub content: String, } impl Comment { #[instrument()] pub fn new(user_id: u64, post_id: u64, content: &str) -> Result { let Ok(comment) = comments::add_comment(user_id, post_id, content) else { tracing::error!("Failed to create comment"); return Err(ResponseCode::InternalServerError.text("Failed to create post")) }; Ok(comment) } #[instrument()] pub fn from_comment_page(page: u64, post_id: u64) -> Result> { let Ok(posts) = database::comments::get_comments_page(page, post_id) else { return Err(ResponseCode::BadRequest.text("Failed to fetch comments")) }; Ok(posts) } #[instrument()] pub fn reterieve_all() -> Result> { let Ok(posts) = database::comments::get_all_comments() else { return Err(ResponseCode::InternalServerError.text("Failed to fetch comments")) }; Ok(posts) } }