added comment index pagination, added author routes

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

View File

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