Function update_user

Source
pub async fn update_user(
    pool: &Pool<MySql>,
    id: i64,
    email: Option<&str>,
    active: Option<bool>,
    status: Option<&str>,
) -> Result<User>
Expand description

Updates an existing user’s information.

This function modifies a user record with the provided information. It supports partial updates, allowing you to update only the fields that need changing.

§Arguments

  • pool - Database connection pool for executing the query
  • id - Unique identifier of the user to update
  • email - Optional new email address for the user
  • active - Optional new active status for the user
  • status - Optional new status (active, deactivated, suspended, pending)

§Returns

  • Ok(User) - Successfully updated user record
  • Err(anyhow::Error) - Failed to update user

§Dynamic Query Building

This function dynamically builds an SQL query based on which parameters are provided. Only the fields specified with Some values will be updated, while None values are ignored.

§Transaction Handling

This function uses a database transaction to ensure atomicity of the operation. If any part of the operation fails, the entire operation is rolled back.

§Note

This function does not support updating passwords. Password updates should be handled by a dedicated function with appropriate security measures.