From 85c2161a4d5685ef7b5258816bc7e1352e867e73 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sat, 23 Mar 2024 00:23:29 +0100 Subject: [PATCH] Comment count API endpoint --- client-solid/src/Util/api.ts | 11 ++++++++++ ...98e403c0638293c845aec61ab662d555180b2.json | 22 +++++++++++++++++++ server/src/main.rs | 4 +++- server/src/routes/comment.rs | 21 +++++++++++++++++- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 server/.sqlx/query-8543bcd80753991e922e6525c4d98e403c0638293c845aec61ab662d555180b2.json diff --git a/client-solid/src/Util/api.ts b/client-solid/src/Util/api.ts index 8cbd464..4f1c208 100644 --- a/client-solid/src/Util/api.ts +++ b/client-solid/src/Util/api.ts @@ -95,6 +95,17 @@ export async function getComments( return data; } +/** + * Gets the total amount of comments for a post + * @param postId The id of the post + * @returns {Promise} A promise that contains the number of comments + */ +export async function getCommentCount(postId: string): Promise { + const res = await fetch(`/api/posts/${postId}/comments/count`); + const data = await res.json(); + return data; +} + /** * Gets the Engagement counts for a post by postId * @param postId The id of the post diff --git a/server/.sqlx/query-8543bcd80753991e922e6525c4d98e403c0638293c845aec61ab662d555180b2.json b/server/.sqlx/query-8543bcd80753991e922e6525c4d98e403c0638293c845aec61ab662d555180b2.json new file mode 100644 index 0000000..4e5f932 --- /dev/null +++ b/server/.sqlx/query-8543bcd80753991e922e6525c4d98e403c0638293c845aec61ab662d555180b2.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT COUNT(*) FROM comments WHERE parent_post_id = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "count", + "type_info": "Int8" + } + ], + "parameters": { + "Left": [ + "Int8" + ] + }, + "nullable": [ + null + ] + }, + "hash": "8543bcd80753991e922e6525c4d98e403c0638293c845aec61ab662d555180b2" +} diff --git a/server/src/main.rs b/server/src/main.rs index 1b3a1d8..476c4be 100755 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -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) diff --git a/server/src/routes/comment.rs b/server/src/routes/comment.rs index acd8cf7..15cb616 100644 --- a/server/src/routes/comment.rs +++ b/server/src/routes/comment.rs @@ -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, + state: Data, +) -> Result { + 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)); +}