From dca2abb62ab2f1102eef36ed6829bc883a53aeef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Thu, 5 Dec 2024 02:30:51 +0300 Subject: [PATCH] feat: :sparkles: let database handles creation times --- migrations/20241204225135_create_user_table.up.sql | 3 ++- migrations/20241204225143_create_post_table.up.sql | 2 +- migrations/20241204225151_create_comment_table.up.sql | 2 +- .../20241204230240_create_post_interaction_table.up.sql | 2 +- ...0241204230248_create_comment_interaction_table.up.sql | 2 +- src/database/comment.rs | 6 ++---- src/database/comment_interaction.rs | 9 +++++---- src/database/post.rs | 6 ++---- src/database/post_interaction.rs | 9 +++++---- src/feature/user.rs | 3 ++- 10 files changed, 22 insertions(+), 22 deletions(-) diff --git a/migrations/20241204225135_create_user_table.up.sql b/migrations/20241204225135_create_user_table.up.sql index 66b10f3..5402bd4 100644 --- a/migrations/20241204225135_create_user_table.up.sql +++ b/migrations/20241204225135_create_user_table.up.sql @@ -6,5 +6,6 @@ CREATE TABLE IF NOT EXISTS "user"( gender boolean NOT NULL, birth_date DATE NOT NULL, email VARCHAR(255) NOT NULL UNIQUE, - role_id BIGSERIAL NOT NULL REFERENCES "role"(id) + role_id BIGSERIAL NOT NULL REFERENCES "role"(id), + creation_time TIMESTAMPTZ NOT NULL DEFAULT NOW() ); \ No newline at end of file diff --git a/migrations/20241204225143_create_post_table.up.sql b/migrations/20241204225143_create_post_table.up.sql index a6739ec..c7be5f0 100644 --- a/migrations/20241204225143_create_post_table.up.sql +++ b/migrations/20241204225143_create_post_table.up.sql @@ -1,6 +1,6 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "post"( - creation_time TIMESTAMPTZ PRIMARY KEY UNIQUE NOT NULL, + creation_time TIMESTAMPTZ PRIMARY KEY UNIQUE NOT NULL DEFAULT NOW(), poster_id BIGSERIAL NOT NULL REFERENCES "user"(id), post VARCHAR NOT NULL UNIQUE ); \ No newline at end of file diff --git a/migrations/20241204225151_create_comment_table.up.sql b/migrations/20241204225151_create_comment_table.up.sql index 41e5d74..0339b36 100644 --- a/migrations/20241204225151_create_comment_table.up.sql +++ b/migrations/20241204225151_create_comment_table.up.sql @@ -1,7 +1,7 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "comment"( post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time), - creation_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE, + creation_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(), commenter_id BIGSERIAL NOT NULL REFERENCES "user"(id), comment VARCHAR NOT NULL ); \ 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 index f7980de..fc12e83 100644 --- a/migrations/20241204230240_create_post_interaction_table.up.sql +++ b/migrations/20241204230240_create_post_interaction_table.up.sql @@ -3,5 +3,5 @@ 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 + interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW() ); \ 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 index f4825c1..1742b20 100644 --- a/migrations/20241204230248_create_comment_interaction_table.up.sql +++ b/migrations/20241204230248_create_comment_interaction_table.up.sql @@ -3,5 +3,5 @@ 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 + interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW() ); \ No newline at end of file diff --git a/src/database/comment.rs b/src/database/comment.rs index b5c3961..8be1a01 100644 --- a/src/database/comment.rs +++ b/src/database/comment.rs @@ -5,7 +5,6 @@ use crate::feature::comment::Comment; pub async fn create( post_creation_time: DateTime, - creation_time: DateTime, commenter_id: i64, comment: &String, database_connection: &Pool, @@ -13,12 +12,11 @@ pub async fn create( sqlx::query_as!( Comment, r#" - INSERT INTO "comment"(post_creation_time, creation_time, commenter_id, comment) - VALUES ($1, $2, $3, $4) + INSERT INTO "comment"(post_creation_time, commenter_id, comment) + VALUES ($1, $2, $3) RETURNING * "#, post_creation_time, - creation_time, commenter_id, comment, ) diff --git a/src/database/comment_interaction.rs b/src/database/comment_interaction.rs index 465a35e..42135f8 100644 --- a/src/database/comment_interaction.rs +++ b/src/database/comment_interaction.rs @@ -7,17 +7,18 @@ 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) + INSERT INTO "comment_interaction"(comment_creation_time, interaction_id, interactor_id) + VALUES ($1, $2, $3) RETURNING * "#, - comment_creation_time, interaction_id, interactor_id, interaction_time, + comment_creation_time, + interaction_id, + interactor_id, ) .fetch_one(database_connection) .await diff --git a/src/database/post.rs b/src/database/post.rs index 5b565b1..6221b79 100644 --- a/src/database/post.rs +++ b/src/database/post.rs @@ -4,7 +4,6 @@ use sqlx::{Pool, Postgres}; use crate::feature::post::Post; pub async fn create( - creation_time: &DateTime, poster_id: i64, post: &String, database_connection: &Pool, @@ -12,11 +11,10 @@ pub async fn create( sqlx::query_as!( Post, r#" - INSERT INTO "post"(creation_time, poster_id, post) - VALUES ($1, $2, $3) + INSERT INTO "post"(poster_id, post) + VALUES ($1, $2) RETURNING * "#, - creation_time, poster_id, post ) diff --git a/src/database/post_interaction.rs b/src/database/post_interaction.rs index 2933cd5..1adda53 100644 --- a/src/database/post_interaction.rs +++ b/src/database/post_interaction.rs @@ -7,17 +7,18 @@ 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) + INSERT INTO "post_interaction"(post_creation_time, interaction_id, interactor_id) + VALUES ($1, $2, $3) RETURNING * "#, - post_creation_time, interaction_id, interactor_id, interaction_time, + post_creation_time, + interaction_id, + interactor_id, ) .fetch_one(database_connection) .await diff --git a/src/feature/user.rs b/src/feature/user.rs index 350a09a..969b087 100644 --- a/src/feature/user.rs +++ b/src/feature/user.rs @@ -1,4 +1,4 @@ -use chrono::NaiveDate; +use chrono::{DateTime, NaiveDate, Utc}; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] @@ -17,4 +17,5 @@ pub struct User { pub birth_date: NaiveDate, pub email: String, pub role_id: i64, + pub creation_time: DateTime, }