added get authors posts route, datasource
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::routes::{authors::Author, comments::Pagination};
|
||||
use crate::routes::{authors::Author, comments::Pagination, posts::Post};
|
||||
|
||||
pub struct AuthorsDatasource;
|
||||
impl AuthorsDatasource {
|
||||
@@ -28,4 +28,17 @@ impl AuthorsDatasource {
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_authors_posts(
|
||||
pool: &Pool<Postgres>,
|
||||
author_id: i32,
|
||||
) -> Result<Vec<Post>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Post,
|
||||
"SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.author_id = $1 ORDER BY created_at DESC",
|
||||
author_id
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user