summaryrefslogtreecommitdiff
path: root/src/public/admin.rs
blob: 25941f126823953815cb2303155971bf67a1bd0e (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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<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();
    }
    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#"
        <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>Date</th>
        </tr>
    "#
    .to_string();

    for post in posts {
        html.push_str(&format!(
            "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>",
            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#"
        <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)
}

pub fn generate_comments() -> Response {
    let comments = match Comment::reterieve_all() {
        Ok(comments) => comments,
        Err(err) => return err,
    };

    let mut html = r#"
        <tr>
            <th>Comment ID</th>
            <th>User ID</th>
            <th>Post ID</th>
            <th>Content</th>
            <th>Date</th>
        </tr>
    "#
    .to_string();

    for comment in comments {
        html.push_str(&format!(
            "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>",
            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#"
        <tr>
            <th>User ID</th>
            <th>Post ID</th>
        </tr>
    "#
    .to_string();

    for like in likes {
        html.push_str(&format!(
            "<tr><td>{}</td><td>{}</td></tr>",
            like.user_id, like.post_id
        ));
    }

    ResponseCode::Success.text(&html)
}