Compare commits
2 Commits
886fddfc41
...
8d8857cace
Author | SHA1 | Date | |
---|---|---|---|
8d8857cace | |||
01af05b999 |
24
src/db.rs
24
src/db.rs
@ -86,6 +86,30 @@ struct ScriptRow {
|
||||
positional_binding: Option<bool>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn get_script<T: AsRef<str>>(pool: &SqlitePool, id: T) -> Result<(Uuid, Script), String> {
|
||||
let script_row = sqlx::query_as::<_, ScriptRow>("SELECT id, name, path, parameters, default_parameter_set, help_uri, supports_paging, supports_should_process, positional_binding FROM scripts WHERE id = ?").bind(id.as_ref())
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let params: Vec<Parameter> =
|
||||
serde_json::from_str(&script_row.parameters).map_err(|e| e.to_string())?;
|
||||
Ok((
|
||||
Uuid::parse_str(&script_row.id).map_err(|e| e.to_string())?,
|
||||
Script::builder()
|
||||
.set_name(script_row.name)
|
||||
.set_path(script_row.path)
|
||||
.set_parameters(params)
|
||||
.set_default_parameter_set(script_row.default_parameter_set)
|
||||
.set_help_uri(script_row.help_uri)
|
||||
.set_supports_paging(script_row.supports_paging)
|
||||
.set_supports_should_process(script_row.supports_should_process)
|
||||
.set_positional_binding(script_row.positional_binding)
|
||||
.build()
|
||||
.map_err(|e| e.to_string())?,
|
||||
))
|
||||
}
|
||||
|
||||
// TODO: Return Vec<Script> instead of String-Tuple
|
||||
// TODO: Add Custom Error Type
|
||||
#[allow(dead_code)]
|
||||
|
@ -1,5 +1,5 @@
|
||||
use axum::routing::{get, get_service};
|
||||
use routes::scripts::get_scripts;
|
||||
use routes::scripts::{get_script_by_id, get_scripts};
|
||||
use sqlx::SqlitePool;
|
||||
use tower_http::services::ServeDir;
|
||||
mod routes;
|
||||
@ -8,5 +8,6 @@ pub fn new_router(db: SqlitePool) -> axum::Router {
|
||||
axum::Router::new()
|
||||
.fallback_service(get_service(ServeDir::new("assets")))
|
||||
.route("/scripts", get(get_scripts))
|
||||
.route("/scripts/{id}", get(get_script_by_id))
|
||||
.with_state(db)
|
||||
}
|
||||
|
@ -1,11 +1,45 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
};
|
||||
use sqlx::SqlitePool;
|
||||
use std::fmt::Write;
|
||||
use tracing::{error, instrument};
|
||||
use tracing::{error, instrument, warn};
|
||||
|
||||
#[instrument(skip_all)]
|
||||
pub async fn get_script_by_id(
|
||||
State(database): State<SqlitePool>,
|
||||
Path(id): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
let script = match crate::db::get_script(&database, id).await {
|
||||
Ok(script) => script,
|
||||
Err(e) => {
|
||||
warn!(err = e.to_string(), "get_script");
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
}
|
||||
};
|
||||
|
||||
let output = format!(
|
||||
r##"<section id="{}"><h1>{}</h1><div id="{}-parameters">{}</div></section>"##,
|
||||
script.0,
|
||||
script.1.name(),
|
||||
script.0,
|
||||
script
|
||||
.1
|
||||
.parameters()
|
||||
.iter()
|
||||
.fold(String::new(), |mut acc, x| {
|
||||
if let Err(e) = write!(acc, "<p>{:?}</p>", x) {
|
||||
error!(err = e.to_string(), script = ?x, "failed to write parameter");
|
||||
return String::new();
|
||||
}
|
||||
acc
|
||||
})
|
||||
);
|
||||
|
||||
Ok(Html(output))
|
||||
}
|
||||
|
||||
#[instrument(skip_all)]
|
||||
pub async fn get_scripts(State(database): State<SqlitePool>) -> impl IntoResponse {
|
||||
@ -18,14 +52,23 @@ pub async fn get_scripts(State(database): State<SqlitePool>) -> impl IntoRespons
|
||||
};
|
||||
|
||||
let content = scripts.iter().fold(String::new(), |mut acc, x| {
|
||||
if let Err(e) = write!(acc, "<li>{} [", x.1.name()) {
|
||||
error!(err = e.to_string(), script=?x ,"failed to write name");
|
||||
return String::new();
|
||||
}
|
||||
if let Err(e) = write!(acc, "{}]</li>", x.0) {
|
||||
error!(err = e.to_string(), script=?x ,"failed to write id");
|
||||
if let Err(e) = write!(
|
||||
acc,
|
||||
r##"<li hx-get="/scripts/{0}" hx-push-url="/scripts/{0}" hx-target="#scripts" hx-swap="outerHTML">{1} [{0}]</li>"##,
|
||||
x.0,
|
||||
x.1.name()
|
||||
) {
|
||||
error!(err = e.to_string(), script=?x ,"failed to write script");
|
||||
return String::new();
|
||||
}
|
||||
// if let Err(e) = write!(acc, "{} [", x.1.name()) {
|
||||
// error!(err = e.to_string(), script=?x ,"failed to write id");
|
||||
// return String::new();
|
||||
// }
|
||||
// if let Err(e) = write!(acc, "{}]</li>", x.0) {
|
||||
// error!(err = e.to_string(), script=?x ,"failed to write id");
|
||||
// return String::new();
|
||||
// }
|
||||
acc
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user