diff options
| author | mrw1593 <botahamec@outlook.com> | 2023-05-13 08:51:12 -0400 |
|---|---|---|
| committer | mrw1593 <botahamec@outlook.com> | 2023-05-29 10:45:49 -0400 |
| commit | d08c9f5e2a6e7327dfc0be60a315c4eab9f8458d (patch) | |
| tree | 114266074df93e2e2fdca7cdf400e9561dad405c /src/services/db.rs | |
| parent | 3aff5f0123778aa453ab5396cb880a83267ae4ee (diff) | |
Allow PUTs to individual fields
Diffstat (limited to 'src/services/db.rs')
| -rw-r--r-- | src/services/db.rs | 43 |
1 files changed, 40 insertions, 3 deletions
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<MySqlPool, RawUnexpected> { 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<Option<String>, RawUnexpected> { +) -> Result<Option<Box<str>>, 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<MySqlQueryResult, sqlx::Error> { @@ -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<MySqlQueryResult, sqlx::Error> { + 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<MySqlQueryResult, sqlx::Error> { + 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 +} |
