2024-09-27 23:17:33 -04:00
|
|
|
use super::posts::serialize_datetime;
|
2024-09-24 20:26:15 -04:00
|
|
|
use crate::{datasources::comments::CommentsDatasource, AppState};
|
|
|
|
use axum::{
|
2024-09-28 01:48:58 -04:00
|
|
|
extract::{Path, State},
|
2024-09-27 23:17:33 -04:00
|
|
|
http::StatusCode,
|
|
|
|
response::IntoResponse,
|
2024-09-24 20:26:15 -04:00
|
|
|
routing::{get, post},
|
|
|
|
Json,
|
|
|
|
};
|
2024-09-27 23:17:33 -04:00
|
|
|
use chrono::Utc;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use sqlx::{Pool, Postgres};
|
2024-09-24 20:26:15 -04:00
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
2024-09-27 23:17:33 -04:00
|
|
|
pub struct CommentInputPayload {
|
|
|
|
pub name: String,
|
|
|
|
pub body: String,
|
|
|
|
pub post_id: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct CommentPathParams {
|
|
|
|
id: i32,
|
|
|
|
}
|
|
|
|
|
2024-09-28 01:48:58 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Pagination {
|
|
|
|
pub page_number: i64,
|
|
|
|
pub page_size: i64,
|
|
|
|
}
|
|
|
|
|
2024-09-27 23:17:33 -04:00
|
|
|
#[derive(sqlx::FromRow, Serialize, Debug)]
|
|
|
|
pub struct Comment {
|
|
|
|
pub comment_id: i32,
|
|
|
|
pub name: String,
|
|
|
|
pub body: String,
|
|
|
|
#[serde(serialize_with = "serialize_datetime")]
|
|
|
|
pub created_at: Option<chrono::DateTime<Utc>>,
|
2024-09-24 20:26:15 -04:00
|
|
|
}
|
|
|
|
|
2024-09-22 22:38:35 -04:00
|
|
|
pub struct CommentsRoute;
|
2024-09-24 20:26:15 -04:00
|
|
|
impl CommentsRoute {
|
|
|
|
pub fn routes(app_state: &AppState) -> axum::Router {
|
|
|
|
// add more comment routes here!
|
|
|
|
axum::Router::new()
|
2024-09-27 23:17:33 -04:00
|
|
|
.route("/post/:id", get(CommentsRoute::get_post_comments))
|
|
|
|
.route("/add", post(CommentsRoute::insert_comment))
|
2024-09-28 01:48:58 -04:00
|
|
|
.route("/index", get(CommentsRoute::get_comments_index))
|
2024-09-25 18:29:12 -04:00
|
|
|
.with_state(app_state.db.clone())
|
2024-09-24 20:26:15 -04:00
|
|
|
}
|
|
|
|
|
2024-09-27 23:17:33 -04:00
|
|
|
async fn get_post_comments(
|
|
|
|
State(pool): State<Pool<Postgres>>,
|
|
|
|
Path(params): Path<CommentPathParams>,
|
|
|
|
) -> impl IntoResponse {
|
|
|
|
match CommentsDatasource::get_posts_comments(&pool, params.id).await {
|
|
|
|
Ok(c) => Ok(Json(c)),
|
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
}
|
|
|
|
}
|
2024-09-25 18:29:12 -04:00
|
|
|
//
|
2024-09-27 23:17:33 -04:00
|
|
|
async fn insert_comment(
|
|
|
|
State(pool): State<Pool<Postgres>>,
|
2024-09-28 01:48:58 -04:00
|
|
|
Json(input): Json<CommentInputPayload>,
|
2024-09-27 23:17:33 -04:00
|
|
|
) -> impl IntoResponse {
|
2024-09-28 01:48:58 -04:00
|
|
|
match CommentsDatasource::insert_comment(&pool, input).await {
|
2024-09-27 23:17:33 -04:00
|
|
|
Ok(c) => Ok((StatusCode::CREATED, Json(c))),
|
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 01:48:58 -04:00
|
|
|
|
|
|
|
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())),
|
|
|
|
}
|
|
|
|
}
|
2024-09-24 20:26:15 -04:00
|
|
|
}
|