diff --git a/migrations/20241204230240_create_post_interaction_table.down.sql b/migrations/20241204230240_create_post_interaction_table.down.sql new file mode 100644 index 0000000..2c62083 --- /dev/null +++ b/migrations/20241204230240_create_post_interaction_table.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS "post_interaction"; \ No newline at end of file diff --git a/migrations/20241204230240_create_post_interaction_table.up.sql b/migrations/20241204230240_create_post_interaction_table.up.sql new file mode 100644 index 0000000..f7980de --- /dev/null +++ b/migrations/20241204230240_create_post_interaction_table.up.sql @@ -0,0 +1,7 @@ +-- Add up migration script here +CREATE TABLE IF NOT EXISTS "post_interaction"( + post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time), + interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id), + interactor_id BIGSERIAL NOT NULL REFERENCES "user"(id), + interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE +); \ No newline at end of file diff --git a/migrations/20241204230248_create_comment_interaction_table.down.sql b/migrations/20241204230248_create_comment_interaction_table.down.sql new file mode 100644 index 0000000..c5e2766 --- /dev/null +++ b/migrations/20241204230248_create_comment_interaction_table.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS "comment_interaction"; \ No newline at end of file diff --git a/migrations/20241204230248_create_comment_interaction_table.up.sql b/migrations/20241204230248_create_comment_interaction_table.up.sql new file mode 100644 index 0000000..f4825c1 --- /dev/null +++ b/migrations/20241204230248_create_comment_interaction_table.up.sql @@ -0,0 +1,7 @@ +-- Add up migration script here +CREATE TABLE IF NOT EXISTS "comment_interaction"( + comment_creation_time TIMESTAMPTZ NOT NULL REFERENCES "comment"(creation_time), + interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id), + interactor_id BIGSERIAL NOT NULL REFERENCES "user"(id), + interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE +); \ No newline at end of file diff --git a/src/database.rs b/src/database.rs index 0ab127c..606dcce 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,6 +1,8 @@ pub mod comment; +pub mod comment_interaction; pub mod interaction; pub mod post; +pub mod post_interaction; pub mod role; pub mod user; diff --git a/src/database/comment_interaction.rs b/src/database/comment_interaction.rs new file mode 100644 index 0000000..465a35e --- /dev/null +++ b/src/database/comment_interaction.rs @@ -0,0 +1,73 @@ +use chrono::{DateTime, Utc}; +use sqlx::{Pool, Postgres}; + +use crate::feature::comment_interaction::CommentInteraction; + +pub async fn create( + comment_creation_time: &DateTime, + interaction_id: i64, + interactor_id: i64, + interaction_time: &DateTime, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + CommentInteraction, + r#" + INSERT INTO "comment_interaction"(comment_creation_time, interaction_id, interactor_id, interaction_time) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + comment_creation_time, interaction_id, interactor_id, interaction_time, + ) + .fetch_one(database_connection) + .await +} + +pub async fn read( + interaction_time: &DateTime, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + CommentInteraction, + r#" + SELECT * FROM "comment_interaction" WHERE "interaction_time" = $1 + "#, + interaction_time + ) + .fetch_one(database_connection) + .await +} + +pub async fn update( + interaction_time: &DateTime, + interaction_id: i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + CommentInteraction, + r#" + UPDATE "comment_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, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + CommentInteraction, + r#" + DELETE FROM "comment_interaction" where "interaction_time" = $1 + RETURNING * + "#, + interaction_time + ) + .fetch_one(database_connection) + .await +} diff --git a/src/database/post_interaction.rs b/src/database/post_interaction.rs new file mode 100644 index 0000000..2933cd5 --- /dev/null +++ b/src/database/post_interaction.rs @@ -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, + interaction_id: i64, + interactor_id: i64, + interaction_time: &DateTime, + database_connection: &Pool, +) -> Result { + 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, + database_connection: &Pool, +) -> Result { + 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, + interaction_id: i64, + database_connection: &Pool, +) -> Result { + 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, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + PostInteraction, + r#" + DELETE FROM "post_interaction" where "interaction_time" = $1 + RETURNING * + "#, + interaction_time + ) + .fetch_one(database_connection) + .await +} diff --git a/src/feature.rs b/src/feature.rs index 314d8c8..a0a90e8 100644 --- a/src/feature.rs +++ b/src/feature.rs @@ -1,5 +1,7 @@ pub mod comment; +pub mod comment_interaction; pub mod interaction; pub mod post; +pub mod post_interaction; pub mod role; pub mod user; diff --git a/src/feature/comment_interaction.rs b/src/feature/comment_interaction.rs new file mode 100644 index 0000000..d68fa98 --- /dev/null +++ b/src/feature/comment_interaction.rs @@ -0,0 +1,8 @@ +use chrono::{DateTime, Utc}; + +pub struct CommentInteraction { + pub comment_creation_time: DateTime, + pub interaction_id: i64, + pub interactor_id: i64, + pub interaction_time: DateTime, +} diff --git a/src/feature/post_interaction.rs b/src/feature/post_interaction.rs new file mode 100644 index 0000000..5ac9156 --- /dev/null +++ b/src/feature/post_interaction.rs @@ -0,0 +1,8 @@ +use chrono::{DateTime, Utc}; + +pub struct PostInteraction { + pub post_creation_time: DateTime, + pub interaction_id: i64, + pub interactor_id: i64, + pub interaction_time: DateTime, +}