From d9ad76b5830f3b1611e500bc76fb2ee96fa8a259 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 7 Jun 2026 18:07:22 -0400 Subject: [PATCH] Saving code --- src/callers/mod.rs | 4 ++-- src/callers/register.rs | 30 +++++++++++++++++------------- src/repo/mod.rs | 25 +++++++++++++++++++++---- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/callers/mod.rs b/src/callers/mod.rs index 33ddec1..ab14499 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -3,6 +3,6 @@ pub mod register; pub mod endpoints { pub const ROOT: &str = "/"; - pub const REGISTER: &str = "/api/v2/register"; - pub const DBTEST: &str = "/api/v2/test/db"; + pub const REGISTER: &str = "/api/v1/register"; + pub const DBTEST: &str = "/api/v1/test/db"; } diff --git a/src/callers/register.rs b/src/callers/register.rs index b435543..c59cfbc 100644 --- a/src/callers/register.rs +++ b/src/callers/register.rs @@ -8,18 +8,15 @@ pub mod request { #[derive(Default, Deserialize, Serialize, utoipa::ToSchema)] pub struct Request { - #[serde(skip_serializing_if = "String::is_empty")] pub username: String, - #[serde(skip_serializing_if = "String::is_empty")] pub password: String, - #[serde(skip_serializing_if = "String::is_empty")] - pub email: String, - #[serde(skip_serializing_if = "String::is_empty")] - pub phone: String, - #[serde(skip_serializing_if = "String::is_empty")] - pub firstname: String, - #[serde(skip_serializing_if = "String::is_empty")] - pub lastname: String, + pub phone_number: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub email: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub firstname: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub lastname: Option, } } @@ -71,13 +68,20 @@ pub async fn register_user( username: payload.username.clone(), password: payload.password.clone(), // email: payload.email.clone(), - phone_number: payload.phone.clone(), - firstname: payload.firstname.clone(), - lastname: payload.lastname.clone(), + phone_number: payload.phone_number.clone(), // email_verified: true, ..Default::default() }; + user.firstname = match payload.firstname { + Some(firstname) => firstname, + None => String::new() + }; + user.lastname = match payload.lastname { + Some(lastname) => lastname, + None => String::new() + }; + match repo::user::exists(&pool, &user.username).await { Ok(res) => { if res { diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 1682239..398d217 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -15,7 +15,7 @@ pub mod user { ) -> Result { let result = sqlx::query( r#" - SELECT * FROM "user" WHERE username = $1 + SELECT id, username, password, phone_number, salt_id, firstname, lastname, created, last_login FROM "user" WHERE username = $1 "#, ) .bind(username) @@ -85,8 +85,25 @@ pub mod user { .await; match result { - Ok(r) => Ok(r.is_some()), - Err(e) => Err(e), + Ok(r) => { + match r { + Some(row) => { + if row.is_empty() { + Ok(false) + } else { + Ok(true) + } + } + None => { + Ok(false) + } + } + }, + Err(e) => { + eprintln!("What??"); + eprintln!("Error: {e:?}"); + Err(e) + } } } @@ -96,7 +113,7 @@ pub mod user { ) -> Result<(uuid::Uuid, std::option::Option), sqlx::Error> { let row = sqlx::query( r#" - INSERT INTO "user" (username, password, phone, firstname, lastname, salt_id) + INSERT INTO "user" (username, password, phone_number, firstname, lastname, salt_id) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, created; "#,