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