From 60d18771d741ebc5f2a3cbf17cf6b01befc68c81 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 01:57:47 +0300 Subject: [PATCH] comment, interaction db --- ...20241204225128_create_role_table.down.sql} | 1 - .../20241204225128_create_role_table.up.sql | 9 +++ .../20241204225135_create_user_table.down.sql | 2 + ...> 20241204225135_create_user_table.up.sql} | 9 --- ...20241204225143_create_post_table.down.sql} | 0 ...> 20241204225143_create_post_table.up.sql} | 2 +- ...241204225151_create_comment_table.down.sql | 2 + ...20241204225151_create_comment_table.up.sql | 7 ++ ...04225155_create_interaction_table.down.sql | 2 + ...1204225155_create_interaction_table.up.sql | 5 ++ src/database/comment.rs | 75 +++++++++++++++++++ src/database/interaction.rs | 68 +++++++++++++++++ src/database/post.rs | 2 +- src/database/role.rs | 2 +- src/database/user.rs | 2 +- src/feature/comment.rs | 7 ++ src/feature/interaction.rs | 21 +----- 17 files changed, 184 insertions(+), 32 deletions(-) rename migrations/{20241203135558_create_user_table.down.sql => 20241204225128_create_role_table.down.sql} (68%) create mode 100644 migrations/20241204225128_create_role_table.up.sql create mode 100644 migrations/20241204225135_create_user_table.down.sql rename migrations/{20241203135558_create_user_table.up.sql => 20241204225135_create_user_table.up.sql} (53%) rename migrations/{20241204130431_create_post_table.down.sql => 20241204225143_create_post_table.down.sql} (100%) rename migrations/{20241204130431_create_post_table.up.sql => 20241204225143_create_post_table.up.sql} (83%) create mode 100644 migrations/20241204225151_create_comment_table.down.sql create mode 100644 migrations/20241204225151_create_comment_table.up.sql create mode 100644 migrations/20241204225155_create_interaction_table.down.sql create mode 100644 migrations/20241204225155_create_interaction_table.up.sql diff --git a/migrations/20241203135558_create_user_table.down.sql b/migrations/20241204225128_create_role_table.down.sql similarity index 68% rename from migrations/20241203135558_create_user_table.down.sql rename to migrations/20241204225128_create_role_table.down.sql index 1d59bb0..c5b3463 100644 --- a/migrations/20241203135558_create_user_table.down.sql +++ b/migrations/20241204225128_create_role_table.down.sql @@ -1,3 +1,2 @@ -- Add down migration script here -DROP TABLE IF EXISTS "user"; DROP TABLE IF EXISTS "role"; \ No newline at end of file diff --git a/migrations/20241204225128_create_role_table.up.sql b/migrations/20241204225128_create_role_table.up.sql new file mode 100644 index 0000000..e33fee9 --- /dev/null +++ b/migrations/20241204225128_create_role_table.up.sql @@ -0,0 +1,9 @@ +-- Add up migration script here +CREATE TABLE IF NOT EXISTS "role"( + id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, + name VARCHAR(16) NOT NULL UNIQUE +); + +INSERT INTO "role"(id, name) VALUES (0, 'Ahmet Kaan Gümüş'); +INSERT INTO "role"(id, name) VALUES (1, 'Founder'); +INSERT INTO "role"(id, name) VALUES (2, 'Normal'); diff --git a/migrations/20241204225135_create_user_table.down.sql b/migrations/20241204225135_create_user_table.down.sql new file mode 100644 index 0000000..9e27c5d --- /dev/null +++ b/migrations/20241204225135_create_user_table.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS "user"; \ No newline at end of file diff --git a/migrations/20241203135558_create_user_table.up.sql b/migrations/20241204225135_create_user_table.up.sql similarity index 53% rename from migrations/20241203135558_create_user_table.up.sql rename to migrations/20241204225135_create_user_table.up.sql index c1ed37f..66b10f3 100644 --- a/migrations/20241203135558_create_user_table.up.sql +++ b/migrations/20241204225135_create_user_table.up.sql @@ -1,13 +1,4 @@ -- Add up migration script here -CREATE TABLE IF NOT EXISTS "role"( - id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, - name VARCHAR(16) NOT NULL UNIQUE -); - -INSERT INTO "role"(id, name) VALUES (0, 'Ahmet Kaan Gümüş'); -INSERT INTO "role"(id, name) VALUES (1, 'Founder'); -INSERT INTO "role"(id, name) VALUES (2, 'Normal'); - CREATE TABLE IF NOT EXISTS "user"( id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, name VARCHAR(255) NOT NULL, diff --git a/migrations/20241204130431_create_post_table.down.sql b/migrations/20241204225143_create_post_table.down.sql similarity index 100% rename from migrations/20241204130431_create_post_table.down.sql rename to migrations/20241204225143_create_post_table.down.sql diff --git a/migrations/20241204130431_create_post_table.up.sql b/migrations/20241204225143_create_post_table.up.sql similarity index 83% rename from migrations/20241204130431_create_post_table.up.sql rename to migrations/20241204225143_create_post_table.up.sql index 6d6ba41..a6739ec 100644 --- a/migrations/20241204130431_create_post_table.up.sql +++ b/migrations/20241204225143_create_post_table.up.sql @@ -1,5 +1,5 @@ -- Add up migration script here -CREATE TABLE "post"( +CREATE TABLE IF NOT EXISTS "post"( creation_time TIMESTAMPTZ PRIMARY KEY UNIQUE NOT NULL, poster_id BIGSERIAL NOT NULL REFERENCES "user"(id), post VARCHAR NOT NULL UNIQUE diff --git a/migrations/20241204225151_create_comment_table.down.sql b/migrations/20241204225151_create_comment_table.down.sql new file mode 100644 index 0000000..057acd1 --- /dev/null +++ b/migrations/20241204225151_create_comment_table.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS "comment"; \ No newline at end of file diff --git a/migrations/20241204225151_create_comment_table.up.sql b/migrations/20241204225151_create_comment_table.up.sql new file mode 100644 index 0000000..41e5d74 --- /dev/null +++ b/migrations/20241204225151_create_comment_table.up.sql @@ -0,0 +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, + commenter_id BIGSERIAL NOT NULL REFERENCES "user"(id), + comment VARCHAR NOT NULL +); \ No newline at end of file diff --git a/migrations/20241204225155_create_interaction_table.down.sql b/migrations/20241204225155_create_interaction_table.down.sql new file mode 100644 index 0000000..5e308d5 --- /dev/null +++ b/migrations/20241204225155_create_interaction_table.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS "interaction"; \ No newline at end of file diff --git a/migrations/20241204225155_create_interaction_table.up.sql b/migrations/20241204225155_create_interaction_table.up.sql new file mode 100644 index 0000000..51b002b --- /dev/null +++ b/migrations/20241204225155_create_interaction_table.up.sql @@ -0,0 +1,5 @@ +-- Add up migration script here +CREATE TABLE IF NOT EXISTS "interaction"( + id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, + name VARCHAR(10) UNIQUE NOT NULL +); \ No newline at end of file diff --git a/src/database/comment.rs b/src/database/comment.rs index 8b13789..b5c3961 100644 --- a/src/database/comment.rs +++ b/src/database/comment.rs @@ -1 +1,76 @@ +use chrono::{DateTime, Utc}; +use sqlx::{Pool, Postgres}; +use crate::feature::comment::Comment; + +pub async fn create( + post_creation_time: DateTime, + creation_time: DateTime, + commenter_id: i64, + comment: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Comment, + r#" + INSERT INTO "comment"(post_creation_time, creation_time, commenter_id, comment) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + post_creation_time, + creation_time, + commenter_id, + comment, + ) + .fetch_one(database_connection) + .await +} + +pub async fn read( + creation_time: DateTime, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Comment, + r#" + SELECT * FROM "comment" WHERE "creation_time" = $1 + "#, + creation_time + ) + .fetch_one(database_connection) + .await +} + +pub async fn update( + creation_time: DateTime, + comment: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Comment, + r#" + UPDATE "comment" SET "comment" = $1 WHERE "creation_time" = $2 + RETURNING * + "#, + comment, + creation_time + ) + .fetch_one(database_connection) + .await +} + +pub async fn delete( + creation_time: DateTime, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Comment, + r#" + DELETE FROM "comment" where "creation_time" = $1 + RETURNING * + "#, + creation_time + ) + .fetch_one(database_connection) + .await +} diff --git a/src/database/interaction.rs b/src/database/interaction.rs index 8b13789..52eb246 100644 --- a/src/database/interaction.rs +++ b/src/database/interaction.rs @@ -1 +1,69 @@ +use sqlx::{Pool, Postgres}; +use crate::feature::interaction::Interaction; + +pub async fn create( + name: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Interaction, + r#" + INSERT INTO "interaction"(name) + VALUES ($1) + RETURNING * + "#, + name, + ) + .fetch_one(database_connection) + .await +} + +pub async fn read( + name: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Interaction, + r#" + SELECT * FROM "interaction" WHERE "name" = $1 + "#, + name + ) + .fetch_one(database_connection) + .await +} + +pub async fn update( + id: i64, + name: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Interaction, + r#" + UPDATE "interaction" SET "name" = $1 WHERE "id" = $2 + RETURNING * + "#, + name, + id + ) + .fetch_one(database_connection) + .await +} + +pub async fn delete( + id: i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Interaction, + r#" + DELETE FROM "interaction" where "id" = $1 + RETURNING * + "#, + id + ) + .fetch_one(database_connection) + .await +} diff --git a/src/database/post.rs b/src/database/post.rs index 1ac3bfa..5b565b1 100644 --- a/src/database/post.rs +++ b/src/database/post.rs @@ -66,7 +66,7 @@ pub async fn delete( sqlx::query_as!( Post, r#" - DELETE FROM "post" where creation_time = $1 + DELETE FROM "post" where "creation_time" = $1 RETURNING * "#, creation_time diff --git a/src/database/role.rs b/src/database/role.rs index 0bfcb40..eda704b 100644 --- a/src/database/role.rs +++ b/src/database/role.rs @@ -56,7 +56,7 @@ pub async fn delete(id: i64, database_connection: &Pool) -> Result) -> Result, + pub creation_time: DateTime, + pub commenter_id: i64, + pub comment: String, +} diff --git a/src/feature/interaction.rs b/src/feature/interaction.rs index 3154916..6064701 100644 --- a/src/feature/interaction.rs +++ b/src/feature/interaction.rs @@ -1,22 +1,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] -enum InteractionType { - Like, - Dislike, - Heart, - Confetti, - Plus, - Minus, - Rocket, - Smile, - Laugh, - Sad, - Shrug, -} -#[derive(Debug, Serialize, Deserialize)] -struct Interaction { - post_id: String, - user_id: String, - interaction_type: InteractionType, +pub struct Interaction { + pub id: i64, + pub name: String, }