Comment count API endpoint
This commit is contained in:
parent
5eeed1e8bc
commit
85c2161a4d
4 changed files with 56 additions and 2 deletions
|
@ -95,6 +95,17 @@ export async function getComments(
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the total amount of comments for a post
|
||||||
|
* @param postId The id of the post
|
||||||
|
* @returns {Promise<number>} A promise that contains the number of comments
|
||||||
|
*/
|
||||||
|
export async function getCommentCount(postId: string): Promise<number> {
|
||||||
|
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
|
* Gets the Engagement counts for a post by postId
|
||||||
* @param postId The id of the post
|
* @param postId The id of the post
|
||||||
|
|
|
@ -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"
|
||||||
|
}
|
|
@ -17,9 +17,10 @@ use jwt::Authentication;
|
||||||
use routes::{get_comments, get_posts, login, new_comment, new_post, post_by_id, register};
|
use routes::{get_comments, get_posts, login, new_comment, new_post, post_by_id, register};
|
||||||
use state::CaptchaState;
|
use state::CaptchaState;
|
||||||
use state::ServerState;
|
use state::ServerState;
|
||||||
|
#[allow(unused_imports)]
|
||||||
use util::hex_string;
|
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]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
|
@ -69,6 +70,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.service(delete_post)
|
.service(delete_post)
|
||||||
.service(new_comment)
|
.service(new_comment)
|
||||||
.service(get_comments)
|
.service(get_comments)
|
||||||
|
.service(get_comment_count)
|
||||||
.service(engage_post)
|
.service(engage_post)
|
||||||
.service(get_engagements)
|
.service(get_engagements)
|
||||||
.service(post_by_id)
|
.service(post_by_id)
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::types::{CommentQueryParams, NewComment};
|
||||||
use crate::ServerState;
|
use crate::ServerState;
|
||||||
|
|
||||||
use actix_web::get;
|
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 actix_web::{post, web::Json, HttpResponse, Responder, Result};
|
||||||
use log::info;
|
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…
Reference in a new issue