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 queryid
- Unique identifier of the user to updateemail
- Optional new email address for the useractive
- Optional new active status for the userstatus
- Optional new status (active, deactivated, suspended, pending)
§Returns
Ok(User)
- Successfully updated user recordErr(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.