diff --git a/crates/collab/src/api.rs b/crates/collab/src/api.rs index d1d4625c90..993e32e445 100644 --- a/crates/collab/src/api.rs +++ b/crates/collab/src/api.rs @@ -71,8 +71,25 @@ pub async fn validate_api_token(req: Request, next: Next) -> impl IntoR Ok::<_, Error>(next.run(req).await) } -async fn get_users(Extension(app): Extension>) -> Result>> { - let users = app.db.get_all_users().await?; +#[derive(Debug, Deserialize)] +struct GetUsersQueryParams { + query: Option, + page: Option, + limit: Option, +} + +async fn get_users( + Query(params): Query, + Extension(app): Extension>, +) -> Result>> { + let limit = params.limit.unwrap_or(100); + let users = if let Some(query) = params.query { + app.db.fuzzy_search_users(&query, limit).await? + } else { + app.db + .get_all_users(params.page.unwrap_or(0), limit) + .await? + }; Ok(Json(users)) } diff --git a/crates/collab/src/db.rs b/crates/collab/src/db.rs index 439c6985a8..c1d5506491 100644 --- a/crates/collab/src/db.rs +++ b/crates/collab/src/db.rs @@ -17,8 +17,8 @@ pub trait Db: Send + Sync { email_address: Option<&str>, admin: bool, ) -> Result; + async fn get_all_users(&self, page: u32, limit: u32) -> Result>; async fn create_users(&self, users: Vec<(String, String, usize)>) -> Result>; - async fn get_all_users(&self) -> Result>; async fn fuzzy_search_users(&self, query: &str, limit: u32) -> Result>; async fn get_user_by_id(&self, id: UserId) -> Result>; async fn get_users_by_ids(&self, ids: Vec) -> Result>; @@ -142,6 +142,15 @@ impl Db for PostgresDb { .map(UserId)?) } + async fn get_all_users(&self, page: u32, limit: u32) -> Result> { + let query = "SELECT * FROM users ORDER BY github_login ASC LIMIT $1 OFFSET $2"; + Ok(sqlx::query_as(query) + .bind(limit) + .bind(page * limit) + .fetch_all(&self.pool) + .await?) + } + async fn create_users(&self, users: Vec<(String, String, usize)>) -> Result> { let mut query = QueryBuilder::new( "INSERT INTO users (github_login, email_address, admin, invite_code, invite_count)", @@ -177,11 +186,6 @@ impl Db for PostgresDb { .collect()) } - async fn get_all_users(&self) -> Result> { - let query = "SELECT * FROM users ORDER BY github_login ASC"; - Ok(sqlx::query_as(query).fetch_all(&self.pool).await?) - } - async fn fuzzy_search_users(&self, name_query: &str, limit: u32) -> Result> { let like_string = fuzzy_like_string(name_query); let query = " @@ -1765,11 +1769,11 @@ pub mod tests { } } - async fn create_users(&self, _users: Vec<(String, String, usize)>) -> Result> { + async fn get_all_users(&self, _page: u32, _limit: u32) -> Result> { unimplemented!() } - async fn get_all_users(&self) -> Result> { + async fn create_users(&self, _users: Vec<(String, String, usize)>) -> Result> { unimplemented!() }