summaryrefslogtreecommitdiff
path: root/src/admin.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/admin.rs')
-rw-r--r--src/admin.rs125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/admin.rs b/src/admin.rs
new file mode 100644
index 0000000..dec6b7d
--- /dev/null
+++ b/src/admin.rs
@@ -0,0 +1,125 @@
+use axum::response::Response;
+use lazy_static::lazy_static;
+use rand::{distributions::Alphanumeric, Rng};
+use tokio::sync::Mutex;
+
+use crate::{types::{user::User, http::ResponseCode, post::Post, session::Session}, console::{self, sanatize}};
+
+lazy_static! {
+ static ref SECRET: Mutex<String> = 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();
+ }
+ return secret.clone();
+}
+
+pub async fn regen_secret() -> String {
+ let mut secret = SECRET.lock().await;
+ *secret = new_secret();
+ return secret.clone();
+}
+
+pub fn generate_users() -> Response {
+
+ let users = match User::reterieve_all() {
+ Ok(users) => users,
+ Err(err) => return err,
+ };
+
+ let mut html = r#"
+ <tr>
+ <th>User ID</th>
+ <th>First Name</th>
+ <th>Last Name</th>
+ <th>Email</th>
+ <th>Password</th>
+ <th>Gender</th>
+ <th>Date</th>
+ <th>Day</th>
+ <th>Month</th>
+ <th>Year</th>
+ </tr>
+ "#.to_string();
+
+ for user in users {
+ html.push_str(
+ &format!("<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>",
+ 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#"
+ <tr>
+ <th>Post ID</th>
+ <th>User ID</th>
+ <th>Content</th>
+ <th>Likes</th>
+ <th>Comments</th>
+ <th>Date</th>
+ </tr>
+ "#.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!("<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>",
+ post.post_id, post.user_id, sanatize(post.content), console::beautify(likes),
+ console::beautify(comments), 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#"
+ <tr>
+ <th>User ID</th>
+ <th>Token</th>
+ </tr>
+ "#.to_string();
+
+ for session in sessions {
+ html.push_str(
+ &format!("<tr><td>{}</td><td>{}</td></tr>",
+ session.user_id, session.token
+ )
+ );
+ }
+
+ ResponseCode::Success.text(&html)
+} \ No newline at end of file