diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2022-09-10 09:11:35 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2022-09-10 09:11:35 +0000 |
| commit | 5a43e5e38bea77d63074a8db9a319e3ff77fd75a (patch) | |
| tree | 2906b8c3292f126ddef9e7801f054b7294f97820 /src/routes/model.rs | |
| parent | e8c799cb65f58c1e9a4b2809489ef3b5760638d8 (diff) | |
Add pagination
Diffstat (limited to 'src/routes/model.rs')
| -rw-r--r-- | src/routes/model.rs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/routes/model.rs b/src/routes/model.rs index a5cd2b3..4ac2688 100644 --- a/src/routes/model.rs +++ b/src/routes/model.rs @@ -3,18 +3,28 @@ use crate::models::{ auth::Claims, model::{Model, ModelCreate, ModelUser}, }; -use axum::{routing::get, Json, Router}; +use crate::pagination::Pagination; +use axum::{extract::Query, routing::get, Json, Router}; +use serde::Serialize; /// Create routes for `/v1/models/` namespace pub fn create_route() -> Router { Router::new().route("/", get(list_models).post(create_model)) } +#[derive(Serialize)] +struct ModelPagination { + count: i64, + results: Vec<ModelUser>, +} + /// List models. -async fn list_models() -> Result<Json<Vec<ModelUser>>, AppError> { - let models = Model::list().await?; +async fn list_models(pagination: Query<Pagination>) -> Result<Json<ModelPagination>, AppError> { + let page = pagination.0.page.unwrap_or_default(); + let results = Model::list(page).await?; + let count = Model::count().await?; - Ok(Json(models)) + Ok(Json(ModelPagination { count, results })) } /// Create a model. Checks Authorization token |
