Working comment route fully implemented
This commit is contained in:
parent
32ff43e87c
commit
d0e5b57006
3 changed files with 14 additions and 17 deletions
|
@ -9,14 +9,13 @@ use sqlx::PgPool;
|
||||||
pub async fn db_new_comment(
|
pub async fn db_new_comment(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
parent_post_id: i64,
|
parent_post_id: i64,
|
||||||
parent_comment_id: Option<i64>,
|
// parent_comment_id: Option<i64>,
|
||||||
user_id: i64,
|
user_id: i64,
|
||||||
content: &str,
|
content: &str,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let insert_query = sqlx::query!(
|
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_post_id,
|
||||||
parent_comment_id,
|
|
||||||
user_id,
|
user_id,
|
||||||
content
|
content
|
||||||
)
|
)
|
||||||
|
@ -40,7 +39,8 @@ pub async fn db_get_comments(
|
||||||
) -> Vec<PublicComment> {
|
) -> Vec<PublicComment> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
PublicComment,
|
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,
|
parent_post_id,
|
||||||
limit,
|
limit,
|
||||||
offset
|
offset
|
||||||
|
|
|
@ -24,6 +24,11 @@ pub async fn get_comments(
|
||||||
|
|
||||||
let comments = db_get_comments(&state.pool, post_id, limit, offset).await;
|
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))
|
Ok(HttpResponse::Ok().json(comments))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,14 +65,7 @@ pub async fn new_comment(
|
||||||
|
|
||||||
info!("Creating a new comment {:?}", &data);
|
info!("Creating a new comment {:?}", &data);
|
||||||
|
|
||||||
let success = db_new_comment(
|
let success = db_new_comment(&state.pool, data.parent_post_id, userid, &content).await;
|
||||||
&state.pool,
|
|
||||||
data.parent_post_id,
|
|
||||||
data.parent_comment_id,
|
|
||||||
userid,
|
|
||||||
&content,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
match success {
|
match success {
|
||||||
true => {
|
true => {
|
||||||
|
|
|
@ -16,8 +16,8 @@ pub struct Comment {
|
||||||
pub parent_post_id: i64,
|
pub parent_post_id: i64,
|
||||||
pub parent_comment_id: Option<i64>,
|
pub parent_comment_id: Option<i64>,
|
||||||
pub author_user_id: i64,
|
pub author_user_id: i64,
|
||||||
pub upvotes: i64,
|
pub upvotes: i32,
|
||||||
pub downvotes: i64,
|
pub downvotes: i32,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub created_at: chrono::NaiveDateTime,
|
pub created_at: chrono::NaiveDateTime,
|
||||||
pub updated_at: chrono::NaiveDateTime,
|
pub updated_at: chrono::NaiveDateTime,
|
||||||
|
@ -28,9 +28,8 @@ pub struct Comment {
|
||||||
pub struct PublicComment {
|
pub struct PublicComment {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub parent_post_id: i64,
|
pub parent_post_id: i64,
|
||||||
pub parent_comment_id: Option<i64>,
|
pub upvotes: i32,
|
||||||
pub upvotes: i64,
|
pub downvotes: i32,
|
||||||
pub downvotes: i64,
|
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub created_at: chrono::NaiveDateTime,
|
pub created_at: chrono::NaiveDateTime,
|
||||||
pub updated_at: chrono::NaiveDateTime,
|
pub updated_at: chrono::NaiveDateTime,
|
||||||
|
|
Loading…
Reference in a new issue