From d08c9f5e2a6e7327dfc0be60a315c4eab9f8458d Mon Sep 17 00:00:00 2001 From: mrw1593 Date: Sat, 13 May 2023 08:51:12 -0400 Subject: Allow PUTs to individual fields --- src/services/db.rs | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'src/services/db.rs') diff --git a/src/services/db.rs b/src/services/db.rs index b508e1b..a6571b5 100644 --- a/src/services/db.rs +++ b/src/services/db.rs @@ -4,6 +4,8 @@ use uuid::Uuid; use crate::models::User; +use super::crypto::PasswordHash; + /// Intialize the connection pool pub async fn initialize(db: &str, user: &str, password: &str) -> Result { let url = format!("mysql://{user}:{password}@localhost/{db}"); @@ -41,10 +43,11 @@ pub async fn username_is_used<'c>( pub async fn get_username<'c>( conn: impl Executor<'c, Database = MySql>, user_id: Uuid, -) -> Result, RawUnexpected> { +) -> Result>, RawUnexpected> { let username = query_scalar!(r"SELECT username FROM users where user_id = ?", user_id) .fetch_optional(conn) - .await?; + .await? + .map(String::into_boxed_str); Ok(username) } @@ -66,7 +69,7 @@ pub async fn new_user<'c>( .await } -pub async fn update_username<'c>( +pub async fn update_user<'c>( conn: impl Executor<'c, Database = MySql>, user: &User, ) -> Result { @@ -86,3 +89,37 @@ pub async fn update_username<'c>( .execute(conn) .await } + +pub async fn update_username<'c>( + conn: impl Executor<'c, Database = MySql>, + user_id: Uuid, + username: &str, +) -> Result { + query!( + r"UPDATE users SET username = ? WHERE user_id = ?", + username, + user_id + ) + .execute(conn) + .await +} + +pub async fn update_password<'c>( + conn: impl Executor<'c, Database = MySql>, + user_id: Uuid, + password: &PasswordHash, +) -> Result { + query!( + r"UPDATE users SET + password_hash = ?, + password_salt = ?, + password_version = ? + WHERE user_id = ?", + password.hash(), + password.salt(), + password.version(), + user_id + ) + .execute(conn) + .await +} -- cgit v1.2.3