Comment count API endpoint

This commit is contained in:
Imbus 2024-03-23 00:23:29 +01:00
parent 5eeed1e8bc
commit 85c2161a4d
4 changed files with 56 additions and 2 deletions

View file

@ -4,7 +4,7 @@ use crate::types::{CommentQueryParams, NewComment};
use crate::ServerState;
use actix_web::get;
use actix_web::web::{Data, Query};
use actix_web::web::{Data, Path, Query};
use actix_web::{post, web::Json, HttpResponse, Responder, Result};
use log::info;
@ -78,3 +78,22 @@ pub async fn new_comment(
}
}
}
#[get("/posts/{id}/comments/count")]
pub async fn get_comment_count(
path: Path<i64>,
state: Data<ServerState>,
) -> Result<impl Responder> {
let post_id = path.into_inner();
let count = sqlx::query!(
"SELECT COUNT(*) FROM comments WHERE parent_post_id = $1",
post_id
)
.fetch_one(&state.pool)
.await
.unwrap()
.count;
return Ok(HttpResponse::Ok().json(count));
}