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

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

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

            
20
pub const SUBMIT_PATH: &str = "/api/account/create/submit";
21

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

            
27
#[derive(Template)]
28
#[template(path = "pages/account/create.html")]
29
struct AccountCreatePage {
30
    form: String,
31
}
32

            
33
/// The create reply: the new id, then the duplicate-name notice if any.
34
#[must_use]
35
11
pub fn created_message(id: Uuid, notice: Option<&str>) -> String {
36
11
    let created = format!("{}: {id}", t!("New account id"));
37
11
    match notice {
38
6
        Some(notice) => format!("{created}. {notice}"),
39
5
        None => created,
40
    }
41
11
}
42

            
43
2
pub async fn account_create_page(
44
2
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
45
2
) -> Result<impl IntoResponse, WebError> {
46
2
    let form = CREATE.blank(jwt_auth.user.id).await?;
47
2
    Ok(HtmlTemplate(AccountCreatePage { form }))
48
2
}
49

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

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