summaryrefslogtreecommitdiffstats
path: root/src/models/model.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-10 09:11:35 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-10 09:11:35 +0000
commit5a43e5e38bea77d63074a8db9a319e3ff77fd75a (patch)
tree2906b8c3292f126ddef9e7801f054b7294f97820 /src/models/model.rs
parente8c799cb65f58c1e9a4b2809489ef3b5760638d8 (diff)
Add pagination
Diffstat (limited to 'src/models/model.rs')
-rw-r--r--src/models/model.rs20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/models/model.rs b/src/models/model.rs
index e3fc7ac..a87cddb 100644
--- a/src/models/model.rs
+++ b/src/models/model.rs
@@ -1,4 +1,6 @@
+use crate::config::PAGE_LIMIT;
use crate::db::get_client;
+
use crate::errors::AppError;
use sqlx::types::JsonValue;
@@ -111,18 +113,32 @@ impl Model {
}
/// List all models
- pub async fn list() -> Result<Vec<ModelUser>, AppError> {
+ pub async fn list(page: i64) -> Result<Vec<ModelUser>, AppError> {
let pool = unsafe { get_client() };
let rows = sqlx::query_as!(
ModelUser,
r#"SELECT
models.*,
json_build_object('id', users.id, 'email', users.email, 'username', users.username, 'is_staff', users.is_staff) as author
- FROM models JOIN users ON users.id = models.author_id"#
+ FROM models JOIN users ON users.id = models.author_id
+ LIMIT $1 OFFSET $2
+ "#,
+ PAGE_LIMIT,
+ PAGE_LIMIT * page
)
.fetch_all(pool)
.await?;
Ok(rows)
}
+
+ /// Return the number of models.
+ pub async fn count() -> Result<i64, AppError> {
+ let pool = unsafe { get_client() };
+ let row = sqlx::query!(r#"SELECT COUNT(id) as count FROM models"#)
+ .fetch_one(pool)
+ .await?;
+
+ Ok(row.count.unwrap())
+ }
}