comment, interaction db

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-05 01:57:47 +03:00
parent ee9015c1e3
commit 60d18771d7
17 changed files with 184 additions and 32 deletions

View file

@ -1,3 +1,2 @@
-- Add down migration script here
DROP TABLE IF EXISTS "user";
DROP TABLE IF EXISTS "role";

View file

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

View file

@ -0,0 +1,2 @@
-- Add down migration script here
DROP TABLE IF EXISTS "user";

View file

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

View file

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

View file

@ -0,0 +1,2 @@
-- Add down migration script here
DROP TABLE IF EXISTS "comment";

View file

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

View file

@ -0,0 +1,2 @@
-- Add down migration script here
DROP TABLE IF EXISTS "interaction";

View file

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

View file

@ -1 +1,76 @@
use chrono::{DateTime, Utc};
use sqlx::{Pool, Postgres};
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>,
) -> Result<Comment, sqlx::Error> {
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<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
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<Utc>,
comment: &String,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
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<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
sqlx::query_as!(
Comment,
r#"
DELETE FROM "comment" where "creation_time" = $1
RETURNING *
"#,
creation_time
)
.fetch_one(database_connection)
.await
}

View file

@ -1 +1,69 @@
use sqlx::{Pool, Postgres};
use crate::feature::interaction::Interaction;
pub async fn create(
name: &String,
database_connection: &Pool<Postgres>,
) -> Result<Interaction, sqlx::Error> {
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<Postgres>,
) -> Result<Interaction, sqlx::Error> {
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<Postgres>,
) -> Result<Interaction, sqlx::Error> {
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<Postgres>,
) -> Result<Interaction, sqlx::Error> {
sqlx::query_as!(
Interaction,
r#"
DELETE FROM "interaction" where "id" = $1
RETURNING *
"#,
id
)
.fetch_one(database_connection)
.await
}

View file

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

View file

@ -56,7 +56,7 @@ pub async fn delete(id: i64, database_connection: &Pool<Postgres>) -> Result<Rol
sqlx::query_as!(
Role,
r#"
DELETE FROM "role" where id = $1
DELETE FROM "role" where "id" = $1
RETURNING *
"#,
id

View file

@ -65,7 +65,7 @@ pub async fn delete(id: i64, database_connection: &Pool<Postgres>) -> Result<Use
sqlx::query_as!(
User,
r#"
DELETE FROM "user" where id = $1
DELETE FROM "user" where "id" = $1
RETURNING *
"#,
id

View file

@ -1 +1,8 @@
use chrono::{DateTime, Utc};
pub struct Comment {
pub post_creation_time: DateTime<Utc>,
pub creation_time: DateTime<Utc>,
pub commenter_id: i64,
pub comment: String,
}

View file

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