Comment count API endpoint
This commit is contained in:
parent
5eeed1e8bc
commit
85c2161a4d
4 changed files with 56 additions and 2 deletions
|
|
@ -17,9 +17,10 @@ use jwt::Authentication;
|
|||
use routes::{get_comments, get_posts, login, new_comment, new_post, post_by_id, register};
|
||||
use state::CaptchaState;
|
||||
use state::ServerState;
|
||||
#[allow(unused_imports)]
|
||||
use util::hex_string;
|
||||
|
||||
use crate::routes::{delete_post, engage_post, get_engagements};
|
||||
use crate::routes::{delete_post, engage_post, get_comment_count, get_engagements};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
|
|
@ -69,6 +70,7 @@ async fn main() -> std::io::Result<()> {
|
|||
.service(delete_post)
|
||||
.service(new_comment)
|
||||
.service(get_comments)
|
||||
.service(get_comment_count)
|
||||
.service(engage_post)
|
||||
.service(get_engagements)
|
||||
.service(post_by_id)
|
||||
|
|
|
|||
|
|
@ -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