2024-09-28 01:48:58 -04:00
|
|
|
use sqlx::{Pool, Postgres};
|
|
|
|
|
|
|
|
use crate::routes::{authors::Author, comments::Pagination};
|
|
|
|
|
2024-09-22 22:38:35 -04:00
|
|
|
pub struct AuthorsDatasource;
|
2024-09-28 01:48:58 -04:00
|
|
|
impl AuthorsDatasource {
|
|
|
|
pub async fn get_all(
|
|
|
|
pool: &Pool<Postgres>,
|
|
|
|
pagination: Pagination,
|
|
|
|
) -> Result<Vec<Author>, sqlx::Error> {
|
|
|
|
let offset: i64 = (pagination.page_number - 1) * pagination.page_size;
|
|
|
|
sqlx::query_as!(
|
|
|
|
Author,
|
|
|
|
"SELECT author_id, first_name, last_name, bio FROM authors ORDER BY created_at DESC LIMIT $1 OFFSET $2",
|
|
|
|
pagination.page_size,
|
|
|
|
offset,
|
|
|
|
)
|
|
|
|
.fetch_all(pool)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_one(pool: &Pool<Postgres>, author_id: i32) -> Result<Author, sqlx::Error> {
|
|
|
|
sqlx::query_as!(
|
|
|
|
Author,
|
|
|
|
"SELECT author_id, first_name, last_name, bio FROM authors WHERE author_id = $1",
|
|
|
|
author_id
|
|
|
|
)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|