Lines
100 %
Functions
32.14 %
Branches
//! The tag edit page over the `tag-edit` form, and the tag delete.
use askama::Template;
use axum::{
Extension, Form,
extract::Path,
response::{IntoResponse, Response},
};
use forms::TAG_EDIT;
use server::command::tag::DeleteTag;
use uuid::Uuid;
use crate::pages::{
HtmlTemplate,
forms::edit::{Edit, Edited},
use crate::{jwt_auth::JWTAuthMiddleware, response::WebError};
pub const SUBMIT_PATH: &str = "/api/tag/edit/submit";
const EDIT: Edit = Edit {
form: &TAG_EDIT,
noun: "Tag",
action: SUBMIT_PATH,
page: "/tag/edit/{id}",
remedy: "",
#[derive(Template)]
#[template(path = "pages/tag/edit.html")]
struct TagEditPage {
id: Uuid,
form: String,
}
/// 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!("Tag updated"))
pub async fn tag_edit_page(
Extension(jwt_auth): Extension<JWTAuthMiddleware>,
Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, WebError> {
let form = EDIT.loaded_form(jwt_auth.user.id, &id.to_string()).await?;
Ok(HtmlTemplate(TagEditPage { id, form }))
pub async fn tag_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_tag(
DeleteTag::new()
.user_id(jwt_auth.user.id)
.tag_id(id)
.run()
.await?;
Ok(t!("Tag deleted"))
#[cfg(test)]
#[path = "edit_tests.rs"]
mod tests;