1
use askama::Template;
2
use axum::{Extension, extract::State, response::IntoResponse};
3
use serde::Serialize;
4
use std::sync::Arc;
5
use uuid::Uuid;
6

            
7
use crate::{AppState, jwt_auth::JWTAuthMiddleware, pages::HtmlTemplate, response::WebError};
8

            
9
#[derive(Template)]
10
#[template(path = "pages/script/list.html")]
11
struct ScriptListPage;
12

            
13
pub async fn script_list_page() -> impl IntoResponse {
14
    HtmlTemplate(ScriptListPage {})
15
}
16

            
17
#[derive(Serialize)]
18
struct ScriptData {
19
    id: Uuid,
20
    name: Option<String>,
21
    size: i32,
22
    enabled: bool,
23
}
24

            
25
#[derive(Template)]
26
#[template(path = "components/script/table.html")]
27
struct ScriptTableTemplate {
28
    scripts: Vec<ScriptData>,
29
}
30

            
31
1
pub async fn script_table(
32
1
    State(_data): State<Arc<AppState>>,
33
1
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
34
1
) -> Result<impl IntoResponse, WebError> {
35
1
    let user = &jwt_auth.user;
36
1
    let server_user = server::user::User { id: user.id };
37

            
38
1
    let scripts = server_user.list_scripts().await?;
39

            
40
    let script_data: Vec<ScriptData> = scripts
41
        .into_iter()
42
        .map(|s| ScriptData {
43
            id: s.id,
44
            name: s.name,
45
            size: s.size,
46
            enabled: s.enabled.as_deref() != Some("false"),
47
        })
48
        .collect();
49

            
50
    let template = ScriptTableTemplate {
51
        scripts: script_data,
52
    };
53
    Ok(HtmlTemplate(template))
54
1
}
55

            
56
#[cfg(test)]
57
#[path = "list_tests.rs"]
58
mod tests;