post, comment interaction db
This commit is contained in:
parent
60d18771d7
commit
1e8d656711
10 changed files with 184 additions and 0 deletions
73
src/database/post_interaction.rs
Normal file
73
src/database/post_interaction.rs
Normal file
|
@ -0,0 +1,73 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::feature::post_interaction::PostInteraction;
|
||||
|
||||
pub async fn create(
|
||||
post_creation_time: &DateTime<Utc>,
|
||||
interaction_id: i64,
|
||||
interactor_id: i64,
|
||||
interaction_time: &DateTime<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<PostInteraction, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
PostInteraction,
|
||||
r#"
|
||||
INSERT INTO "post_interaction"(post_creation_time, interaction_id, interactor_id, interaction_time)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING *
|
||||
"#,
|
||||
post_creation_time, interaction_id, interactor_id, interaction_time,
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read(
|
||||
interaction_time: &DateTime<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<PostInteraction, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
PostInteraction,
|
||||
r#"
|
||||
SELECT * FROM "post_interaction" WHERE "interaction_time" = $1
|
||||
"#,
|
||||
interaction_time
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
interaction_time: &DateTime<Utc>,
|
||||
interaction_id: i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<PostInteraction, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
PostInteraction,
|
||||
r#"
|
||||
UPDATE "post_interaction" SET "interaction_id" = $1 WHERE "interaction_time" = $2
|
||||
RETURNING *
|
||||
"#,
|
||||
interaction_id,
|
||||
interaction_time
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
interaction_time: &DateTime<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<PostInteraction, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
PostInteraction,
|
||||
r#"
|
||||
DELETE FROM "post_interaction" where "interaction_time" = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
interaction_time
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue