1
use askama::Template;
2
use axum::{Extension, extract::State, http::StatusCode, 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};
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
pub async fn script_table(
32
    State(_data): State<Arc<AppState>>,
33
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
34
) -> Result<impl IntoResponse, (StatusCode, String)> {
35
    let user = &jwt_auth.user;
36
    let server_user = server::user::User { id: user.id };
37

            
38
    let scripts = server_user.list_scripts().await.map_err(|e| {
39
        (
40
            StatusCode::INTERNAL_SERVER_ERROR,
41
            format!("DB error: {e:?}"),
42
        )
43
    })?;
44

            
45
    let script_data: Vec<ScriptData> = scripts
46
        .into_iter()
47
        .map(|s| ScriptData {
48
            id: s.id,
49
            name: s.name,
50
            size: s.size,
51
            enabled: s.enabled.as_deref() != Some("false"),
52
        })
53
        .collect();
54

            
55
    let template = ScriptTableTemplate {
56
        scripts: script_data,
57
    };
58
    Ok(HtmlTemplate(template))
59
}