Skip to main content

web/pages/tag/
edit.rs

1//! The tag edit page over the `tag-edit` form, and the tag delete.
2
3use askama::Template;
4use axum::{
5    Extension, Form,
6    extract::Path,
7    response::{IntoResponse, Response},
8};
9use forms::TAG_EDIT;
10use server::command::tag::DeleteTag;
11use uuid::Uuid;
12
13use crate::pages::{
14    HtmlTemplate,
15    forms::edit::{Edit, Edited},
16};
17use crate::{jwt_auth::JWTAuthMiddleware, response::WebError};
18
19pub const SUBMIT_PATH: &str = "/api/tag/edit/submit";
20
21const EDIT: Edit = Edit {
22    form: &TAG_EDIT,
23    noun: "Tag",
24    action: SUBMIT_PATH,
25    page: "/tag/edit/{id}",
26    remedy: "",
27};
28
29#[derive(Template)]
30#[template(path = "pages/tag/edit.html")]
31struct TagEditPage {
32    id: Uuid,
33    form: String,
34}
35
36/// The message of a successful edit, shown in the form's result box.
37#[must_use]
38pub fn updated_message(id: Uuid) -> String {
39    format!("{}: {id}", t!("Tag updated"))
40}
41
42pub async fn tag_edit_page(
43    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
44    Path(id): Path<Uuid>,
45) -> Result<impl IntoResponse, WebError> {
46    let form = EDIT.loaded_form(jwt_auth.user.id, &id.to_string()).await?;
47    Ok(HtmlTemplate(TagEditPage { id, form }))
48}
49
50pub async fn tag_edit_submit(
51    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
52    Form(pairs): Form<Vec<(String, String)>>,
53) -> Result<Response, WebError> {
54    Ok(match EDIT.submit(jwt_auth.user.id, pairs).await? {
55        Edited::Saved { id, .. } => {
56            EDIT.saved_response(jwt_auth.user.id, id, updated_message(id))
57                .await?
58        }
59        Edited::Invalid(response) => response,
60    })
61}
62
63pub async fn delete_tag(
64    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
65    Path(id): Path<Uuid>,
66) -> Result<impl IntoResponse, WebError> {
67    DeleteTag::new()
68        .user_id(jwt_auth.user.id)
69        .tag_id(id)
70        .run()
71        .await?;
72    Ok(t!("Tag deleted"))
73}
74
75#[cfg(test)]
76#[path = "edit_tests.rs"]
77mod tests;