user, role, post db
This commit is contained in:
parent
20af44c357
commit
ee9015c1e3
15 changed files with 210 additions and 74 deletions
|
@ -1,3 +1,3 @@
|
|||
-- Add down migration script here
|
||||
DROP TABLE IF EXISTS "user";
|
||||
DROP TYPE IF EXISTS role;
|
||||
DROP TABLE IF EXISTS "role";
|
|
@ -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)
|
||||
);
|
2
migrations/20241204130431_create_post_table.down.sql
Normal file
2
migrations/20241204130431_create_post_table.down.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
-- Add down migration script here
|
||||
DROP TABLE IF EXISTS "post";
|
6
migrations/20241204130431_create_post_table.up.sql
Normal file
6
migrations/20241204130431_create_post_table.up.sql
Normal file
|
@ -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
|
||||
);
|
|
@ -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;
|
||||
|
|
|
@ -1 +1,76 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
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>,
|
||||
) -> Result<Post, sqlx::Error> {
|
||||
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<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Post, sqlx::Error> {
|
||||
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<Utc>,
|
||||
poster_id: i64,
|
||||
post: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Post, sqlx::Error> {
|
||||
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<Utc>,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Post, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Post,
|
||||
r#"
|
||||
DELETE FROM "post" where creation_time = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
creation_time
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
|
66
src/database/role.rs
Normal file
66
src/database/role.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::feature::role::Role;
|
||||
|
||||
pub async fn create(
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Role, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<Role, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<Role, sqlx::Error> {
|
||||
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<Postgres>) -> Result<Role, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Role,
|
||||
r#"
|
||||
DELETE FROM "role" where id = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
|
@ -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<Postgres>,
|
||||
) -> Result<User, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<User, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<User, sqlx::Error> {
|
||||
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<Postgres>,
|
||||
) -> Result<User, sqlx::Error> {
|
||||
pub async fn delete(id: i64, database_connection: &Pool<Postgres>) -> Result<User, sqlx::Error> {
|
||||
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
|
||||
)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
pub mod comment;
|
||||
pub mod interaction;
|
||||
pub mod message;
|
||||
pub mod post;
|
||||
pub mod role;
|
||||
pub mod user;
|
||||
|
|
1
src/feature/comment.rs
Normal file
1
src/feature/comment.rs
Normal file
|
@ -0,0 +1 @@
|
|||
|
|
@ -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<Utc>,
|
||||
}
|
||||
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<Utc>,
|
||||
pub poster_id: i64,
|
||||
pub post: String,
|
||||
}
|
||||
|
||||
impl Post {
|
||||
pub async fn new(poster_email: String, post: String) -> Self {
|
||||
Self { poster_email, post }
|
||||
}
|
||||
}
|
||||
|
|
7
src/feature/role.rs
Normal file
7
src/feature/role.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Role {
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
}
|
|
@ -8,13 +8,6 @@ pub struct Contact {
|
|||
pub website: Option<String>,
|
||||
}
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue