Comment count API endpoint
This commit is contained in:
parent
5eeed1e8bc
commit
85c2161a4d
4 changed files with 56 additions and 2 deletions
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue