diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2022-09-24 16:10:08 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2022-09-24 16:10:08 +0000 |
| commit | 357b9e646df220575df37fe1d6705a9667210997 (patch) | |
| tree | 76f415c19f636a2899dd469745baf21fbff8b808 /src/routes/user.rs | |
| parent | f1b9eb9e54bc264c235a4d77a18ad2f3c943a6d1 (diff) | |
Get user's models
Diffstat (limited to 'src/routes/user.rs')
| -rw-r--r-- | src/routes/user.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/routes/user.rs b/src/routes/user.rs index 4904531..172007a 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -6,6 +6,7 @@ use crate::{ user::{User, UserList}, }, pagination::Pagination, + routes::model::ModelPagination, }; use axum::{ extract::{ContentLengthLimit, Multipart, Path, Query}, @@ -21,6 +22,7 @@ pub fn create_route() -> Router { .route("/me", get(get_me)) .route("/me/avatar", put(edit_my_avatar).delete(delete_my_avatar)) .route("/:id", get(get_user)) + .route("/:id/models", get(get_user_models)) } #[derive(Serialize)] @@ -108,3 +110,22 @@ async fn get_user(Path(user_id): Path<i32>) -> Result<Json<UserList>, AppError> Err(_) => Err(AppError::NotFound("User not found".to_string())), } } + +/// Get user models list +async fn get_user_models( + Path(user_id): Path<i32>, + pagination: Query<Pagination>, +) -> Result<Json<ModelPagination>, AppError> { + let user = match User::find_by_id(user_id).await { + Ok(user) => user, + Err(_) => { + return Err(AppError::NotFound("User not found".to_string())); + } + }; + + let page = pagination.0.page.unwrap_or_default(); + let results = user.get_models(page).await?; + let count = user.count_models().await?; + + Ok(Json(ModelPagination { count, results })) +} |
