added error handling

This commit is contained in:
2024-12-10 03:59:34 -08:00
parent 6ce6b9289a
commit 4caa0c8328
7 changed files with 160 additions and 23 deletions

View File

@@ -1,11 +1,31 @@
use axum::{extract::State, response::Response};
use crate::*;
use sqlx::query;
use crate::state::AppState;
use super::*;
#[derive(Serialize, Deserialize, ToSchema)]
pub struct SignupBody {
real_name: String,
username: String,
email: String,
password: String,
}
/// Sign up
#[utoipa::path(get, path = "/signup", responses((status = OK, body = String)), tag = super::AUTH_TAG)]
pub async fn signup(State(state): State<AppState>) -> Response<String> {
Response::new("Hello, World!".to_string())
pub async fn signup(
State(state): State<AppState>,
Json(body): Json<SignupBody>,
) -> Result<String, AppError> {
let user = query!(
"INSERT INTO users (real_name, username, email, password_hash)
VALUES ($1, $2, $3, $4)
RETURNING id, real_name, username, email, password_hash, is_admin, created_at",
body.real_name,
body.username,
body.email,
body.password,
)
.fetch_one(&*state.db)
.await?;
Ok(user.id.to_string())
}