Lines
100 %
Functions
37.5 %
Branches
//! The script edit page over the `script-edit` form, and the script delete.
use askama::Template;
use axum::{
Extension, Form,
extract::Path,
response::{IntoResponse, Response},
};
use forms::SCRIPT_EDIT;
use server::command::script::DeleteScript;
use uuid::Uuid;
use crate::pages::{
HtmlTemplate,
forms::edit::{Edit, Edited},
use crate::{jwt_auth::JWTAuthMiddleware, parse::parse_uuid, response::WebError};
pub const SUBMIT_PATH: &str = "/api/script/edit/submit";
const EDIT: Edit = Edit {
form: &SCRIPT_EDIT,
noun: "Script",
action: SUBMIT_PATH,
page: "/script/edit/{id}",
remedy: "",
#[derive(Template)]
#[template(path = "pages/script/edit.html")]
struct ScriptEditPage {
id: Uuid,
form: String,
notice: Option<String>,
}
fn parse_script_id(raw: &str) -> Result<Uuid, WebError> {
parse_uuid(raw, "script ID")
/// The message of a successful edit, shown in the form's result box.
#[must_use]
pub fn updated_message(id: Uuid) -> String {
format!("{}: {id}", t!("Script updated"))
pub async fn script_edit_page(
Extension(jwt_auth): Extension<JWTAuthMiddleware>,
Path(id): Path<String>,
) -> Result<impl IntoResponse, WebError> {
let id = parse_script_id(&id)?;
let (form, notice) = EDIT
.loaded_noticed(jwt_auth.user.id, &id.to_string())
.await?;
Ok(HtmlTemplate(ScriptEditPage { id, form, notice }))
pub async fn script_edit_submit(
Form(pairs): Form<Vec<(String, String)>>,
) -> Result<Response, WebError> {
Ok(match EDIT.submit(jwt_auth.user.id, pairs).await? {
Edited::Saved { id, .. } => {
EDIT.saved_response(jwt_auth.user.id, id, updated_message(id))
.await?
Edited::Invalid(response) => response,
})
pub async fn delete_script(
DeleteScript::new()
.user_id(jwt_auth.user.id)
.script_id(id)
.run()
Ok(format!("{}: {id}", t!("Script deleted")))
#[cfg(test)]
#[path = "edit_tests.rs"]
mod tests;