use axum::response::Response; use lazy_static::lazy_static; use rand::{distributions::Alphanumeric, Rng}; use tokio::sync::Mutex; use crate::{ console::sanatize, types::{http::ResponseCode, post::Post, session::Session, user::User, comment::Comment, like::Like}, }; lazy_static! { static ref SECRET: Mutex = Mutex::new(String::new()); } pub fn new_secret() -> String { rand::thread_rng() .sample_iter(&Alphanumeric) .take(32) .map(char::from) .collect() } pub async fn get_secret() -> String { let mut secret = SECRET.lock().await; if secret.is_empty() { *secret = new_secret(); } secret.clone() } pub async fn regen_secret() -> String { let mut secret = SECRET.lock().await; *secret = new_secret(); secret.clone() } pub fn generate_users() -> Response { let users = match User::reterieve_all() { Ok(users) => users, Err(err) => return err, }; let mut html = r#" User ID First Name Last Name Email Password Gender Date Day Month Year "# .to_string(); for user in users { html.push_str( &format!("{}{}{}{}{}{}{}{}{}{}", user.user_id, sanatize(&user.firstname), sanatize(&user.lastname), sanatize(&user.email), sanatize(&user.password), sanatize(&user.gender), user.date, user.day, user.month, user.year ) ); } ResponseCode::Success.text(&html) } pub fn generate_posts() -> Response { let posts = match Post::reterieve_all() { Ok(posts) => posts, Err(err) => return err, }; let mut html = r#" Post ID User ID Content Date "# .to_string(); for post in posts { html.push_str(&format!( "{}{}{}{}", post.post_id, post.user_id, sanatize(&post.content), post.date )); } ResponseCode::Success.text(&html) } pub fn generate_sessions() -> Response { let sessions = match Session::reterieve_all() { Ok(sessions) => sessions, Err(err) => return err, }; let mut html = r#" User ID Token "# .to_string(); for session in sessions { html.push_str(&format!( "{}{}", session.user_id, session.token )); } 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) }