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(
|
|
|
|
post_creation_time: DateTime<Utc>,
|
|
|
|
commenter_id: i64,
|
|
|
|
comment: &String,
|
|
|
|
database_connection: &Pool<Postgres>,
|
|
|
|
) -> Result<Comment, sqlx::Error> {
|
|
|
|
sqlx::query_as!(
|
|
|
|
Comment,
|
|
|
|
r#"
|
2024-12-05 02:30:51 +03:00
|
|
|
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(
|
|
|
|
creation_time: DateTime<Utc>,
|
|
|
|
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(
|
|
|
|
creation_time: DateTime<Utc>,
|
|
|
|
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(
|
|
|
|
creation_time: DateTime<Utc>,
|
|
|
|
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
|
|
|
|
}
|
2024-12-05 02:47:51 +03:00
|
|
|
|
|
|
|
pub async fn read_all(
|
|
|
|
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
|
|
|
|
}
|