Lines
0 %
Functions
Branches
100 %
use askama::Template;
use axum::{Extension, extract::State, http::StatusCode, response::IntoResponse};
use serde::Serialize;
use std::sync::Arc;
use uuid::Uuid;
use crate::{AppState, jwt_auth::JWTAuthMiddleware, pages::HtmlTemplate};
#[derive(Template)]
#[template(path = "pages/script/list.html")]
struct ScriptListPage;
pub async fn script_list_page() -> impl IntoResponse {
HtmlTemplate(ScriptListPage {})
}
#[derive(Serialize)]
struct ScriptData {
id: Uuid,
name: Option<String>,
size: i32,
enabled: bool,
#[template(path = "components/script/table.html")]
struct ScriptTableTemplate {
scripts: Vec<ScriptData>,
pub async fn script_table(
State(_data): State<Arc<AppState>>,
Extension(jwt_auth): Extension<JWTAuthMiddleware>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
let user = &jwt_auth.user;
let server_user = server::user::User { id: user.id };
let scripts = server_user.list_scripts().await.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("DB error: {e:?}"),
)
})?;
let script_data: Vec<ScriptData> = scripts
.into_iter()
.map(|s| ScriptData {
id: s.id,
name: s.name,
size: s.size,
enabled: s.enabled.as_deref() != Some("false"),
})
.collect();
let template = ScriptTableTemplate {
scripts: script_data,
};
Ok(HtmlTemplate(template))