1use askama::Template;
2use axum::{Extension, extract::State, response::IntoResponse};
3use serde::Serialize;
4use std::sync::Arc;
5use uuid::Uuid;
6
7use crate::{AppState, jwt_auth::JWTAuthMiddleware, pages::HtmlTemplate, response::WebError};
8
9#[derive(Template)]
10#[template(path = "pages/script/list.html")]
11struct ScriptListPage;
12
13pub async fn script_list_page() -> impl IntoResponse {
14 HtmlTemplate(ScriptListPage {})
15}
16
17#[derive(Serialize)]
18struct 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")]
27struct ScriptTableTemplate {
28 scripts: Vec<ScriptData>,
29}
30
31pub async fn script_table(
32 State(_data): State<Arc<AppState>>,
33 Extension(jwt_auth): Extension<JWTAuthMiddleware>,
34) -> Result<impl IntoResponse, WebError> {
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?;
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}
55
56#[cfg(test)]
57#[path = "list_tests.rs"]
58mod tests;