added pagination to a given authors page

This commit is contained in:
2025-07-07 21:05:27 -04:00
parent a64b8fdceb
commit 6694f47d70
16 changed files with 350 additions and 93 deletions

View File

@@ -1,6 +1,9 @@
use sqlx::{Pool, Postgres};
use crate::routes::{authors::Author, comments::Pagination, posts::Post};
use crate::{
routes::{authors::Author, posts::Post},
utils::pagination::Pagination,
};
pub struct AuthorsDatasource;
impl AuthorsDatasource {
@@ -8,11 +11,11 @@ impl AuthorsDatasource {
pool: &Pool<Postgres>,
pagination: Pagination,
) -> Result<Vec<Author>, sqlx::Error> {
let offset: i64 = (pagination.page_number - 1) * pagination.page_size;
let offset: i64 = (pagination.page - 1) * pagination.limit;
sqlx::query_as!(
Author,
"SELECT author_id, first_name, last_name, bio, image FROM authors ORDER BY created_at DESC LIMIT $1 OFFSET $2",
pagination.page_size,
pagination.page,
offset,
)
.fetch_all(pool)
@@ -32,13 +35,32 @@ impl AuthorsDatasource {
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, a.author_id 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",
pagination: Pagination,
) -> Result<(Vec<Post>, i64), sqlx::Error> {
let offset: i64 = (pagination.page - 1) * pagination.limit;
println!(
"Author ID: {}, Page: {}, Size: {}, Offset: {}",
author_id, pagination.page, pagination.limit, offset
);
let total_count = sqlx::query_scalar!(
"SELECT COUNT(*) FROM posts p WHERE p.deleted_at IS NULL AND p.author_id = $1",
author_id
)
.fetch_one(pool)
.await?
.unwrap_or(0);
let posts_query = sqlx::query_as!(
Post,
"SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.created_at, a.author_id 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 LIMIT $2 OFFSET $3",
author_id,
pagination.limit,
offset,
)
.fetch_all(pool)
.await
.await?;
Ok((posts_query, total_count))
}
}

View File

@@ -1,4 +1,7 @@
use crate::routes::comments::{Comment, CommentInputPayload, Pagination};
use crate::{
routes::comments::{Comment, CommentInputPayload},
utils::pagination::Pagination,
};
use sqlx::{Pool, Postgres};
pub struct CommentsDatasource;
@@ -25,8 +28,8 @@ impl CommentsDatasource {
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)
let offset: i64 = (pagination.page - 1) * pagination.limit;
sqlx::query_as!(Comment, "SELECT comment_id, name, body, created_at FROM comments ORDER BY created_at DESC LIMIT $1 OFFSET $2", pagination.page, offset)
.fetch_all(pool)
.await
}

View File

@@ -121,20 +121,11 @@ async fn main() {
// build our application with some routes
let app = Router::new()
.nest("/", routes::root::RootRoute::routes())
.nest("/posts", routes::posts::PostsRoute::routes(&app_state))
.nest(
"/comments",
routes::comments::CommentsRoute::routes(&app_state),
)
.nest(
"/authors",
routes::authors::AuthorsRoute::routes(&app_state),
)
.nest(
"/projects",
routes::projects::ProjectsRoute::routes(&app_state),
)
.merge(routes::root::RootRoute::routes())
.merge(routes::posts::PostsRoute::routes(&app_state))
.merge(routes::comments::CommentsRoute::routes(&app_state))
.merge(routes::authors::AuthorsRoute::routes(&app_state))
.merge(routes::projects::ProjectsRoute::routes(&app_state))
.layer(CorsLayer::permissive())
.layer(
TraceLayer::new_for_http()
@@ -179,3 +170,5 @@ async fn shutdown_signal() {
_ = terminate => {},
}
}

View File

@@ -1,5 +1,5 @@
use axum::{
extract::{Path, State},
extract::{Path, Query, State},
http::StatusCode,
response::IntoResponse,
routing::get,
@@ -8,9 +8,12 @@ use axum::{
use fred::types::Expiration;
use serde::{Deserialize, Serialize};
use crate::{datasources::authors::AuthorsDatasource, state::AppState};
use super::comments::Pagination;
use crate::{
datasources::authors::AuthorsDatasource,
routes::posts::Post,
state::AppState,
utils::pagination::{Pagination, PaginationQuery},
};
#[derive(Deserialize, Serialize, Clone)]
pub struct Author {
@@ -26,20 +29,31 @@ pub struct AuthorGetOneParams {
pub id: i32,
}
#[derive(Deserialize, Serialize)]
pub struct AuthorPostsResponse {
posts: Vec<Post>,
total_posts: i64,
}
pub struct 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))
.route("/:id/posts", get(AuthorsRoute::get_authors_posts))
.route("/authors", get(AuthorsRoute::get_all))
.route("/authors/{id}", get(AuthorsRoute::get_one))
.route("/authors/{id}/posts", get(AuthorsRoute::get_authors_posts))
.with_state(app_state.clone())
}
async fn get_all(
State(app_state): State<AppState>,
Json(pagination): Json<Pagination>,
Query(query): Query<PaginationQuery>,
) -> impl IntoResponse {
let pagination = Pagination {
page: query.page.unwrap_or(1),
limit: query.limit.unwrap_or(12),
};
let mut state = app_state.lock().await;
let cached: Option<Vec<Author>> = state
.cache
@@ -104,6 +118,7 @@ impl AuthorsRoute {
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
@@ -127,12 +142,20 @@ impl AuthorsRoute {
async fn get_authors_posts(
State(app_state): State<AppState>,
Path(params): Path<AuthorGetOneParams>,
Query(pagination): Query<PaginationQuery>,
) -> impl IntoResponse {
let pagination = Pagination {
page: pagination.page.unwrap_or(1),
limit: pagination.limit.unwrap_or(12),
};
let state = app_state.lock().await;
match AuthorsDatasource::get_authors_posts(&state.database, params.id).await {
Ok(p) => Ok(Json(p)),
match AuthorsDatasource::get_authors_posts(&state.database, params.id, pagination).await {
Ok((posts, total_posts)) => Ok(Json(AuthorPostsResponse { posts, total_posts })),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
}

View File

@@ -1,6 +1,13 @@
use crate::{datasources::comments::CommentsDatasource, state::AppState, utils::datetime::*};
use crate::{
datasources::comments::CommentsDatasource,
state::AppState,
utils::{
datetime::*,
pagination::{Pagination, PaginationQuery},
},
};
use axum::{
extract::{Path, State},
extract::{Path, Query, State},
http::StatusCode,
response::IntoResponse,
routing::{get, post},
@@ -21,13 +28,6 @@ pub struct CommentInputPayload {
pub struct CommentPathParams {
id: i32,
}
#[derive(Deserialize, Serialize)]
pub struct Pagination {
pub page_number: i64,
pub page_size: i64,
}
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct Comment {
pub comment_id: i32,
@@ -43,9 +43,9 @@ impl CommentsRoute {
pub fn routes(app_state: &AppState) -> axum::Router {
// add more comment routes here!
axum::Router::new()
.route("/post/:id", get(CommentsRoute::get_post_comments))
.route("/add", post(CommentsRoute::insert_comment))
.route("/index", get(CommentsRoute::get_comments_index))
.route("/comments/post/{id}", get(CommentsRoute::get_post_comments))
.route("/comments/add", post(CommentsRoute::insert_comment))
.route("/comments/index", get(CommentsRoute::get_comments_index))
.with_state(app_state.clone())
}
@@ -96,8 +96,13 @@ impl CommentsRoute {
async fn get_comments_index(
State(app_state): State<AppState>,
Json(pagination): Json<Pagination>,
Query(query): Query<PaginationQuery>,
) -> impl IntoResponse {
let pagination = Pagination {
page: query.page.unwrap_or(1),
limit: query.limit.unwrap_or(12),
};
let state = app_state.lock().await;
match CommentsDatasource::get_index_comments(&state.database, pagination).await {
@@ -106,3 +111,5 @@ impl CommentsRoute {
}
}
}

View File

@@ -57,14 +57,14 @@ impl PostsRoute {
pub fn routes(app_state: &AppState) -> Router {
// add more post routes here!
Router::new()
.route("/all", get(PostsRoute::get_all))
.route("/:id", get(PostsRoute::get_one))
.route("/recent", get(PostsRoute::get_recent_posts))
.route("/popular", get(PostsRoute::get_popular_posts))
.route("/hot", get(PostsRoute::get_hot_posts))
.route("/featured", get(PostsRoute::get_featured_posts))
.route("/rss", get(PostsRoute::get_rss_posts))
.route("/sitemap", get(PostsRoute::get_sitemap))
.route("/posts/all", get(PostsRoute::get_all))
.route("/posts/{id}", get(PostsRoute::get_one))
.route("/posts/recent", get(PostsRoute::get_recent_posts))
.route("/posts/popular", get(PostsRoute::get_popular_posts))
.route("/posts/hot", get(PostsRoute::get_hot_posts))
.route("/posts/featured", get(PostsRoute::get_featured_posts))
.route("/posts/rss", get(PostsRoute::get_rss_posts))
.route("/posts/sitemap", get(PostsRoute::get_sitemap))
.with_state(app_state.clone())
}
@@ -399,3 +399,5 @@ impl PostsRoute {
}
}
}

View File

@@ -1,5 +1,4 @@
use crate::{datasources::projects::ProjectsDatasource, state::AppState, utils::datetime::*};
use axum::http::{HeaderMap, HeaderValue};
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
use fred::types::Expiration;
use serde::{Deserialize, Serialize};
@@ -21,7 +20,7 @@ pub struct ProjectsRoute;
impl ProjectsRoute {
pub fn routes(app_state: &AppState) -> Router {
Router::new()
.route("/", get(ProjectsRoute::get_all))
.route("/projects", get(ProjectsRoute::get_all))
.with_state(app_state.clone())
}
@@ -67,3 +66,5 @@ impl ProjectsRoute {
}
}
}

View File

@@ -1,15 +1,10 @@
use axum::{
extract::State,
http::StatusCode,
response::{Html, IntoResponse},
routing::get,
Router,
};
use crate::{datasources::posts::PostsDatasource, state::AppState};
use super::posts::Post;
pub struct RootRoute;
impl RootRoute {
pub fn routes() -> Router {

View File

@@ -1,3 +1,4 @@
pub mod datetime;
pub mod pagination;
pub mod rss;
pub mod sitemap;

View File

@@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct PaginationQuery {
pub page: Option<i64>,
pub limit: Option<i64>,
}
#[derive(Deserialize, Serialize)]
pub struct Pagination {
pub page: i64,
pub limit: i64,
}