Lines
100 %
Functions
35 %
Branches
//! The account create page over the `account-create` form.
use askama::Template;
use axum::{
Extension, Form,
response::{IntoResponse, Response},
};
use forms::ACCOUNT_CREATE;
use uuid::Uuid;
use crate::pages::{
HtmlTemplate,
forms::{
create::{Create, Created},
message,
},
use crate::{jwt_auth::JWTAuthMiddleware, response::WebError};
pub const SUBMIT_PATH: &str = "/api/account/create/submit";
const CREATE: Create = Create {
form: &ACCOUNT_CREATE,
action: SUBMIT_PATH,
#[derive(Template)]
#[template(path = "pages/account/create.html")]
struct AccountCreatePage {
form: String,
}
/// The create reply: the new id, then the duplicate-name notice if any.
#[must_use]
pub fn created_message(id: Uuid, notice: Option<&str>) -> String {
let created = format!("{}: {id}", t!("New account id"));
match notice {
Some(notice) => format!("{created}. {notice}"),
None => created,
pub async fn account_create_page(
Extension(jwt_auth): Extension<JWTAuthMiddleware>,
) -> Result<impl IntoResponse, WebError> {
let form = CREATE.blank(jwt_auth.user.id).await?;
Ok(HtmlTemplate(AccountCreatePage { form }))
pub async fn account_create_submit(
Form(pairs): Form<Vec<(String, String)>>,
) -> Result<Response, WebError> {
Ok(match CREATE.submit(jwt_auth.user.id, pairs).await? {
Created::Saved { id, notice } => message::reply(&created_message(id, notice.as_deref()))?,
Created::Invalid(response) => response,
})
#[cfg(test)]
#[path = "create_tests.rs"]
mod tests;