added get authors posts route, datasource
This commit is contained in:
parent
c508f78043
commit
4480b87cab
@ -1,6 +1,6 @@
|
|||||||
use sqlx::{Pool, Postgres};
|
use sqlx::{Pool, Postgres};
|
||||||
|
|
||||||
use crate::routes::{authors::Author, comments::Pagination};
|
use crate::routes::{authors::Author, comments::Pagination, posts::Post};
|
||||||
|
|
||||||
pub struct AuthorsDatasource;
|
pub struct AuthorsDatasource;
|
||||||
impl AuthorsDatasource {
|
impl AuthorsDatasource {
|
||||||
@ -28,4 +28,17 @@ impl AuthorsDatasource {
|
|||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
.await
|
.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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ impl AuthorsRoute {
|
|||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
.route("/", get(AuthorsRoute::get_all))
|
.route("/", get(AuthorsRoute::get_all))
|
||||||
.route("/:id", get(AuthorsRoute::get_one))
|
.route("/:id", get(AuthorsRoute::get_one))
|
||||||
|
.route("/:id/posts", get(AuthorsRoute::get_authors_posts))
|
||||||
.with_state(app_state.db.clone())
|
.with_state(app_state.db.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,4 +54,14 @@ impl AuthorsRoute {
|
|||||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_authors_posts(
|
||||||
|
State(pool): State<Pool<Postgres>>,
|
||||||
|
Path(params): Path<AuthorGetOneParams>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
match AuthorsDatasource::get_authors_posts(&pool, params.id).await {
|
||||||
|
Ok(p) => Ok(Json(p)),
|
||||||
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user