diff --git a/server/src/db.rs b/server/src/db.rs index d9d49e4..1c83492 100644 --- a/server/src/db.rs +++ b/server/src/db.rs @@ -9,14 +9,13 @@ use sqlx::PgPool; pub async fn db_new_comment( pool: &PgPool, parent_post_id: i64, - parent_comment_id: Option, + // parent_comment_id: Option, 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 { 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 diff --git a/server/src/routes/comment.rs b/server/src/routes/comment.rs index e7098f1..acd8cf7 100644 --- a/server/src/routes/comment.rs +++ b/server/src/routes/comment.rs @@ -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 => { diff --git a/server/src/types/comment.rs b/server/src/types/comment.rs index 0fcdc7c..70fe32a 100644 --- a/server/src/types/comment.rs +++ b/server/src/types/comment.rs @@ -16,8 +16,8 @@ pub struct Comment { pub parent_post_id: i64, pub parent_comment_id: Option, 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, - 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,