From 3d71da490947aacc52a3b77efdc13d5f0458c57f Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Sun, 12 Feb 2023 14:11:50 -0500 Subject: refactor --- src/public/admin.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 10 deletions(-) (limited to 'src/public/admin.rs') diff --git a/src/public/admin.rs b/src/public/admin.rs index 1da2f1e..25941f1 100644 --- a/src/public/admin.rs +++ b/src/public/admin.rs @@ -4,8 +4,8 @@ use rand::{distributions::Alphanumeric, Rng}; use tokio::sync::Mutex; use crate::{ - console::{self, sanatize}, - types::{http::ResponseCode, post::Post, session::Session, user::User}, + console::sanatize, + types::{http::ResponseCode, post::Post, session::Session, user::User, comment::Comment, like::Like}, }; lazy_static! { @@ -79,24 +79,17 @@ pub fn generate_posts() -> Response { Post ID User ID Content - Likes - Comments Date "# .to_string(); for post in posts { - let Ok(likes) = serde_json::to_string(&post.likes) else { continue }; - let Ok(comments) = serde_json::to_string(&post.comments) else { continue }; - html.push_str(&format!( - "{}{}{}{}{}{}", + "{}{}{}{}", post.post_id, post.user_id, sanatize(&post.content), - console::beautify(&likes), - console::beautify(&comments), post.date )); } @@ -127,3 +120,54 @@ pub fn generate_sessions() -> Response { ResponseCode::Success.text(&html) } + +pub fn generate_comments() -> Response { + let comments = match Comment::reterieve_all() { + Ok(comments) => comments, + Err(err) => return err, + }; + + let mut html = r#" + + Comment ID + User ID + Post ID + Content + Date + + "# + .to_string(); + + for comment in comments { + html.push_str(&format!( + "{}{}{}{}{}", + comment.comment_id, comment.user_id, comment.post_id, sanatize(&comment.content), comment.date + )); + } + + ResponseCode::Success.text(&html) +} + +pub fn generate_likes() -> Response { + let likes = match Like::reterieve_all() { + Ok(likes) => likes, + Err(err) => return err, + }; + + let mut html = r#" + + User ID + Post ID + + "# + .to_string(); + + for like in likes { + html.push_str(&format!( + "{}{}", + like.user_id, like.post_id + )); + } + + ResponseCode::Success.text(&html) +} -- cgit v1.2.3-freya