use serde::Serialize; use tracing::instrument; use crate::{ database::Database, 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(skip(db))] pub fn new(db: &Database, user_id: u64, post_id: u64, content: &str) -> Result { let Ok(comment) = db.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(skip(db))] pub fn from_comment_page(db: &Database, page: u64, post_id: u64) -> Result> { let Ok(posts) = db.get_comments_page(page, post_id) else { return Err(ResponseCode::BadRequest.text("Failed to fetch comments")) }; Ok(posts) } #[instrument(skip(db))] pub fn reterieve_all(db: &Database) -> Result> { let Ok(posts) = db.get_all_comments() else { return Err(ResponseCode::InternalServerError.text("Failed to fetch comments")) }; Ok(posts) } }