Allow specifying query, limit and page when hitting /api/users

This is needed to introduce pagination and search in our admin panel.
This commit is contained in:
Antonio Scandurra 2022-06-13 17:30:01 +02:00
parent b1e8e81513
commit 49d7b4bc12
2 changed files with 28 additions and 7 deletions

View file

@ -70,8 +70,25 @@ pub async fn validate_api_token<B>(req: Request<B>, next: Next<B>) -> impl IntoR
Ok::<_, Error>(next.run(req).await)
}
async fn get_users(Extension(app): Extension<Arc<AppState>>) -> Result<Json<Vec<User>>> {
let users = app.db.get_all_users().await?;
#[derive(Debug, Deserialize)]
struct GetUsersQueryParams {
query: Option<String>,
page: Option<u32>,
limit: Option<u32>,
}
async fn get_users(
Query(params): Query<GetUsersQueryParams>,
Extension(app): Extension<Arc<AppState>>,
) -> Result<Json<Vec<User>>> {
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))
}