1
//! The template create page over the `template-create` form.
2

            
3
use askama::Template;
4
use axum::{
5
    Extension, Form,
6
    response::{IntoResponse, Response},
7
};
8
use forms::TEMPLATE_CREATE;
9
use uuid::Uuid;
10

            
11
use super::draft::TEST_PATH;
12
use crate::pages::{
13
    HtmlTemplate,
14
    forms::{
15
        create::{Create, Created},
16
        message,
17
    },
18
};
19
use crate::{jwt_auth::JWTAuthMiddleware, response::WebError};
20

            
21
pub const SUBMIT_PATH: &str = "/api/template/create/submit";
22

            
23
const CREATE: Create = Create {
24
    form: &TEMPLATE_CREATE,
25
    action: SUBMIT_PATH,
26
};
27

            
28
#[derive(Template)]
29
#[template(path = "pages/template/create.html")]
30
struct TemplateCreatePage {
31
    form: String,
32
    test_path: &'static str,
33
}
34

            
35
/// The message of a successful create, shown in the form's result box.
36
#[must_use]
37
4
pub fn created_message(id: Uuid) -> String {
38
4
    format!("{}: {id}", t!("Template created"))
39
4
}
40

            
41
1
pub async fn template_create_page(
42
1
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
43
1
) -> Result<impl IntoResponse, WebError> {
44
1
    let form = CREATE.blank(jwt_auth.user.id).await?;
45
1
    Ok(HtmlTemplate(TemplateCreatePage {
46
1
        form,
47
1
        test_path: TEST_PATH,
48
1
    }))
49
1
}
50

            
51
2
pub async fn template_create_submit(
52
2
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
53
2
    Form(pairs): Form<Vec<(String, String)>>,
54
2
) -> Result<Response, WebError> {
55
2
    Ok(match CREATE.submit(jwt_auth.user.id, pairs).await? {
56
1
        Created::Saved { id, .. } => message::reply(&created_message(id))?,
57
1
        Created::Invalid(response) => response,
58
    })
59
2
}
60

            
61
#[cfg(test)]
62
#[path = "create_tests.rs"]
63
mod tests;