Cleanup
Some checks failed
Rust Build / Check (pull_request) Successful in 59s
Rust Build / Test Suite (pull_request) Successful in 1m34s
Rust Build / Rustfmt (pull_request) Failing after 37s
Rust Build / Clippy (pull_request) Successful in 1m16s
Rust Build / build (pull_request) Successful in 2m2s
Some checks failed
Rust Build / Check (pull_request) Successful in 59s
Rust Build / Test Suite (pull_request) Successful in 1m34s
Rust Build / Rustfmt (pull_request) Failing after 37s
Rust Build / Clippy (pull_request) Successful in 1m16s
Rust Build / build (pull_request) Successful in 2m2s
This commit is contained in:
@@ -1,52 +1,42 @@
|
|||||||
use axum::{Json, http::StatusCode};
|
use axum::{Json, http::StatusCode};
|
||||||
// use sqlx::{Executor, PgPool, Row, postgres::PgPoolOptions}; // Added Row for potential use
|
|
||||||
|
|
||||||
use crate::models;
|
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(
|
pub async fn register_user(
|
||||||
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
Json(payload): Json<models::common::CreateUser>,
|
Json(payload): Json<models::common::CreateUser>,
|
||||||
) -> (StatusCode, Json<models::common::User>) {
|
) -> (StatusCode, Json<response::Response>) {
|
||||||
let mut user = models::common::User {
|
let mut user = models::common::User {
|
||||||
id: uuid::Uuid::nil(),
|
id: uuid::Uuid::nil(),
|
||||||
username: payload.username.clone(),
|
username: payload.username.clone(),
|
||||||
password: payload.password.clone(),
|
password: payload.password.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let insert_sql = "INSERT INTO \"user\" (username, password) VALUES ($1, $2) RETURNING id";
|
match repo::user::insert(&pool, &user).await {
|
||||||
println!("SQL: {:?}", insert_sql);
|
Ok(id) => {
|
||||||
/*
|
println!("User inserted.");
|
||||||
sqlx::query(insert_sql)
|
user.id = id;
|
||||||
.bind(&user.username)
|
(StatusCode::CREATED, Json(response::Response{
|
||||||
.bind(&user.password)
|
message: String::from("User inserted"),
|
||||||
.execute(&pool)
|
data: user,
|
||||||
.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))
|
|
||||||
}
|
}
|
||||||
};
|
Err(err) => (StatusCode::BAD_REQUEST, Json(response::Response{
|
||||||
|
message: err.to_string(),
|
||||||
if id != uuid::Uuid::nil() {
|
data: user,
|
||||||
println!("User inserted.");
|
}))
|
||||||
user.id = id;
|
|
||||||
(StatusCode::CREATED, Json(user))
|
|
||||||
} else {
|
|
||||||
(StatusCode::BAD_REQUEST, Json(user))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@ pub mod callers;
|
|||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod hashing;
|
pub mod hashing;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
pub mod repo;
|
||||||
|
|
||||||
mod keys {
|
mod keys {
|
||||||
pub const DBURL: &str = "DATABASE_URL";
|
pub const DBURL: &str = "DATABASE_URL";
|
||||||
|
@@ -6,7 +6,7 @@ pub struct CreateUser {
|
|||||||
pub password: String,
|
pub password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub username: String,
|
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