diff --git a/src/database.rs b/src/database.rs index 606dcce..76e69cf 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,6 +1,7 @@ pub mod comment; pub mod comment_interaction; pub mod interaction; +pub mod permission; pub mod post; pub mod post_interaction; pub mod role; diff --git a/src/database/comment.rs b/src/database/comment.rs index 5f52ec4..085172d 100644 --- a/src/database/comment.rs +++ b/src/database/comment.rs @@ -4,8 +4,8 @@ use sqlx::{Pool, Postgres}; use crate::feature::comment::Comment; pub async fn create( - post_creation_time: DateTime, - commenter_id: i64, + post_creation_time: &DateTime, + commenter_id: &i64, comment: &String, database_connection: &Pool, ) -> Result { @@ -25,7 +25,7 @@ pub async fn create( } pub async fn read( - creation_time: DateTime, + creation_time: &DateTime, database_connection: &Pool, ) -> Result { sqlx::query_as!( @@ -40,7 +40,7 @@ pub async fn read( } pub async fn update( - creation_time: DateTime, + creation_time: &DateTime, comment: &String, database_connection: &Pool, ) -> Result { @@ -58,7 +58,7 @@ pub async fn update( } pub async fn delete( - creation_time: DateTime, + creation_time: &DateTime, database_connection: &Pool, ) -> Result { sqlx::query_as!( @@ -74,7 +74,7 @@ pub async fn delete( } pub async fn read_all_for_post( - post_creation_time: DateTime, + post_creation_time: &DateTime, database_connection: &Pool, ) -> Result, sqlx::Error> { sqlx::query_as!( diff --git a/src/database/comment_interaction.rs b/src/database/comment_interaction.rs index a2559c6..8280c00 100644 --- a/src/database/comment_interaction.rs +++ b/src/database/comment_interaction.rs @@ -5,8 +5,8 @@ use crate::feature::comment_interaction::CommentInteraction; pub async fn create( comment_creation_time: &DateTime, - interaction_id: i64, - interactor_id: i64, + interaction_id: &i64, + interactor_id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( @@ -41,7 +41,7 @@ pub async fn read( pub async fn update( interaction_time: &DateTime, - interaction_id: i64, + interaction_id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( diff --git a/src/database/interaction.rs b/src/database/interaction.rs index 52eb246..c69860a 100644 --- a/src/database/interaction.rs +++ b/src/database/interaction.rs @@ -35,7 +35,7 @@ pub async fn read( } pub async fn update( - id: i64, + id: &i64, name: &String, database_connection: &Pool, ) -> Result { @@ -53,7 +53,7 @@ pub async fn update( } pub async fn delete( - id: i64, + id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( diff --git a/src/database/permission.rs b/src/database/permission.rs new file mode 100644 index 0000000..49bed5e --- /dev/null +++ b/src/database/permission.rs @@ -0,0 +1,148 @@ +use std::fmt::Display; + +use sqlx::{Pool, Postgres}; + +use crate::feature::permission::Permission; + +#[derive(Debug)] +pub enum PermissionTable { + Role, + User, + Post, + Comment, + Interaction, + PostInteraction, + CommentInteraction, +} + +impl Display for PermissionTable { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let default_identifier = "permission_"; + match self { + PermissionTable::Role => write!(f, "{}{}", default_identifier, "role"), + PermissionTable::User => write!(f, "{}{}", default_identifier, "user"), + PermissionTable::Post => write!(f, "{}{}", default_identifier, "post"), + PermissionTable::Comment => write!(f, "{}{}", default_identifier, "comment"), + PermissionTable::Interaction => write!(f, "{}{}", default_identifier, "interaction"), + PermissionTable::PostInteraction => { + write!(f, "{}{}", default_identifier, "post_interaction") + } + PermissionTable::CommentInteraction => { + write!(f, "{}{}", default_identifier, "comment_interaction") + } + } + } +} + +pub async fn create( + role_id: &i64, + create_self: &bool, + read_self: &bool, + update_self: &bool, + delete_self: &bool, + create_other: &bool, + read_other: &bool, + update_other: &bool, + delete_other: &bool, + create_lower: &bool, + read_lower: &bool, + update_lower: &bool, + delete_lower: &bool, + permission_table: &PermissionTable, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Permission, + r#" + INSERT INTO "permission_role"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) + RETURNING * + "#, + role_id, + create_self, + read_self, + update_self, + delete_self, + create_other, + read_other, + update_other, + delete_other, + create_lower, + read_lower, + update_lower, + delete_lower, + ) + .fetch_one(database_connection) + .await +} + +pub async fn read( + role_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Permission, + r#" + SELECT * FROM "permission_role" WHERE "role_id" = $1 + "#, + role_id + ) + .fetch_one(database_connection) + .await +} + +pub async fn update( + role_id: &i64, + create_self: &bool, + read_self: &bool, + update_self: &bool, + delete_self: &bool, + create_other: &bool, + read_other: &bool, + update_other: &bool, + delete_other: &bool, + create_lower: &bool, + read_lower: &bool, + update_lower: &bool, + delete_lower: &bool, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Permission, + r#" + UPDATE "permission_role" SET "create_self" = $2, "read_self" = $3, "update_self" = $4, "delete_self" = $5, "create_other" = $6, "read_other" = $7, "update_other" = $8, "delete_other" = $9, "create_lower" = $10, "read_lower" = $11, "update_lower" = $12, "delete_lower" = $13 WHERE "role_id" = $1 + RETURNING * + "#, + role_id, + create_self, + read_self, + update_self, + delete_self, + create_other, + read_other, + update_other, + delete_other, + create_lower, + read_lower, + update_lower, + delete_lower, + ) + .fetch_one(database_connection) + .await +} + +pub async fn delete( + role_id: &i64, + database_connection: &Pool, +) -> Result { + sqlx::query_as!( + Permission, + r#" + DELETE FROM "permission_role" where "role_id" = $1 + RETURNING * + "#, + role_id + ) + .fetch_one(database_connection) + .await +} diff --git a/src/database/post.rs b/src/database/post.rs index 416d1e4..079203d 100644 --- a/src/database/post.rs +++ b/src/database/post.rs @@ -4,7 +4,7 @@ use sqlx::{Pool, Postgres}; use crate::feature::post::Post; pub async fn create( - poster_id: i64, + poster_id: &i64, post: &String, database_connection: &Pool, ) -> Result { @@ -39,7 +39,7 @@ pub async fn read( pub async fn update( creation_time: &DateTime, - poster_id: i64, + poster_id: &i64, post: &String, database_connection: &Pool, ) -> Result { @@ -85,7 +85,7 @@ pub async fn read_all(database_connection: &Pool) -> Result, } pub async fn read_all_for_user( - poster_id: i64, + poster_id: &i64, database_connection: &Pool, ) -> Result, sqlx::Error> { sqlx::query_as!( diff --git a/src/database/post_interaction.rs b/src/database/post_interaction.rs index aacf0f5..aea0fe8 100644 --- a/src/database/post_interaction.rs +++ b/src/database/post_interaction.rs @@ -5,8 +5,8 @@ use crate::feature::post_interaction::PostInteraction; pub async fn create( post_creation_time: &DateTime, - interaction_id: i64, - interactor_id: i64, + interaction_id: &i64, + interactor_id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( @@ -41,7 +41,7 @@ pub async fn read( pub async fn update( interaction_time: &DateTime, - interaction_id: i64, + interaction_id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( diff --git a/src/database/role.rs b/src/database/role.rs index eda704b..e045507 100644 --- a/src/database/role.rs +++ b/src/database/role.rs @@ -35,7 +35,7 @@ pub async fn read( } pub async fn update( - id: i64, + id: &i64, name: &String, database_connection: &Pool, ) -> Result { @@ -52,7 +52,7 @@ pub async fn update( .await } -pub async fn delete(id: i64, database_connection: &Pool) -> Result { +pub async fn delete(id: &i64, database_connection: &Pool) -> Result { sqlx::query_as!( Role, r#" diff --git a/src/database/user.rs b/src/database/user.rs index a57dd20..553015d 100644 --- a/src/database/user.rs +++ b/src/database/user.rs @@ -268,6 +268,23 @@ pub async fn read_all_id_for_surname( .collect::>()) } +pub async fn read_all_id_for_birth_date( + birth_date: &NaiveDate, + database_connection: &Pool, +) -> Result, sqlx::Error> { + Ok(sqlx::query!( + r#" + SELECT "id" FROM "user" WHERE "birth_date" = $1 + "#, + birth_date + ) + .fetch_all(database_connection) + .await? + .iter() + .map(|record| record.id) + .collect::>()) +} + pub async fn count_all(database_connection: &Pool) -> Result { sqlx::query!( r#" @@ -353,3 +370,21 @@ pub async fn count_all_for_surname( .try_into() .or(Ok(0)) } + +pub async fn count_all_for_birth_date( + birth_date: &NaiveDate, + database_connection: &Pool, +) -> Result { + sqlx::query!( + r#" + SELECT COUNT(id) FROM "user" WHERE "birth_date" = $1 + "#, + birth_date + ) + .fetch_one(database_connection) + .await? + .count + .map_or(0, |count| count) + .try_into() + .or(Ok(0)) +} diff --git a/src/feature.rs b/src/feature.rs index a0a90e8..5b8ba5b 100644 --- a/src/feature.rs +++ b/src/feature.rs @@ -1,6 +1,7 @@ pub mod comment; pub mod comment_interaction; pub mod interaction; +pub mod permission; pub mod post; pub mod post_interaction; pub mod role; diff --git a/src/feature/comment.rs b/src/feature/comment.rs index 9b22d44..2fff712 100644 --- a/src/feature/comment.rs +++ b/src/feature/comment.rs @@ -1,5 +1,7 @@ use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +#[derive(Debug, Serialize, Deserialize)] pub struct Comment { pub post_creation_time: DateTime, pub creation_time: DateTime, diff --git a/src/feature/comment_interaction.rs b/src/feature/comment_interaction.rs index d68fa98..fffc545 100644 --- a/src/feature/comment_interaction.rs +++ b/src/feature/comment_interaction.rs @@ -1,5 +1,7 @@ use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +#[derive(Debug, Serialize, Deserialize)] pub struct CommentInteraction { pub comment_creation_time: DateTime, pub interaction_id: i64, diff --git a/src/feature/permission.rs b/src/feature/permission.rs new file mode 100644 index 0000000..2f61a3c --- /dev/null +++ b/src/feature/permission.rs @@ -0,0 +1,18 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Permission { + pub role_id: i64, + pub create_self: bool, + pub read_self: bool, + pub update_self: bool, + pub delete_self: bool, + pub create_other: bool, + pub read_other: bool, + pub update_other: bool, + pub delete_other: bool, + pub create_lower: bool, + pub read_lower: bool, + pub update_lower: bool, + pub delete_lower: bool, +} diff --git a/src/feature/post_interaction.rs b/src/feature/post_interaction.rs index 5ac9156..8639fec 100644 --- a/src/feature/post_interaction.rs +++ b/src/feature/post_interaction.rs @@ -1,5 +1,7 @@ use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +#[derive(Debug, Serialize, Deserialize)] pub struct PostInteraction { pub post_creation_time: DateTime, pub interaction_id: i64,