added pagination to a given authors page
This commit is contained in:
@@ -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 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user