Working comment route fully implemented

This commit is contained in:
Imbus 2024-03-06 08:05:23 +01:00
parent 32ff43e87c
commit d0e5b57006
3 changed files with 14 additions and 17 deletions

View file

@ -9,14 +9,13 @@ use sqlx::PgPool;
pub async fn db_new_comment(
pool: &PgPool,
parent_post_id: i64,
parent_comment_id: Option<i64>,
// parent_comment_id: Option<i64>,
user_id: i64,
content: &str,
) -> bool {
let insert_query = sqlx::query!(
"INSERT INTO comments (parent_post_id, parent_comment_id, author_user_id, content) VALUES ($1, $2, $3, $4)",
"INSERT INTO comments (parent_post_id, author_user_id, content) VALUES ($1, $2, $3)",
parent_post_id,
parent_comment_id,
user_id,
content
)
@ -40,7 +39,8 @@ pub async fn db_get_comments(
) -> Vec<PublicComment> {
sqlx::query_as!(
PublicComment,
"SELECT id, parent_post_id, parent_comment_id, upvotes, downvotes, content, created_at, updated_at FROM comments WHERE parent_post_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3",
"SELECT id, parent_post_id, upvotes, downvotes, content, created_at, updated_at
FROM comments WHERE parent_post_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3",
parent_post_id,
limit,
offset

View file

@ -24,6 +24,11 @@ pub async fn get_comments(
let comments = db_get_comments(&state.pool, post_id, limit, offset).await;
if comments.is_empty() {
info!("No comments found for post {}", post_id);
return Ok(HttpResponse::NotFound().json("No comments found"));
}
Ok(HttpResponse::Ok().json(comments))
}
@ -60,14 +65,7 @@ pub async fn new_comment(
info!("Creating a new comment {:?}", &data);
let success = db_new_comment(
&state.pool,
data.parent_post_id,
data.parent_comment_id,
userid,
&content,
)
.await;
let success = db_new_comment(&state.pool, data.parent_post_id, userid, &content).await;
match success {
true => {

View file

@ -16,8 +16,8 @@ pub struct Comment {
pub parent_post_id: i64,
pub parent_comment_id: Option<i64>,
pub author_user_id: i64,
pub upvotes: i64,
pub downvotes: i64,
pub upvotes: i32,
pub downvotes: i32,
pub content: String,
pub created_at: chrono::NaiveDateTime,
pub updated_at: chrono::NaiveDateTime,
@ -28,9 +28,8 @@ pub struct Comment {
pub struct PublicComment {
pub id: i64,
pub parent_post_id: i64,
pub parent_comment_id: Option<i64>,
pub upvotes: i64,
pub downvotes: i64,
pub upvotes: i32,
pub downvotes: i32,
pub content: String,
pub created_at: chrono::NaiveDateTime,
pub updated_at: chrono::NaiveDateTime,