summaryrefslogtreecommitdiff
path: root/src/admin.rs
blob: dec6b7d7223bd0caad3ba0a1e338fe7b42f0cd94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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)
}