feat: let database handles creation times

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-05 02:30:51 +03:00
parent 1e8d656711
commit dca2abb62a
10 changed files with 22 additions and 22 deletions

View file

@ -6,5 +6,6 @@ CREATE TABLE IF NOT EXISTS "user"(
gender boolean NOT NULL, gender boolean NOT NULL,
birth_date DATE NOT NULL, birth_date DATE NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE, 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()
); );

View file

@ -1,6 +1,6 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "post"( 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), poster_id BIGSERIAL NOT NULL REFERENCES "user"(id),
post VARCHAR NOT NULL UNIQUE post VARCHAR NOT NULL UNIQUE
); );

View file

@ -1,7 +1,7 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "comment"( CREATE TABLE IF NOT EXISTS "comment"(
post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time), 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), commenter_id BIGSERIAL NOT NULL REFERENCES "user"(id),
comment VARCHAR NOT NULL comment VARCHAR NOT NULL
); );

View file

@ -3,5 +3,5 @@ CREATE TABLE IF NOT EXISTS "post_interaction"(
post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time), post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time),
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id), interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id),
interactor_id BIGSERIAL NOT NULL REFERENCES "user"(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()
); );

View file

@ -3,5 +3,5 @@ CREATE TABLE IF NOT EXISTS "comment_interaction"(
comment_creation_time TIMESTAMPTZ NOT NULL REFERENCES "comment"(creation_time), comment_creation_time TIMESTAMPTZ NOT NULL REFERENCES "comment"(creation_time),
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id), interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id),
interactor_id BIGSERIAL NOT NULL REFERENCES "user"(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()
); );

View file

@ -5,7 +5,6 @@ use crate::feature::comment::Comment;
pub async fn create( pub async fn create(
post_creation_time: DateTime<Utc>, post_creation_time: DateTime<Utc>,
creation_time: DateTime<Utc>,
commenter_id: i64, commenter_id: i64,
comment: &String, comment: &String,
database_connection: &Pool<Postgres>, database_connection: &Pool<Postgres>,
@ -13,12 +12,11 @@ pub async fn create(
sqlx::query_as!( sqlx::query_as!(
Comment, Comment,
r#" r#"
INSERT INTO "comment"(post_creation_time, creation_time, commenter_id, comment) INSERT INTO "comment"(post_creation_time, commenter_id, comment)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3)
RETURNING * RETURNING *
"#, "#,
post_creation_time, post_creation_time,
creation_time,
commenter_id, commenter_id,
comment, comment,
) )

View file

@ -7,17 +7,18 @@ pub async fn create(
comment_creation_time: &DateTime<Utc>, comment_creation_time: &DateTime<Utc>,
interaction_id: i64, interaction_id: i64,
interactor_id: i64, interactor_id: i64,
interaction_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>, database_connection: &Pool<Postgres>,
) -> Result<CommentInteraction, sqlx::Error> { ) -> Result<CommentInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
CommentInteraction, CommentInteraction,
r#" r#"
INSERT INTO "comment_interaction"(comment_creation_time, interaction_id, interactor_id, interaction_time) INSERT INTO "comment_interaction"(comment_creation_time, interaction_id, interactor_id)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3)
RETURNING * RETURNING *
"#, "#,
comment_creation_time, interaction_id, interactor_id, interaction_time, comment_creation_time,
interaction_id,
interactor_id,
) )
.fetch_one(database_connection) .fetch_one(database_connection)
.await .await

View file

@ -4,7 +4,6 @@ use sqlx::{Pool, Postgres};
use crate::feature::post::Post; use crate::feature::post::Post;
pub async fn create( pub async fn create(
creation_time: &DateTime<Utc>,
poster_id: i64, poster_id: i64,
post: &String, post: &String,
database_connection: &Pool<Postgres>, database_connection: &Pool<Postgres>,
@ -12,11 +11,10 @@ pub async fn create(
sqlx::query_as!( sqlx::query_as!(
Post, Post,
r#" r#"
INSERT INTO "post"(creation_time, poster_id, post) INSERT INTO "post"(poster_id, post)
VALUES ($1, $2, $3) VALUES ($1, $2)
RETURNING * RETURNING *
"#, "#,
creation_time,
poster_id, poster_id,
post post
) )

View file

@ -7,17 +7,18 @@ pub async fn create(
post_creation_time: &DateTime<Utc>, post_creation_time: &DateTime<Utc>,
interaction_id: i64, interaction_id: i64,
interactor_id: i64, interactor_id: i64,
interaction_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>, database_connection: &Pool<Postgres>,
) -> Result<PostInteraction, sqlx::Error> { ) -> Result<PostInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
PostInteraction, PostInteraction,
r#" r#"
INSERT INTO "post_interaction"(post_creation_time, interaction_id, interactor_id, interaction_time) INSERT INTO "post_interaction"(post_creation_time, interaction_id, interactor_id)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3)
RETURNING * RETURNING *
"#, "#,
post_creation_time, interaction_id, interactor_id, interaction_time, post_creation_time,
interaction_id,
interactor_id,
) )
.fetch_one(database_connection) .fetch_one(database_connection)
.await .await

View file

@ -1,4 +1,4 @@
use chrono::NaiveDate; use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -17,4 +17,5 @@ pub struct User {
pub birth_date: NaiveDate, pub birth_date: NaiveDate,
pub email: String, pub email: String,
pub role_id: i64, pub role_id: i64,
pub creation_time: DateTime<Utc>,
} }