From 5d69705239283089bd27b4a344011eb681d9d0dd Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Tue, 2 Apr 2024 01:55:00 -0400 Subject: [PATCH] Add find_user_by_id function --- src/users.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/users.rs b/src/users.rs index e8084e2..2d61c69 100644 --- a/src/users.rs +++ b/src/users.rs @@ -39,6 +39,21 @@ pub async fn find_user(username_or_email: String) -> Result, 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, 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::(db_con).optional() + .map_err(|e| ServerFnError::::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")]