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 | |
| parent | f1b9eb9e54bc264c235a4d77a18ad2f3c943a6d1 (diff) | |
Get user's models
Diffstat (limited to 'src/models')
| -rw-r--r-- | src/models/model.rs | 38 | ||||
| -rw-r--r-- | src/models/user.rs | 19 |
2 files changed, 55 insertions, 2 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 { diff --git a/src/models/user.rs b/src/models/user.rs index c4debf5..7c08075 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -1,4 +1,9 @@ -use crate::{config::CONFIG, db::get_client, errors::AppError}; +use crate::{ + config::CONFIG, + db::get_client, + errors::AppError, + models::model::{Model, ModelUser}, +}; use serde::{Deserialize, Serialize}; use serde_with::{serde_as, NoneAsEmptyString}; @@ -170,7 +175,7 @@ impl User { } impl UserList { - // Edit an user + /// Edit an user pub async fn edit_avatar(&mut self, avatar: Option<String>) -> Result<(), AppError> { let pool = unsafe { get_client() }; sqlx::query( @@ -187,4 +192,14 @@ impl UserList { Ok(()) } + + /// Get all models created by an user + pub async fn get_models(&self, page: i64) -> Result<Vec<ModelUser>, AppError> { + Model::list_from_author(page, self.id).await + } + + /// Returns the number of models for an user + pub async fn count_models(&self) -> Result<i64, AppError> { + Model::count_filter_by_author(self.id).await + } } |
