rust_forum/src/database/comment.rs

90 lines
2 KiB
Rust
Raw Normal View History

2024-12-05 01:57:47 +03:00
use chrono::{DateTime, Utc};
use sqlx::{Pool, Postgres};
2024-12-02 23:56:43 +03:00
2024-12-05 01:57:47 +03:00
use crate::feature::comment::Comment;
pub async fn create(
2024-12-09 15:18:32 +03:00
post_creation_time: &DateTime<Utc>,
commenter_id: &i64,
2024-12-05 01:57:47 +03:00
comment: &String,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
sqlx::query_as!(
Comment,
r#"
INSERT INTO "comment"(post_creation_time, commenter_id, comment)
VALUES ($1, $2, $3)
2024-12-05 01:57:47 +03:00
RETURNING *
"#,
post_creation_time,
commenter_id,
comment,
)
.fetch_one(database_connection)
.await
}
pub async fn read(
2024-12-09 15:18:32 +03:00
creation_time: &DateTime<Utc>,
2024-12-05 01:57:47 +03:00
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
sqlx::query_as!(
Comment,
r#"
SELECT * FROM "comment" WHERE "creation_time" = $1
"#,
creation_time
)
.fetch_one(database_connection)
.await
}
pub async fn update(
2024-12-09 15:18:32 +03:00
creation_time: &DateTime<Utc>,
2024-12-05 01:57:47 +03:00
comment: &String,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
sqlx::query_as!(
Comment,
r#"
UPDATE "comment" SET "comment" = $1 WHERE "creation_time" = $2
RETURNING *
"#,
comment,
creation_time
)
.fetch_one(database_connection)
.await
}
pub async fn delete(
2024-12-09 15:18:32 +03:00
creation_time: &DateTime<Utc>,
2024-12-05 01:57:47 +03:00
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
sqlx::query_as!(
Comment,
r#"
DELETE FROM "comment" where "creation_time" = $1
RETURNING *
"#,
creation_time
)
.fetch_one(database_connection)
.await
}
pub async fn read_all_for_post(
2024-12-09 15:18:32 +03:00
post_creation_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<Vec<Comment>, sqlx::Error> {
sqlx::query_as!(
Comment,
r#"
SELECT * FROM "comment" WHERE "post_creation_time" = $1
"#,
post_creation_time
)
.fetch_all(database_connection)
.await
}