Skip to main content

web/pages/account/
create.rs

1//! The account create page over the `account-create` form.
2
3use askama::Template;
4use axum::{
5    Extension, Form,
6    response::{IntoResponse, Response},
7};
8use forms::ACCOUNT_CREATE;
9use uuid::Uuid;
10
11use crate::pages::{
12    HtmlTemplate,
13    forms::{
14        create::{Create, Created},
15        message,
16    },
17};
18use crate::{jwt_auth::JWTAuthMiddleware, response::WebError};
19
20pub const SUBMIT_PATH: &str = "/api/account/create/submit";
21
22const CREATE: Create = Create {
23    form: &ACCOUNT_CREATE,
24    action: SUBMIT_PATH,
25};
26
27#[derive(Template)]
28#[template(path = "pages/account/create.html")]
29struct AccountCreatePage {
30    form: String,
31}
32
33/// The create reply: the new id, then the duplicate-name notice if any.
34#[must_use]
35pub fn created_message(id: Uuid, notice: Option<&str>) -> String {
36    let created = format!("{}: {id}", t!("New account id"));
37    match notice {
38        Some(notice) => format!("{created}. {notice}"),
39        None => created,
40    }
41}
42
43pub async fn account_create_page(
44    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
45) -> Result<impl IntoResponse, WebError> {
46    let form = CREATE.blank(jwt_auth.user.id).await?;
47    Ok(HtmlTemplate(AccountCreatePage { form }))
48}
49
50pub async fn account_create_submit(
51    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
52    Form(pairs): Form<Vec<(String, String)>>,
53) -> Result<Response, WebError> {
54    Ok(match CREATE.submit(jwt_auth.user.id, pairs).await? {
55        Created::Saved { id, notice } => message::reply(&created_message(id, notice.as_deref()))?,
56        Created::Invalid(response) => response,
57    })
58}
59
60#[cfg(test)]
61#[path = "create_tests.rs"]
62mod tests;