added comment index pagination, added author routes

This commit is contained in:
Wyatt J. Miller 2024-09-28 01:48:58 -04:00
parent 8128459b4e
commit 65e058b3c1
4 changed files with 118 additions and 7 deletions

View File

@ -1,2 +1,31 @@
use sqlx::{Pool, Postgres};
use crate::routes::{authors::Author, comments::Pagination};
pub struct AuthorsDatasource; pub struct AuthorsDatasource;
impl AuthorsDatasource {} 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
}
}

View File

@ -1,4 +1,4 @@
use crate::routes::comments::{Comment, CommentInputPayload}; use crate::routes::comments::{Comment, CommentInputPayload, Pagination};
use sqlx::{Pool, Postgres}; use sqlx::{Pool, Postgres};
pub struct CommentsDatasource; pub struct CommentsDatasource;
@ -11,12 +11,23 @@ impl CommentsDatasource {
.fetch_all(pool) .fetch_all(pool)
.await .await
} }
pub async fn insert_comment( pub async fn insert_comment(
pool: &Pool<Postgres>, pool: &Pool<Postgres>,
comment_input: CommentInputPayload, comment_input: CommentInputPayload,
) -> Result<Comment, sqlx::Error> { ) -> 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) .fetch_one(pool)
.await .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
}
} }

View File

@ -1,2 +1,56 @@
use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::get,
Json,
};
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres};
use crate::{datasources::authors::AuthorsDatasource, AppState};
use super::comments::Pagination;
#[derive(Serialize)]
pub struct Author {
pub author_id: i32,
pub first_name: String,
pub last_name: String,
pub bio: Option<String>,
}
#[derive(Deserialize)]
pub struct AuthorGetOneParams {
pub id: i32,
}
pub struct AuthorsRoute; pub struct AuthorsRoute;
impl AuthorsRoute {} impl AuthorsRoute {
pub fn routes(app_state: &AppState) -> axum::Router {
axum::Router::new()
.route("/", get(AuthorsRoute::get_all))
.route("/:id", get(AuthorsRoute::get_one))
.with_state(app_state.db.clone())
}
async fn get_all(
State(pool): State<Pool<Postgres>>,
Json(pagination): Json<Pagination>,
) -> impl IntoResponse {
match AuthorsDatasource::get_all(&pool, pagination).await {
Ok(a) => Ok(Json(a)),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
async fn get_one(
State(pool): State<Pool<Postgres>>,
Path(params): Path<AuthorGetOneParams>,
) -> impl IntoResponse {
match AuthorsDatasource::get_one(&pool, params.id).await {
Ok(a) => Ok(Json(a)),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
}

View File

@ -1,7 +1,7 @@
use super::posts::serialize_datetime; use super::posts::serialize_datetime;
use crate::{datasources::comments::CommentsDatasource, AppState}; use crate::{datasources::comments::CommentsDatasource, AppState};
use axum::{ use axum::{
extract::{Form, Path, State}, extract::{Path, State},
http::StatusCode, http::StatusCode,
response::IntoResponse, response::IntoResponse,
routing::{get, post}, routing::{get, post},
@ -23,6 +23,12 @@ pub struct CommentPathParams {
id: i32, id: i32,
} }
#[derive(Deserialize)]
pub struct Pagination {
pub page_number: i64,
pub page_size: i64,
}
#[derive(sqlx::FromRow, Serialize, Debug)] #[derive(sqlx::FromRow, Serialize, Debug)]
pub struct Comment { pub struct Comment {
pub comment_id: i32, pub comment_id: i32,
@ -39,6 +45,7 @@ impl CommentsRoute {
axum::Router::new() axum::Router::new()
.route("/post/:id", get(CommentsRoute::get_post_comments)) .route("/post/:id", get(CommentsRoute::get_post_comments))
.route("/add", post(CommentsRoute::insert_comment)) .route("/add", post(CommentsRoute::insert_comment))
.route("/index", get(CommentsRoute::get_comments_index))
.with_state(app_state.db.clone()) .with_state(app_state.db.clone())
} }
@ -54,11 +61,21 @@ impl CommentsRoute {
// //
async fn insert_comment( async fn insert_comment(
State(pool): State<Pool<Postgres>>, State(pool): State<Pool<Postgres>>,
Json(comment_input): Json<CommentInputPayload>, Json(input): Json<CommentInputPayload>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match CommentsDatasource::insert_comment(&pool, comment_input).await { match CommentsDatasource::insert_comment(&pool, input).await {
Ok(c) => Ok((StatusCode::CREATED, Json(c))), Ok(c) => Ok((StatusCode::CREATED, Json(c))),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())), Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
} }
} }
async fn get_comments_index(
State(pool): State<Pool<Postgres>>,
Json(pagination): Json<Pagination>,
) -> impl IntoResponse {
match CommentsDatasource::get_index_comments(&pool, pagination).await {
Ok(c) => Ok(Json(c)),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
} }