diff --git a/migrations/20241203135558_create_user_table.down.sql b/migrations/20241203135558_create_user_table.down.sql index 7d2e7e3..1d59bb0 100644 --- a/migrations/20241203135558_create_user_table.down.sql +++ b/migrations/20241203135558_create_user_table.down.sql @@ -1,3 +1,3 @@ -- Add down migration script here DROP TABLE IF EXISTS "user"; -DROP TYPE IF EXISTS role; \ No newline at end of file +DROP TABLE IF EXISTS "role"; \ No newline at end of file diff --git a/migrations/20241203135558_create_user_table.up.sql b/migrations/20241203135558_create_user_table.up.sql index 571890a..c1ed37f 100644 --- a/migrations/20241203135558_create_user_table.up.sql +++ b/migrations/20241203135558_create_user_table.up.sql @@ -1,12 +1,19 @@ -- Add up migration script here -DROP TYPE IF EXISTS role; -CREATE TYPE role AS ENUM('Zero', 'Hero'); +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, - name VARCHAR(255) NOT NULL, - surname VARCHAR(255) NOT NULL, - gender boolean NOT NULL, - birth_date DATE NOT NULL, - email VARCHAR(255) NOT NULL UNIQUE, - role ROLE NOT NULL DEFAULT 'Zero' + id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, + name VARCHAR(255) NOT NULL, + surname VARCHAR(255) NOT NULL, + gender boolean NOT NULL, + birth_date DATE NOT NULL, + email VARCHAR(255) NOT NULL UNIQUE, + role_id BIGSERIAL NOT NULL REFERENCES "role"(id) ); \ No newline at end of file diff --git a/migrations/20241204130431_create_post_table.down.sql b/migrations/20241204130431_create_post_table.down.sql new file mode 100644 index 0000000..1cc5c25 --- /dev/null +++ b/migrations/20241204130431_create_post_table.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS "post"; \ No newline at end of file diff --git a/migrations/20241204130431_create_post_table.up.sql b/migrations/20241204130431_create_post_table.up.sql new file mode 100644 index 0000000..6d6ba41 --- /dev/null +++ b/migrations/20241204130431_create_post_table.up.sql @@ -0,0 +1,6 @@ +-- Add up migration script here +CREATE TABLE "post"( + creation_time TIMESTAMPTZ PRIMARY KEY UNIQUE NOT NULL, + poster_id BIGSERIAL NOT NULL REFERENCES "user"(id), + post VARCHAR NOT NULL UNIQUE +); \ No newline at end of file diff --git a/src/database.rs b/src/database.rs index 8f19e64..0ab127c 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,6 +1,7 @@ +pub mod comment; pub mod interaction; -pub mod message; pub mod post; +pub mod role; pub mod user; use std::time::Duration; diff --git a/src/database/message.rs b/src/database/comment.rs similarity index 100% rename from src/database/message.rs rename to src/database/comment.rs diff --git a/src/database/post.rs b/src/database/post.rs index 8b13789..1ac3bfa 100644 --- a/src/database/post.rs +++ b/src/database/post.rs @@ -1 +1,76 @@ +use chrono::{DateTime, Utc}; +use sqlx::{Pool, Postgres}; +use crate::feature::post::Post; + +pub async fn create( + creation_time: &DateTime, + poster_id: i64, + post: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Post, + r#" + INSERT INTO "post"(creation_time, poster_id, post) + VALUES ($1, $2, $3) + RETURNING * + "#, + creation_time, + poster_id, + post + ) + .fetch_one(database_connection) + .await +} + +pub async fn read( + creation_time: &DateTime, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Post, + r#" + SELECT * FROM "post" WHERE "creation_time" = $1 + "#, + creation_time + ) + .fetch_one(database_connection) + .await +} + +pub async fn update( + creation_time: &DateTime, + poster_id: i64, + post: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Post, + r#" + UPDATE "post" SET poster_id = $1, post = $2 WHERE "creation_time" = $3 + RETURNING * + "#, + poster_id, + post, + creation_time + ) + .fetch_one(database_connection) + .await +} + +pub async fn delete( + creation_time: &DateTime, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Post, + r#" + DELETE FROM "post" where creation_time = $1 + RETURNING * + "#, + creation_time + ) + .fetch_one(database_connection) + .await +} diff --git a/src/database/role.rs b/src/database/role.rs new file mode 100644 index 0000000..0bfcb40 --- /dev/null +++ b/src/database/role.rs @@ -0,0 +1,66 @@ +use sqlx::{Pool, Postgres}; + +use crate::feature::role::Role; + +pub async fn create( + name: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Role, + r#" + INSERT INTO "role"(name) + VALUES ($1) + RETURNING * + "#, + name, + ) + .fetch_one(database_connection) + .await +} + +pub async fn read( + name: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Role, + r#" + SELECT * FROM "role" WHERE "name" = $1 + "#, + name + ) + .fetch_one(database_connection) + .await +} + +pub async fn update( + id: i64, + name: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Role, + r#" + UPDATE "role" 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!( + Role, + r#" + DELETE FROM "role" where id = $1 + RETURNING * + "#, + id + ) + .fetch_one(database_connection) + .await +} diff --git a/src/database/user.rs b/src/database/user.rs index 4ef09fc..7b3f56b 100644 --- a/src/database/user.rs +++ b/src/database/user.rs @@ -1,9 +1,9 @@ use chrono::NaiveDate; use sqlx::{Pool, Postgres}; -use crate::feature::user::{Role, User}; +use crate::feature::user::User; -pub async fn create_user( +pub async fn create( name: &String, surname: &String, gender: bool, @@ -14,57 +14,59 @@ pub async fn create_user( sqlx::query_as!( User, r#" - INSERT INTO "user"(name, surname, gender, birth_date, email) - VALUES ($1, $2, $3, $4, $5) - RETURNING id, name, surname, gender, birth_date, email, role AS "role:Role" + INSERT INTO "user"(name, surname, gender, birth_date, email, role_id) + VALUES ($1, $2, $3, $4, $5, $6) + RETURNING * "#, name, surname, gender, birth_date, + email, + 2 + ) + .fetch_one(database_connection) + .await +} + +pub async fn read( + email: &String, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + User, + r#" + SELECT * FROM "user" WHERE "email" = $1 + "#, email ) .fetch_one(database_connection) .await } -pub async fn read_user( - email: &String, - database_connection: &Pool, -) -> Result { - sqlx::query_as!(User, - r#" - SELECT id, name, surname, gender, birth_date, email, role AS "role:Role" FROM "user" WHERE "email" = $1 - "#, - email - ).fetch_one(database_connection).await -} - -pub async fn update_user( +pub async fn update( id: i64, name: &String, surname: &String, gender: &bool, birth_date: &NaiveDate, email: &String, + role_id: i64, database_connection: &Pool, ) -> Result { sqlx::query_as!(User, r#" - UPDATE "user" SET "name" = $1, "surname" = $2, "gender" = $3, "birth_date" = $4, "email" = $5 WHERE "id" = $6 - RETURNING id, name, surname, gender, birth_date, email, role AS "role:Role" - "#, name, surname, gender, birth_date, email, id).fetch_one(database_connection).await + UPDATE "user" SET "name" = $1, "surname" = $2, "gender" = $3, "birth_date" = $4, "email" = $5, "role_id" = $6 WHERE "id" = $7 + RETURNING * + "#, name, surname, gender, birth_date, email, role_id, id).fetch_one(database_connection).await } -pub async fn delete_user( - id: i64, - database_connection: &Pool, -) -> Result { +pub async fn delete(id: i64, database_connection: &Pool) -> Result { sqlx::query_as!( User, r#" DELETE FROM "user" where id = $1 - RETURNING id, name, surname, gender, birth_date, email, role AS "role:Role" + RETURNING * "#, id ) diff --git a/src/feature.rs b/src/feature.rs index cb941d5..314d8c8 100644 --- a/src/feature.rs +++ b/src/feature.rs @@ -1,4 +1,5 @@ +pub mod comment; pub mod interaction; -pub mod message; pub mod post; +pub mod role; pub mod user; diff --git a/src/feature/comment.rs b/src/feature/comment.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/feature/comment.rs @@ -0,0 +1 @@ + diff --git a/src/feature/message.rs b/src/feature/message.rs deleted file mode 100644 index 7fc03ba..0000000 --- a/src/feature/message.rs +++ /dev/null @@ -1,21 +0,0 @@ -use chrono::{DateTime, Utc}; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Serialize, Deserialize)] -pub struct Message { - pub sender_email: String, - pub receiver_email: String, - pub message: String, - pub execution_time: DateTime, -} - -impl Message { - pub async fn new(sender_email: String, receiver_email: String, message: String) -> Self { - Self { - sender_email, - receiver_email, - message, - execution_time: Utc::now(), - } - } -} diff --git a/src/feature/post.rs b/src/feature/post.rs index d4cd769..c85a799 100644 --- a/src/feature/post.rs +++ b/src/feature/post.rs @@ -1,13 +1,9 @@ +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct Post { - pub poster_email: String, + pub creation_time: DateTime, + pub poster_id: i64, pub post: String, } - -impl Post { - pub async fn new(poster_email: String, post: String) -> Self { - Self { poster_email, post } - } -} diff --git a/src/feature/role.rs b/src/feature/role.rs new file mode 100644 index 0000000..ff5287a --- /dev/null +++ b/src/feature/role.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Role { + pub id: i64, + pub name: String, +} diff --git a/src/feature/user.rs b/src/feature/user.rs index 1dd9c06..350a09a 100644 --- a/src/feature/user.rs +++ b/src/feature/user.rs @@ -8,13 +8,6 @@ pub struct Contact { pub website: Option, } -#[derive(Debug, Serialize, Deserialize, sqlx::Type)] -#[sqlx(type_name = "role")] -pub enum Role { - Zero, - Hero, -} - #[derive(Debug, Serialize, Deserialize)] pub struct User { pub id: i64, @@ -23,5 +16,5 @@ pub struct User { pub gender: bool, pub birth_date: NaiveDate, pub email: String, - pub role: Role, + pub role_id: i64, }