pre-metrics
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
use serde::Deserialize;
|
||||
use utoipa::ToSchema;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Deserialize, Validate, ToSchema)]
|
||||
pub struct JoinRequest {
|
||||
#[validate(length(min = 3, message = "Username must be at least 3 characters long"))]
|
||||
pub username: String,
|
||||
#[validate(length(min = 8, message = "Password must be at least 8 characters long"))]
|
||||
pub password: String,
|
||||
#[validate(must_match(other = "password", message = "Passwords do not match"))]
|
||||
pub password_valid: String,
|
||||
pub super_admin_token: Option<String>,
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use crate::core::AppState;
|
||||
use crate::http::error::HTTPError;
|
||||
use crate::http::validation::ValidatedJson;
|
||||
use crate::routes::core::dto::JoinRequest;
|
||||
use crate::routes::core::mapper::join_request_to_user_am;
|
||||
use axum::extract::State;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
|
||||
pub async fn join(
|
||||
State(state): State<AppState>,
|
||||
ValidatedJson(payload): ValidatedJson<JoinRequest>,
|
||||
) -> Result<impl IntoResponse, HTTPError> {
|
||||
let user_exists = state
|
||||
.repositories
|
||||
.user
|
||||
.username_exists(&payload.username)
|
||||
.await?;
|
||||
|
||||
if user_exists {
|
||||
return Err(HTTPError::validation_error(
|
||||
"username",
|
||||
"Username already exists",
|
||||
));
|
||||
};
|
||||
|
||||
let user_am = join_request_to_user_am(payload)?;
|
||||
let user = state.repositories.user.create(user_am).await?;
|
||||
state
|
||||
.repositories
|
||||
.server
|
||||
.add_user(state.default_server.id, user.id)
|
||||
.await?;
|
||||
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
use crate::auth::password::hash_password;
|
||||
use crate::models::user;
|
||||
use crate::routes::core::dto::JoinRequest;
|
||||
use anyhow::Result as AnyResult;
|
||||
use sea_orm::Set;
|
||||
|
||||
pub fn join_request_to_user_am(payload: JoinRequest) -> AnyResult<user::ActiveModel> {
|
||||
Ok(user::ActiveModel {
|
||||
id: Default::default(),
|
||||
username: Set(payload.username),
|
||||
password: Set(hash_password(&payload.password)?),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
pub mod domain;
|
||||
pub mod dto;
|
||||
pub mod handlers;
|
||||
pub mod mapper;
|
||||
pub mod routes;
|
||||
pub mod service;
|
||||
@@ -0,0 +1,7 @@
|
||||
use super::handlers;
|
||||
use crate::http::OxRouter;
|
||||
use axum::{routing::get, Router};
|
||||
|
||||
pub fn router() -> OxRouter {
|
||||
Router::new().route("/join", get(handlers::join))
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Reference in New Issue
Block a user