This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-09 15:18:32 +03:00
parent c6d3246f71
commit a93ed4e9aa
14 changed files with 228 additions and 19 deletions

View file

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

View file

@ -4,8 +4,8 @@ use sqlx::{Pool, Postgres};
use crate::feature::comment::Comment;
pub async fn create(
post_creation_time: DateTime<Utc>,
commenter_id: i64,
post_creation_time: &DateTime<Utc>,
commenter_id: &i64,
comment: &String,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
@ -25,7 +25,7 @@ pub async fn create(
}
pub async fn read(
creation_time: DateTime<Utc>,
creation_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
sqlx::query_as!(
@ -40,7 +40,7 @@ pub async fn read(
}
pub async fn update(
creation_time: DateTime<Utc>,
creation_time: &DateTime<Utc>,
comment: &String,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
@ -58,7 +58,7 @@ pub async fn update(
}
pub async fn delete(
creation_time: DateTime<Utc>,
creation_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<Comment, sqlx::Error> {
sqlx::query_as!(
@ -74,7 +74,7 @@ pub async fn delete(
}
pub async fn read_all_for_post(
post_creation_time: DateTime<Utc>,
post_creation_time: &DateTime<Utc>,
database_connection: &Pool<Postgres>,
) -> Result<Vec<Comment>, sqlx::Error> {
sqlx::query_as!(

View file

@ -5,8 +5,8 @@ use crate::feature::comment_interaction::CommentInteraction;
pub async fn create(
comment_creation_time: &DateTime<Utc>,
interaction_id: i64,
interactor_id: i64,
interaction_id: &i64,
interactor_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<CommentInteraction, sqlx::Error> {
sqlx::query_as!(
@ -41,7 +41,7 @@ pub async fn read(
pub async fn update(
interaction_time: &DateTime<Utc>,
interaction_id: i64,
interaction_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<CommentInteraction, sqlx::Error> {
sqlx::query_as!(

View file

@ -35,7 +35,7 @@ pub async fn read(
}
pub async fn update(
id: i64,
id: &i64,
name: &String,
database_connection: &Pool<Postgres>,
) -> Result<Interaction, sqlx::Error> {
@ -53,7 +53,7 @@ pub async fn update(
}
pub async fn delete(
id: i64,
id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<Interaction, sqlx::Error> {
sqlx::query_as!(

148
src/database/permission.rs Normal file
View file

@ -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<Postgres>,
) -> Result<Permission, sqlx::Error> {
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<Postgres>,
) -> Result<Permission, sqlx::Error> {
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<Postgres>,
) -> Result<Permission, sqlx::Error> {
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<Postgres>,
) -> Result<Permission, sqlx::Error> {
sqlx::query_as!(
Permission,
r#"
DELETE FROM "permission_role" where "role_id" = $1
RETURNING *
"#,
role_id
)
.fetch_one(database_connection)
.await
}

View file

@ -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<Postgres>,
) -> Result<Post, sqlx::Error> {
@ -39,7 +39,7 @@ pub async fn read(
pub async fn update(
creation_time: &DateTime<Utc>,
poster_id: i64,
poster_id: &i64,
post: &String,
database_connection: &Pool<Postgres>,
) -> Result<Post, sqlx::Error> {
@ -85,7 +85,7 @@ pub async fn read_all(database_connection: &Pool<Postgres>) -> Result<Vec<Post>,
}
pub async fn read_all_for_user(
poster_id: i64,
poster_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<Vec<Post>, sqlx::Error> {
sqlx::query_as!(

View file

@ -5,8 +5,8 @@ use crate::feature::post_interaction::PostInteraction;
pub async fn create(
post_creation_time: &DateTime<Utc>,
interaction_id: i64,
interactor_id: i64,
interaction_id: &i64,
interactor_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<PostInteraction, sqlx::Error> {
sqlx::query_as!(
@ -41,7 +41,7 @@ pub async fn read(
pub async fn update(
interaction_time: &DateTime<Utc>,
interaction_id: i64,
interaction_id: &i64,
database_connection: &Pool<Postgres>,
) -> Result<PostInteraction, sqlx::Error> {
sqlx::query_as!(

View file

@ -35,7 +35,7 @@ pub async fn read(
}
pub async fn update(
id: i64,
id: &i64,
name: &String,
database_connection: &Pool<Postgres>,
) -> Result<Role, sqlx::Error> {
@ -52,7 +52,7 @@ pub async fn update(
.await
}
pub async fn delete(id: i64, database_connection: &Pool<Postgres>) -> Result<Role, sqlx::Error> {
pub async fn delete(id: &i64, database_connection: &Pool<Postgres>) -> Result<Role, sqlx::Error> {
sqlx::query_as!(
Role,
r#"

View file

@ -268,6 +268,23 @@ pub async fn read_all_id_for_surname(
.collect::<Vec<i64>>())
}
pub async fn read_all_id_for_birth_date(
birth_date: &NaiveDate,
database_connection: &Pool<Postgres>,
) -> Result<Vec<i64>, 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::<Vec<i64>>())
}
pub async fn count_all(database_connection: &Pool<Postgres>) -> Result<u64, sqlx::Error> {
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<Postgres>,
) -> Result<u64, sqlx::Error> {
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))
}

View file

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

View file

@ -1,5 +1,7 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Comment {
pub post_creation_time: DateTime<Utc>,
pub creation_time: DateTime<Utc>,

View file

@ -1,5 +1,7 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct CommentInteraction {
pub comment_creation_time: DateTime<Utc>,
pub interaction_id: i64,

18
src/feature/permission.rs Normal file
View file

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

View file

@ -1,5 +1,7 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct PostInteraction {
pub post_creation_time: DateTime<Utc>,
pub interaction_id: i64,