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/models/model.rs | |
| parent | f1b9eb9e54bc264c235a4d77a18ad2f3c943a6d1 (diff) | |
Get user's models
Diffstat (limited to 'src/models/model.rs')
| -rw-r--r-- | src/models/model.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/models/model.rs b/src/models/model.rs index 0a9ea0a..60a6814 100644 --- a/src/models/model.rs +++ b/src/models/model.rs @@ -198,6 +198,32 @@ impl Model { Ok(rows) } + /// List author's models + pub async fn list_from_author(page: i64, author: i32) -> Result<Vec<ModelUser>, AppError> { + let pool = unsafe { get_client() }; + let rows: Vec<ModelUser> = sqlx::query_as( + r#" + SELECT + models.*, + json_build_object('id', users.id, 'email', users.email, 'username', users.username, 'is_staff', users.is_staff, 'avatar', users.avatar) as author, + json_agg(uploads.*) filter (where uploads.* is not null) as uploads + FROM models + JOIN users ON users.id = models.author_id + LEFT JOIN uploads ON uploads.model_id = models.id + WHERE author_id = $1 + GROUP BY models.id, users.id + ORDER BY id DESC + LIMIT $2 OFFSET $3 + "#) + .bind(author) + .bind(CONFIG.page_limit) + .bind(CONFIG.page_limit * page) + .fetch_all(pool) + .await?; + + Ok(rows) + } + /// Delete a model pub async fn delete(model_id: i32) -> Result<(), AppError> { let pool = unsafe { get_client() }; @@ -224,6 +250,18 @@ impl Model { let count: i64 = cursor.try_get(0).unwrap(); Ok(count) } + + /// Return the number of author models + pub async fn count_filter_by_author(author: i32) -> Result<i64, AppError> { + let pool = unsafe { get_client() }; + let cursor = sqlx::query(r#"SELECT COUNT(id) as count FROM models WHERE author_id = $1"#) + .bind(author) + .fetch_one(pool) + .await?; + + let count: i64 = cursor.try_get(0).unwrap(); + Ok(count) + } } impl ModelUser { |
