diff --git a/src/callers/register.rs b/src/callers/register.rs index 1330ef4..4436a98 100644 --- a/src/callers/register.rs +++ b/src/callers/register.rs @@ -1,52 +1,42 @@ use axum::{Json, http::StatusCode}; -// use sqlx::{Executor, PgPool, Row, postgres::PgPoolOptions}; // Added Row for potential use use crate::models; +use crate::repo; + +mod response { + use serde::{Deserialize, Serialize}; + + use crate::models; + + #[derive(Deserialize, Serialize)] + pub struct Response { + pub message: String, + pub data: models::common::User, + } +} pub async fn register_user( axum::Extension(pool): axum::Extension, Json(payload): Json, -) -> (StatusCode, Json) { +) -> (StatusCode, Json) { let mut user = models::common::User { id: uuid::Uuid::nil(), username: payload.username.clone(), password: payload.password.clone(), }; - let insert_sql = "INSERT INTO \"user\" (username, password) VALUES ($1, $2) RETURNING id"; - println!("SQL: {:?}", insert_sql); - /* - sqlx::query(insert_sql) - .bind(&user.username) - .bind(&user.password) - .execute(&pool) - .await; - */ - - /* - let returned_id: uuid::Uuid = sqlx::query_scalar(insert_sql) - .bind(&user.username) - .bind(&user.password) - .fetch_one(&pool).await? // fetch_one expects exactly one row - */ - let id = match sqlx::query_scalar(insert_sql) - .bind(&user.username) // Bind the input message securely - .bind(&user.password) - .fetch_one(&pool) // Execute and expect exactly ONE row with ONE column back - .await - { - Ok(o) => o, - _ => { - uuid::Uuid::nil() - // (StatusCode::BAD_REQUEST, Json(user)) + match repo::user::insert(&pool, &user).await { + Ok(id) => { + println!("User inserted."); + user.id = id; + (StatusCode::CREATED, Json(response::Response{ + message: String::from("User inserted"), + data: user, + })) } - }; - - if id != uuid::Uuid::nil() { - println!("User inserted."); - user.id = id; - (StatusCode::CREATED, Json(user)) - } else { - (StatusCode::BAD_REQUEST, Json(user)) + Err(err) => (StatusCode::BAD_REQUEST, Json(response::Response{ + message: err.to_string(), + data: user, + })) } } diff --git a/src/lib.rs b/src/lib.rs index f1f9d66..ab8bf15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub mod callers; pub mod config; pub mod hashing; pub mod models; +pub mod repo; mod keys { pub const DBURL: &str = "DATABASE_URL"; diff --git a/src/models/common.rs b/src/models/common.rs index 6838cfb..8bf5ab7 100644 --- a/src/models/common.rs +++ b/src/models/common.rs @@ -6,7 +6,7 @@ pub struct CreateUser { pub password: String, } -#[derive(Serialize)] +#[derive(Deserialize, Serialize)] pub struct User { pub id: uuid::Uuid, pub username: String, diff --git a/src/repo/mod.rs b/src/repo/mod.rs new file mode 100644 index 0000000..95d3c7a --- /dev/null +++ b/src/repo/mod.rs @@ -0,0 +1,20 @@ +pub mod user { + use crate::models; + + pub async fn insert( + pool: &sqlx::PgPool, + user: &models::common::User, + ) -> Result { + let insert_sql = "INSERT INTO \"user\" (username, password) VALUES ($1, $2) RETURNING id"; + + match sqlx::query_scalar(insert_sql) + .bind(&user.username) // Bind the input message securely + .bind(&user.password) + .fetch_one(pool) // Execute and expect exactly ONE row with ONE column back + .await + { + Ok(o) => Ok(o), + Err(err) => Err(err), // _ => uuid::Uuid::nil(), + } + } +}