feat: add removal of scripts
This commit is contained in:
parent
ff090c9d68
commit
001be86948
13
src/db.rs
13
src/db.rs
@ -106,6 +106,19 @@ pub async fn get_script<T: AsRef<str>>(pool: &SqlitePool, id: T) -> Result<(Uuid
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub async fn delete_script<T: AsRef<str>>(pool: &SqlitePool, id: T) -> Result<(), String> {
|
||||||
|
if let Err(e) = sqlx::query("DELETE FROM scripts WHERE id = ?")
|
||||||
|
.bind(id.as_ref())
|
||||||
|
.execute(pool)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Err(e.to_string())
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Return Vec<Script> instead of String-Tuple
|
// TODO: Return Vec<Script> instead of String-Tuple
|
||||||
// TODO: Add Custom Error Type
|
// TODO: Add Custom Error Type
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use axum::routing::{get, get_service};
|
use axum::routing::{delete, get, get_service};
|
||||||
use routes::scripts::{get_script_by_id, get_scripts};
|
use routes::scripts::{delete_script, get_script_by_id, get_scripts};
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
mod routes;
|
mod routes;
|
||||||
@ -9,5 +9,6 @@ pub fn new_router(db: SqlitePool) -> axum::Router {
|
|||||||
.fallback_service(get_service(ServeDir::new("assets")))
|
.fallback_service(get_service(ServeDir::new("assets")))
|
||||||
.route("/scripts", get(get_scripts))
|
.route("/scripts", get(get_scripts))
|
||||||
.route("/scripts/{id}", get(get_script_by_id))
|
.route("/scripts/{id}", get(get_script_by_id))
|
||||||
|
.route("/scripts/{id}", delete(delete_script))
|
||||||
.with_state(db)
|
.with_state(db)
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,32 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{Html, IntoResponse},
|
response::{Html, IntoResponse, Redirect},
|
||||||
};
|
};
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use tracing::{error, instrument, warn};
|
use tracing::{error, info, instrument, warn};
|
||||||
|
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all, fields(id = ?Path(&id)))]
|
||||||
|
pub async fn delete_script(
|
||||||
|
State(database): State<SqlitePool>,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
info!("deleting");
|
||||||
|
if let Err(e) = crate::db::delete_script(&database, id).await {
|
||||||
|
warn!(err = e.to_string(), "delete_script");
|
||||||
|
return Err(StatusCode::NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Redirect::to("/"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip_all, fields(id = ?Path(&id)))]
|
||||||
pub async fn get_script_by_id(
|
pub async fn get_script_by_id(
|
||||||
State(database): State<SqlitePool>,
|
State(database): State<SqlitePool>,
|
||||||
Path(id): Path<String>,
|
Path(id): Path<String>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
|
info!("retrieving");
|
||||||
let script = match crate::db::get_script(&database, id).await {
|
let script = match crate::db::get_script(&database, id).await {
|
||||||
Ok(script) => script,
|
Ok(script) => script,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@ -21,10 +36,9 @@ pub async fn get_script_by_id(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let output = format!(
|
let output = format!(
|
||||||
r##"<section id="{}"><h1>{}</h1><div id="{}-parameters">{}</div></section>"##,
|
r##"<section id="id-{0}"><h1>{1}</h1><div id="id-{0}-parameters">{2}</div><button hx-push-url="/" hx-target="body" hx-swap="outerHTML" hx-delete="/scripts/{0}">DELETE</button></section>"##,
|
||||||
script.0,
|
script.0,
|
||||||
script.1.name(),
|
script.1.name(),
|
||||||
script.0,
|
|
||||||
script
|
script
|
||||||
.1
|
.1
|
||||||
.parameters()
|
.parameters()
|
||||||
@ -35,7 +49,7 @@ pub async fn get_script_by_id(
|
|||||||
return String::new();
|
return String::new();
|
||||||
}
|
}
|
||||||
acc
|
acc
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(Html(output))
|
Ok(Html(output))
|
||||||
@ -43,6 +57,7 @@ pub async fn get_script_by_id(
|
|||||||
|
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
pub async fn get_scripts(State(database): State<SqlitePool>) -> impl IntoResponse {
|
pub async fn get_scripts(State(database): State<SqlitePool>) -> impl IntoResponse {
|
||||||
|
info!("listing");
|
||||||
let scripts = match crate::db::list_scripts(&database).await {
|
let scripts = match crate::db::list_scripts(&database).await {
|
||||||
Ok(scripts) => scripts,
|
Ok(scripts) => scripts,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user