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,
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()
);

View file

@ -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
);

View file

@ -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
);

View file

@ -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()
);

View file

@ -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()
);

View file

@ -5,7 +5,6 @@ use crate::feature::comment::Comment;
pub async fn create(
post_creation_time: DateTime<Utc>,
creation_time: DateTime<Utc>,
commenter_id: i64,
comment: &String,
database_connection: &Pool<Postgres>,
@ -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,
)

View file

@ -7,17 +7,18 @@ pub async fn create(
comment_creation_time: &DateTime<Utc>,
interaction_id: i64,
interactor_id: i64,
interaction_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<CommentInteraction, sqlx::Error> {
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

View file

@ -4,7 +4,6 @@ use sqlx::{Pool, Postgres};
use crate::feature::post::Post;
pub async fn create(
creation_time: &DateTime<Utc>,
poster_id: i64,
post: &String,
database_connection: &Pool<Postgres>,
@ -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
)

View file

@ -7,17 +7,18 @@ 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)
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

View file

@ -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<Utc>,
}