added comment index pagination, added author routes
This commit is contained in:
1 parent
8128459b4e
commit
65e058b3c1
4 files changed
+118
-7
No files matched your search
@@ -1,4 +1,4 @@
|
||||
use crate::routes::comments::{Comment, CommentInputPayload};
|
||||
use crate::routes::comments::{Comment, CommentInputPayload, Pagination};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
pub struct CommentsDatasource;
|
||||
@@ -11,12 +11,23 @@ impl CommentsDatasource {
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn insert_comment(
|
||||
pool: &Pool<Postgres>,
|
||||
comment_input: CommentInputPayload,
|
||||
) -> Result<Comment, sqlx::Error> {
|
||||
sqlx::query!("INSERT INTO comments (post_id, name, body) VALUES ($1, $2, $3) RETURNING comment_id, name, body, created_at", comment_input.post_id, comment_input.name, comment_input.body)
|
||||
sqlx::query_as!(Comment, "INSERT INTO comments (post_id, name, body) VALUES ($1, $2, $3) RETURNING comment_id, name, body, created_at", comment_input.post_id, comment_input.name, comment_input.body)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_index_comments(
|
||||
pool: &Pool<Postgres>,
|
||||
pagination: Pagination,
|
||||
) -> Result<Vec<Comment>, sqlx::Error> {
|
||||
let offset: i64 = (pagination.page_number - 1) * pagination.page_size;
|
||||
sqlx::query_as!(Comment, "SELECT comment_id, name, body, created_at FROM comments ORDER BY created_at DESC LIMIT $1 OFFSET $2", pagination.page_size, offset)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user