web/pages/template/
create.rs1use askama::Template;
4use axum::{
5 Extension, Form,
6 response::{IntoResponse, Response},
7};
8use forms::TEMPLATE_CREATE;
9use uuid::Uuid;
10
11use super::draft::TEST_PATH;
12use crate::pages::{
13 HtmlTemplate,
14 forms::{
15 create::{Create, Created},
16 message,
17 },
18};
19use crate::{jwt_auth::JWTAuthMiddleware, response::WebError};
20
21pub const SUBMIT_PATH: &str = "/api/template/create/submit";
22
23const CREATE: Create = Create {
24 form: &TEMPLATE_CREATE,
25 action: SUBMIT_PATH,
26};
27
28#[derive(Template)]
29#[template(path = "pages/template/create.html")]
30struct TemplateCreatePage {
31 form: String,
32 test_path: &'static str,
33}
34
35#[must_use]
37pub fn created_message(id: Uuid) -> String {
38 format!("{}: {id}", t!("Template created"))
39}
40
41pub async fn template_create_page(
42 Extension(jwt_auth): Extension<JWTAuthMiddleware>,
43) -> Result<impl IntoResponse, WebError> {
44 let form = CREATE.blank(jwt_auth.user.id).await?;
45 Ok(HtmlTemplate(TemplateCreatePage {
46 form,
47 test_path: TEST_PATH,
48 }))
49}
50
51pub async fn template_create_submit(
52 Extension(jwt_auth): Extension<JWTAuthMiddleware>,
53 Form(pairs): Form<Vec<(String, String)>>,
54) -> Result<Response, WebError> {
55 Ok(match CREATE.submit(jwt_auth.user.id, pairs).await? {
56 Created::Saved { id, .. } => message::reply(&created_message(id))?,
57 Created::Invalid(response) => response,
58 })
59}
60
61#[cfg(test)]
62#[path = "create_tests.rs"]
63mod tests;