Add find_user_by_id function

This commit is contained in:
Ethan Girouard 2024-04-02 01:55:00 -04:00
parent d6eb05514e
commit 5d69705239
Signed by: eta357
GPG Key ID: 7BCDC36DFD11C146

View File

@ -39,6 +39,21 @@ pub async fn find_user(username_or_email: String) -> Result<Option<User>, Server
Ok(user)
}
/// Get a user from the database by ID
/// Returns a Result with the user if found, None if not found, or an error if there was a problem
#[cfg(feature = "ssr")]
pub async fn find_user_by_id(user_id: i32) -> Result<Option<User>, ServerFnError> {
use crate::schema::users::dsl::*;
use leptos::server_fn::error::NoCustomError;
let db_con = &mut get_db_conn();
let user = users.filter(id.eq(user_id))
.first::<User>(db_con).optional()
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Error getting user from database: {}", e)))?;
Ok(user)
}
/// Create a new user in the database
/// Returns an empty Result if successful, or an error if there was a problem
#[cfg(feature = "ssr")]