Register endpoint #16
@@ -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<sqlx::PgPool>,
|
||||
Json(payload): Json<models::common::CreateUser>,
|
||||
) -> (StatusCode, Json<models::common::User>) {
|
||||
) -> (StatusCode, Json<response::Response>) {
|
||||
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,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
@@ -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";
|
||||
|
@@ -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,
|
||||
|
20
src/repo/mod.rs
Normal file
20
src/repo/mod.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
pub mod user {
|
||||
use crate::models;
|
||||
|
||||
pub async fn insert(
|
||||
pool: &sqlx::PgPool,
|
||||
user: &models::common::User,
|
||||
) -> Result<uuid::Uuid, sqlx::Error> {
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user