wip: added get post comments, working insert comment to post
This commit is contained in:
@@ -1,8 +1,22 @@
|
||||
use crate::routes::comments::CommentInput;
|
||||
use sqlx::PgPool;
|
||||
use crate::routes::comments::{Comment, CommentInputPayload};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
pub struct CommentsDatasource;
|
||||
impl CommentsDatasource {
|
||||
pub async fn get_posts_comments(pool: PgPool) {}
|
||||
pub async fn insert_comment(pool: PgPool, comment_input: CommentInput) {}
|
||||
pub async fn get_posts_comments(
|
||||
pool: &Pool<Postgres>,
|
||||
post_id: i32,
|
||||
) -> Result<Vec<Comment>, sqlx::Error> {
|
||||
sqlx::query_as!(Comment, "SELECT c.comment_id, c.name, c.body, c.created_at FROM comments c LEFT JOIN posts p ON p.post_id = c.post_id WHERE p.post_id = $1 AND c.deleted_at IS NULL ORDER BY created_at DESC LIMIT 20", post_id)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
}
|
||||
pub async fn insert_comment(
|
||||
pool: &Pool<Postgres>,
|
||||
comment_input: CommentInputPayload,
|
||||
) -> Result<Comment, sqlx::Error> {
|
||||
sqlx::query!("INSERT INTO comments (post_id, name, body) VALUES ($1, $2, $3) RETURNING comment_id, name, body, created_at", comment_input.post_id, comment_input.name, comment_input.body)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user