diff --git a/migrations/20241204225128_create_role_table.up.sql b/migrations/20241204225128_create_role_table.up.sql index 2d4115f..0afdef8 100644 --- a/migrations/20241204225128_create_role_table.up.sql +++ b/migrations/20241204225128_create_role_table.up.sql @@ -4,9 +4,7 @@ CREATE TABLE IF NOT EXISTS "role"( name VARCHAR(16) NOT NULL UNIQUE ); -INSERT INTO "role"(id, name) VALUES (0, 'Ahmet Kaan Gümüş') ON CONFLICT(id) DO UPDATE SET "name" = 'Ahmet Kaan Gümüş'; -INSERT INTO "role"(id, name) VALUES (1, 'Founder') ON CONFLICT(id) DO UPDATE SET "name" = 'Founder'; -INSERT INTO "role"(id, name) VALUES (2, 'Maintainer') ON CONFLICT(id) DO UPDATE SET "name" = 'Founder'; -INSERT INTO "role"(id, name) VALUES (3, 'Admin') ON CONFLICT(id) DO UPDATE SET "name" = 'Admin'; +INSERT INTO "role"(id, name) VALUES (0, 'Builder') ON CONFLICT(id) DO UPDATE SET "name" = 'Builder'; +INSERT INTO "role"(id, name) VALUES (1, 'Admin') ON CONFLICT(id) DO UPDATE SET "name" = 'Admin'; INSERT INTO "role"(id, name) VALUES (10, 'Normal') ON CONFLICT(id) DO UPDATE SET "name" = 'Normal'; -INSERT INTO "role"(id, name) VALUES (-1, 'Banned') ON CONFLICT(id) DO UPDATE SET "name" = 'Banned'; \ No newline at end of file +INSERT INTO "role"(id, name) VALUES (-1, 'Banned') ON CONFLICT(id) DO UPDATE SET "name" = 'Banned'; diff --git a/migrations/20241213115604_permission.down.sql b/migrations/20241213115604_permission.down.sql deleted file mode 100644 index d2f607c..0000000 --- a/migrations/20241213115604_permission.down.sql +++ /dev/null @@ -1 +0,0 @@ --- Add down migration script here diff --git a/migrations/20241213115604_permission.up.sql b/migrations/20241213115604_permission.up.sql deleted file mode 100644 index 845db5e..0000000 --- a/migrations/20241213115604_permission.up.sql +++ /dev/null @@ -1,5 +0,0 @@ --- Add up migration script here -CREATE TABLE IF NOT EXISTS "permission"( - id BIGSERIAL PRIMARY KEY UNIQUE NOT NULL, - name VARCHAR(256) UNIQUE NOT NULL -); \ No newline at end of file diff --git a/migrations/20241213120203_role_permission.down.sql b/migrations/20241213120203_role_permission.down.sql deleted file mode 100644 index f73c9b3..0000000 --- a/migrations/20241213120203_role_permission.down.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Add down migration script here -DROP TABLE IF EXISTS "role_permission"; \ No newline at end of file diff --git a/migrations/20241213120203_role_permission.up.sql b/migrations/20241213120203_role_permission.up.sql deleted file mode 100644 index 1c6dd70..0000000 --- a/migrations/20241213120203_role_permission.up.sql +++ /dev/null @@ -1,6 +0,0 @@ --- Add up migration script here -CREATE TABLE IF NOT EXISTS "role_permission"( - role_id BIGSERIAL NOT NULL REFERENCES "role"(id), - permission_id BIGSERIAL NOT NULL REFERENCES "permission"(id), - PRIMARY KEY (role_id, permission_id) -); \ No newline at end of file diff --git a/migrations/20241223234406_routing.down.sql b/migrations/20241223234406_routing.down.sql deleted file mode 100644 index a510791..0000000 --- a/migrations/20241223234406_routing.down.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Add down migration script here -DROP TABLE IF EXISTS "routing"; \ No newline at end of file diff --git a/migrations/20241223234406_routing.up.sql b/migrations/20241223234406_routing.up.sql deleted file mode 100644 index ddd20df..0000000 --- a/migrations/20241223234406_routing.up.sql +++ /dev/null @@ -1,5 +0,0 @@ --- Add up migration script here -CREATE TABLE IF NOT EXISTS "routing"( - id BIGSERIAL PRIMARY KEY UNIQUE NOT NULL, - endpoint VARCHAR(255) UNIQUE NOT NULL -); \ No newline at end of file diff --git a/migrations/20241224112057_routing_permission.down.sql b/migrations/20241224112057_routing_permission.down.sql deleted file mode 100644 index 1a41b63..0000000 --- a/migrations/20241224112057_routing_permission.down.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Add down migration script here -DROP TABLE IF EXISTS "routing_permission"; \ No newline at end of file diff --git a/migrations/20241224112057_routing_permission.up.sql b/migrations/20241224112057_routing_permission.up.sql deleted file mode 100644 index 882fd7c..0000000 --- a/migrations/20241224112057_routing_permission.up.sql +++ /dev/null @@ -1,6 +0,0 @@ --- Add up migration script here -CREATE TABLE IF NOT EXISTS "routing_permission"( - routing_id BIGSERIAL NOT NULL REFERENCES "routing"(id), - permission_id BIGSERIAL NOT NULL REFERENCES "permission"(id), - PRIMARY KEY (routing_id, permission_id) -); \ No newline at end of file diff --git a/src/database.rs b/src/database.rs index 5add61b..d42c87d 100644 --- a/src/database.rs +++ b/src/database.rs @@ -3,13 +3,9 @@ pub mod comment_interaction; pub mod contact; pub mod interaction; pub mod login; -pub mod permission; pub mod post; pub mod post_interaction; pub mod role; -pub mod role_permission; -pub mod routing; -pub mod routing_permission; pub mod user; pub mod user_contact; diff --git a/src/database/permission.rs b/src/database/permission.rs deleted file mode 100644 index 7db0c44..0000000 --- a/src/database/permission.rs +++ /dev/null @@ -1,82 +0,0 @@ -use sqlx::{Pool, Postgres}; - -use crate::feature::permission::Permission; - -pub async fn create( - name: &String, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - Permission, - r#" - INSERT INTO "permission"(name) - VALUES ($1) - RETURNING * - "#, - name, - ) - .fetch_one(database_connection) - .await -} - -pub async fn read( - id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - Permission, - r#" - SELECT * FROM "permission" WHERE "id" = $1 - "#, - id, - ) - .fetch_one(database_connection) - .await -} - -pub async fn update( - id: &i64, - name: &String, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - Permission, - r#" - UPDATE "permission" SET "name" = $2 WHERE "id" = $1 - RETURNING * - "#, - id, - name, - ) - .fetch_one(database_connection) - .await -} - -pub async fn delete( - id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - Permission, - r#" - DELETE FROM "permission" WHERE "id" = $1 - RETURNING * - "#, - id - ) - .fetch_one(database_connection) - .await -} - -pub async fn read_all( - database_connection: &Pool, -) -> Result, sqlx::Error> { - sqlx::query_as!( - Permission, - r#" - SELECT * FROM "permission" - "#, - ) - .fetch_all(database_connection) - .await -} diff --git a/src/database/role_permission.rs b/src/database/role_permission.rs deleted file mode 100644 index e1f8a6f..0000000 --- a/src/database/role_permission.rs +++ /dev/null @@ -1,106 +0,0 @@ -use sqlx::{Pool, Postgres}; - -use crate::feature::role_permission::RolePermission; - -pub async fn create( - role_id: &i64, - permission_id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - RolePermission, - r#" - INSERT INTO "role_permission"(role_id, permission_id) - VALUES ($1, $2) - RETURNING * - "#, - role_id, - permission_id - ) - .fetch_one(database_connection) - .await -} - -pub async fn read( - role_id: &i64, - permission_id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - RolePermission, - r#" - SELECT * FROM "role_permission" WHERE "role_id" = $1 AND "permission_id" = $2 - "#, - role_id, - permission_id, - ) - .fetch_one(database_connection) - .await -} - -pub async fn update( - role_id: &i64, - permission_id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - RolePermission, - r#" - UPDATE "role_permission" SET "permission_id" = $2 WHERE "role_id" = $1 - RETURNING * - "#, - role_id, - permission_id, - ) - .fetch_one(database_connection) - .await -} - -pub async fn delete( - role_id: &i64, - permission_id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - RolePermission, - r#" - DELETE FROM "role_permission" WHERE "role_id" = $1 AND "permission_id" = $2 - RETURNING * - "#, - role_id, - permission_id, - ) - .fetch_one(database_connection) - .await -} - -pub async fn read_all_for_role( - role_id: &i64, - database_connection: &Pool, -) -> Result, sqlx::Error> { - sqlx::query_as!( - RolePermission, - r#" - SELECT * FROM "role_permission" WHERE "role_id" = $1 - "#, - role_id - ) - .fetch_all(database_connection) - .await -} - -pub async fn delete_all_for_role( - role_id: &i64, - database_connection: &Pool, -) -> Result, sqlx::Error> { - sqlx::query_as!( - RolePermission, - r#" - DELETE FROM "role_permission" WHERE "role_id" = $1 - RETURNING * - "#, - role_id, - ) - .fetch_all(database_connection) - .await -} diff --git a/src/database/routing.rs b/src/database/routing.rs deleted file mode 100644 index ae0bc40..0000000 --- a/src/database/routing.rs +++ /dev/null @@ -1,89 +0,0 @@ -use sqlx::{Pool, Postgres}; - -use crate::feature::routing::Routing; - -pub async fn create( - endpoint: &String, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - Routing, - r#" - INSERT INTO "routing"(endpoint) - VALUES ($1) - RETURNING * - "#, - endpoint, - ) - .fetch_one(database_connection) - .await -} - -pub async fn read(id: &i64, database_connection: &Pool) -> Result { - sqlx::query_as!( - Routing, - r#" - SELECT * FROM "routing" WHERE "id" = $1 - "#, - id - ) - .fetch_one(database_connection) - .await -} - -pub async fn update( - id: &i64, - endpoint: &String, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - Routing, - r#" - UPDATE "routing" SET "endpoint" = $2 WHERE "id" = $1 - RETURNING * - "#, - id, - endpoint, - ) - .fetch_one(database_connection) - .await -} - -pub async fn delete( - id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - Routing, - r#" - DELETE FROM "routing" WHERE "id" = $1 - RETURNING * - "#, - id - ) - .fetch_one(database_connection) - .await -} - -pub async fn read_all(database_connection: &Pool) -> Result, sqlx::Error> { - sqlx::query_as!( - Routing, - r#" - SELECT * FROM "routing" - "#, - ) - .fetch_all(database_connection) - .await -} - -pub async fn delete_all(database_connection: &Pool) -> Result, sqlx::Error> { - sqlx::query_as!( - Routing, - r#" - DELETE FROM "routing" - RETURNING * - "#, - ) - .fetch_all(database_connection) - .await -} diff --git a/src/database/routing_permission.rs b/src/database/routing_permission.rs deleted file mode 100644 index 3d6de10..0000000 --- a/src/database/routing_permission.rs +++ /dev/null @@ -1,106 +0,0 @@ -use sqlx::{Pool, Postgres}; - -use crate::feature::routing_permission::RoutingPermission; - -pub async fn create( - routing_id: &i64, - permission_id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - RoutingPermission, - r#" - INSERT INTO "routing_permission"(routing_id, permission_id) - VALUES ($1, $2) - RETURNING * - "#, - routing_id, - permission_id - ) - .fetch_one(database_connection) - .await -} - -pub async fn read( - routing_id: &i64, - permission_id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - RoutingPermission, - r#" - SELECT * FROM "routing_permission" WHERE "routing_id" = $1 AND "permission_id" = $2 - "#, - routing_id, - permission_id, - ) - .fetch_one(database_connection) - .await -} - -pub async fn update( - routing_id: &i64, - permission_id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - RoutingPermission, - r#" - UPDATE "routing_permission" SET "permission_id" = $2 WHERE "routing_id" = $1 - RETURNING * - "#, - routing_id, - permission_id, - ) - .fetch_one(database_connection) - .await -} - -pub async fn delete( - routing_id: &i64, - permission_id: &i64, - database_connection: &Pool, -) -> Result { - sqlx::query_as!( - RoutingPermission, - r#" - DELETE FROM "routing_permission" WHERE "routing_id" = $1 AND "permission_id" = $2 - RETURNING * - "#, - routing_id, - permission_id, - ) - .fetch_one(database_connection) - .await -} - -pub async fn read_all_for_routing( - routing_id: &i64, - database_connection: &Pool, -) -> Result, sqlx::Error> { - sqlx::query_as!( - RoutingPermission, - r#" - SELECT * FROM "routing_permission" WHERE "routing_id" = $1 - "#, - routing_id - ) - .fetch_all(database_connection) - .await -} - -pub async fn delete_all_for_routing( - routing_id: &i64, - database_connection: &Pool, -) -> Result, sqlx::Error> { - sqlx::query_as!( - RoutingPermission, - r#" - DELETE FROM "routing_permission" WHERE "routing_id" = $1 - RETURNING * - "#, - routing_id, - ) - .fetch_all(database_connection) - .await -} diff --git a/src/feature.rs b/src/feature.rs index 549472e..0446ee6 100644 --- a/src/feature.rs +++ b/src/feature.rs @@ -4,12 +4,8 @@ pub mod comment_interaction; pub mod contact; pub mod interaction; pub mod login; -pub mod permission; pub mod post; pub mod post_interaction; pub mod role; -pub mod role_permission; -pub mod routing; -pub mod routing_permission; pub mod user; pub mod user_contact; diff --git a/src/feature/permission.rs b/src/feature/permission.rs deleted file mode 100644 index c038281..0000000 --- a/src/feature/permission.rs +++ /dev/null @@ -1,47 +0,0 @@ -use serde::{Deserialize, Serialize}; -use sqlx::{Pool, Postgres}; - -use crate::database::permission; - -#[derive(Debug, Serialize, Deserialize)] -pub struct Permission { - pub id: i64, - pub name: String, -} - -impl Permission { - pub async fn create( - name: &String, - database_connection: &Pool, - ) -> Result { - permission::create(name, database_connection).await - } - - pub async fn read( - id: &i64, - database_connection: &Pool, - ) -> Result { - permission::read(id, database_connection).await - } - - pub async fn update( - id: &i64, - name: &String, - database_connection: &Pool, - ) -> Result { - permission::update(id, name, database_connection).await - } - - pub async fn delete( - id: &i64, - database_connection: &Pool, - ) -> Result { - permission::delete(id, database_connection).await - } - - pub async fn read_all( - database_connection: &Pool, - ) -> Result, sqlx::Error> { - permission::read_all(database_connection).await - } -} diff --git a/src/feature/role_permission.rs b/src/feature/role_permission.rs deleted file mode 100644 index c192209..0000000 --- a/src/feature/role_permission.rs +++ /dev/null @@ -1,57 +0,0 @@ -use serde::{Deserialize, Serialize}; -use sqlx::{Pool, Postgres}; - -use crate::database::role_permission; - -#[derive(Debug, Serialize, Deserialize)] -pub struct RolePermission { - pub role_id: i64, - pub permission_id: i64, -} - -impl RolePermission { - pub async fn create( - role_id: &i64, - permission_id: &i64, - database_connection: &Pool, - ) -> Result { - role_permission::create(role_id, permission_id, database_connection).await - } - - pub async fn read( - role_id: &i64, - permission_id: &i64, - database_connection: &Pool, - ) -> Result { - role_permission::read(role_id, permission_id, database_connection).await - } - - pub async fn update( - role_id: &i64, - permission_id: &i64, - database_connection: &Pool, - ) -> Result { - role_permission::update(role_id, permission_id, database_connection).await - } - pub async fn delete( - role_id: &i64, - permission_id: &i64, - database_connection: &Pool, - ) -> Result { - role_permission::delete(role_id, permission_id, database_connection).await - } - - pub async fn read_all_for_role( - role_id: &i64, - database_connection: &Pool, - ) -> Result, sqlx::Error> { - role_permission::read_all_for_role(role_id, database_connection).await - } - - pub async fn delete_all_for_role( - role_id: &i64, - database_connection: &Pool, - ) -> Result, sqlx::Error> { - role_permission::delete_all_for_role(role_id, database_connection).await - } -} diff --git a/src/feature/routing.rs b/src/feature/routing.rs deleted file mode 100644 index 80938bd..0000000 --- a/src/feature/routing.rs +++ /dev/null @@ -1,53 +0,0 @@ -use serde::{Deserialize, Serialize}; -use sqlx::{Pool, Postgres}; - -use crate::database::routing; - -#[derive(Debug, Serialize, Deserialize)] -pub struct Routing { - pub id: i64, - pub endpoint: String, -} - -impl Routing { - pub async fn create( - endpoint: &String, - database_connection: &Pool, - ) -> Result { - routing::create(endpoint, database_connection).await - } - - pub async fn read( - id: &i64, - database_connection: &Pool, - ) -> Result { - routing::read(id, database_connection).await - } - - pub async fn update( - id: &i64, - endpoint: &String, - database_connection: &Pool, - ) -> Result { - routing::update(id, endpoint, database_connection).await - } - - pub async fn delete( - id: &i64, - database_connection: &Pool, - ) -> Result { - routing::delete(id, database_connection).await - } - - pub async fn read_all( - database_connection: &Pool, - ) -> Result, sqlx::Error> { - routing::read_all(database_connection).await - } - - pub async fn delete_all( - database_connection: &Pool, - ) -> Result, sqlx::Error> { - routing::delete_all(database_connection).await - } -} diff --git a/src/feature/routing_permission.rs b/src/feature/routing_permission.rs deleted file mode 100644 index a19afee..0000000 --- a/src/feature/routing_permission.rs +++ /dev/null @@ -1,57 +0,0 @@ -use serde::{Deserialize, Serialize}; -use sqlx::{Pool, Postgres}; - -use crate::database::routing_permission; - -#[derive(Debug, Serialize, Deserialize)] -pub struct RoutingPermission { - pub routing_id: i64, - pub permission_id: i64, -} - -impl RoutingPermission { - pub async fn create( - routing_id: &i64, - permission_id: &i64, - database_connection: &Pool, - ) -> Result { - routing_permission::create(routing_id, permission_id, database_connection).await - } - - pub async fn read( - routing_id: &i64, - permission_id: &i64, - database_connection: &Pool, - ) -> Result { - routing_permission::read(routing_id, permission_id, database_connection).await - } - - pub async fn update( - routing_id: &i64, - permission_id: &i64, - database_connection: &Pool, - ) -> Result { - routing_permission::update(routing_id, permission_id, database_connection).await - } - pub async fn delete( - routing_id: &i64, - permission_id: &i64, - database_connection: &Pool, - ) -> Result { - routing_permission::delete(routing_id, permission_id, database_connection).await - } - - pub async fn read_all_for_routing( - routing_id: &i64, - database_connection: &Pool, - ) -> Result, sqlx::Error> { - routing_permission::read_all_for_routing(routing_id, database_connection).await - } - - pub async fn delete_all_for_routing( - routing_id: &i64, - database_connection: &Pool, - ) -> Result, sqlx::Error> { - routing_permission::delete_all_for_routing(routing_id, database_connection).await - } -} diff --git a/src/routing.rs b/src/routing.rs index 68e8847..4ae2c6d 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -3,13 +3,9 @@ pub mod comment_interaction; pub mod contact; pub mod interaction; pub mod login; -pub mod permission; pub mod post; pub mod post_interaction; pub mod role; -pub mod role_permission; -pub mod routing; -pub mod routing_permission; pub mod user; pub mod user_contact; @@ -50,10 +46,6 @@ pub async fn route(concurrency_limit: &usize, State(app_state): State) "/comment_interactions", comment_interaction::route(axum::extract::State(app_state.clone())), ) - .nest( - "/role_permissions", - role_permission::route(axum::extract::State(app_state.clone())), - ) .nest( "/contacts", contact::route(axum::extract::State(app_state.clone())), @@ -62,14 +54,6 @@ pub async fn route(concurrency_limit: &usize, State(app_state): State) "/user_contacts", user_contact::route(axum::extract::State(app_state.clone())), ) - .nest( - "/routings", - routing::route(axum::extract::State(app_state.clone())), - ) - .nest( - "/routing_permissions", - routing_permission::route(axum::extract::State(app_state.clone())), - ) .layer(CorsLayer::permissive()) .layer(ConcurrencyLimitLayer::new(*concurrency_limit)) .with_state(app_state) diff --git a/src/routing/permission.rs b/src/routing/permission.rs deleted file mode 100644 index 4c5026f..0000000 --- a/src/routing/permission.rs +++ /dev/null @@ -1,93 +0,0 @@ -use axum::{ - extract::{Path, State}, - http::StatusCode, - response::IntoResponse, - routing::{delete, get, patch, post}, - Json, Router, -}; -use serde::{Deserialize, Serialize}; - -use crate::{feature::permission::Permission, AppState}; - -#[derive(Debug, Serialize, Deserialize)] -struct CreatePermission { - name: String, -} - -#[derive(Debug, Serialize, Deserialize)] -struct UpdatePermission { - id: i64, - name: String, -} - -pub fn route(State(app_state): State) -> Router { - Router::new() - .route("/", post(create)) - .route("/:id", get(read)) - .route("/", patch(update)) - .route("/:id", delete(delete_)) - .route("/", get(read_all)) - .with_state(app_state) -} - -async fn create( - State(app_state): State, - Json(create_permission): Json, -) -> impl IntoResponse { - match Permission::create(&create_permission.name, &app_state.database_connection).await { - Ok(permission) => (StatusCode::CREATED, Json(serde_json::json!(permission))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn read(State(app_state): State, Path(id): Path) -> impl IntoResponse { - match Permission::read(&id, &app_state.database_connection).await { - Ok(permission) => (StatusCode::OK, Json(serde_json::json!(permission))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn update( - State(app_state): State, - Json(update_permission): Json, -) -> impl IntoResponse { - match Permission::update( - &update_permission.id, - &update_permission.name, - &app_state.database_connection, - ) - .await - { - Ok(permission) => (StatusCode::ACCEPTED, Json(serde_json::json!(permission))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn delete_(State(app_state): State, Path(id): Path) -> impl IntoResponse { - match Permission::delete(&id, &app_state.database_connection).await { - Ok(permission) => (StatusCode::NO_CONTENT, Json(serde_json::json!(permission))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn read_all(State(app_state): State) -> impl IntoResponse { - match Permission::read_all(&app_state.database_connection).await { - Ok(permissions) => (StatusCode::OK, Json(serde_json::json!(permissions))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} diff --git a/src/routing/role_permission.rs b/src/routing/role_permission.rs deleted file mode 100644 index 13ac4f4..0000000 --- a/src/routing/role_permission.rs +++ /dev/null @@ -1,135 +0,0 @@ -use axum::{ - extract::{Path, State}, - http::StatusCode, - response::IntoResponse, - routing::{delete, get, patch, post}, - Json, Router, -}; -use serde::{Deserialize, Serialize}; - -use crate::{feature::role_permission::RolePermission, AppState}; - -#[derive(Debug, Serialize, Deserialize)] -struct CreateRolePermission { - pub role_id: i64, - pub permission_id: i64, -} - -#[derive(Debug, Serialize, Deserialize)] -struct UpdateRolePermission { - pub role_id: i64, - pub permission_id: i64, -} - -pub fn route(State(app_state): State) -> Router { - Router::new() - .route("/", post(create)) - .route("/roles/:role_id/permissions/:permission_id", get(read)) - .route("/", patch(update)) - .route( - "/roles/:role_id/permissions/:permission_id", - delete(delete_), - ) - .route("/roles/:role_id", get(read_all_for_role)) - .route("/roles/:role_id", delete(delete_all_for_role)) - .with_state(app_state) -} - -async fn create( - State(app_state): State, - Json(create_role_permission): Json, -) -> impl IntoResponse { - match RolePermission::create( - &create_role_permission.role_id, - &create_role_permission.permission_id, - &app_state.database_connection, - ) - .await - { - Ok(role_permission) => ( - StatusCode::CREATED, - Json(serde_json::json!(role_permission)), - ), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn read( - State(app_state): State, - Path((role_id, permission_id)): Path<(i64, i64)>, -) -> impl IntoResponse { - match RolePermission::read(&role_id, &permission_id, &app_state.database_connection).await { - Ok(role_permission) => (StatusCode::OK, Json(serde_json::json!(role_permission))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn update( - State(app_state): State, - Json(update_role): Json, -) -> impl IntoResponse { - match RolePermission::update( - &update_role.role_id, - &update_role.permission_id, - &app_state.database_connection, - ) - .await - { - Ok(role_permission) => ( - StatusCode::ACCEPTED, - Json(serde_json::json!(role_permission)), - ), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn delete_( - State(app_state): State, - Path((role_id, permission_id)): Path<(i64, i64)>, -) -> impl IntoResponse { - match RolePermission::delete(&role_id, &permission_id, &app_state.database_connection).await { - Ok(role_permission) => ( - StatusCode::NO_CONTENT, - Json(serde_json::json!(role_permission)), - ), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn read_all_for_role( - State(app_state): State, - Path(role_id): Path, -) -> impl IntoResponse { - match RolePermission::read_all_for_role(&role_id, &app_state.database_connection).await { - Ok(role_permissions) => (StatusCode::OK, Json(serde_json::json!(role_permissions))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn delete_all_for_role( - State(app_state): State, - Path(role_id): Path, -) -> impl IntoResponse { - match RolePermission::delete_all_for_role(&role_id, &app_state.database_connection).await { - Ok(role_permissions) => (StatusCode::OK, Json(serde_json::json!(role_permissions))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} diff --git a/src/routing/routing.rs b/src/routing/routing.rs deleted file mode 100644 index 2bae7e4..0000000 --- a/src/routing/routing.rs +++ /dev/null @@ -1,36 +0,0 @@ -use axum::{ - extract::{Path, State}, - http::StatusCode, - response::IntoResponse, - routing::get, - Json, Router, -}; - -use crate::{feature::routing::Routing, AppState}; - -pub fn route(State(app_state): State) -> Router { - Router::new() - .route("/:id", get(read)) - .route("/", get(read_all)) - .with_state(app_state) -} - -async fn read(State(app_state): State, Path(id): Path) -> impl IntoResponse { - match Routing::read(&id, &app_state.database_connection).await { - Ok(routing) => (StatusCode::OK, Json(serde_json::json!(routing))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn read_all(State(app_state): State) -> impl IntoResponse { - match Routing::read_all(&app_state.database_connection).await { - Ok(routings) => (StatusCode::OK, Json(serde_json::json!(routings))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} diff --git a/src/routing/routing_permission.rs b/src/routing/routing_permission.rs deleted file mode 100644 index 017a9ab..0000000 --- a/src/routing/routing_permission.rs +++ /dev/null @@ -1,144 +0,0 @@ -use axum::{ - extract::{Path, State}, - http::StatusCode, - response::IntoResponse, - routing::{delete, get, patch, post}, - Json, Router, -}; -use serde::{Deserialize, Serialize}; - -use crate::{feature::routing_permission::RoutingPermission, AppState}; - -#[derive(Debug, Serialize, Deserialize)] -struct CreateRoutingPermission { - pub routing_id: i64, - pub permission_id: i64, -} - -#[derive(Debug, Serialize, Deserialize)] -struct UpdateRoutingPermission { - pub routing_id: i64, - pub permission_id: i64, -} - -pub fn route(State(app_state): State) -> Router { - Router::new() - .route("/", post(create)) - .route( - "/routings/:routing_id/permissions/:permission_id", - get(read), - ) - .route("/", patch(update)) - .route( - "/routings/:routing_id/permissions/:permission_id", - delete(delete_), - ) - .route("/routings/:routing_id", get(read_all_for_routing)) - .route("/routings/:routing_id", delete(delete_all_for_routing)) - .with_state(app_state) -} - -async fn create( - State(app_state): State, - Json(create_role_permission): Json, -) -> impl IntoResponse { - match RoutingPermission::create( - &create_role_permission.routing_id, - &create_role_permission.permission_id, - &app_state.database_connection, - ) - .await - { - Ok(role_permission) => ( - StatusCode::CREATED, - Json(serde_json::json!(role_permission)), - ), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn read( - State(app_state): State, - Path((routing_id, permission_id)): Path<(i64, i64)>, -) -> impl IntoResponse { - match RoutingPermission::read(&routing_id, &permission_id, &app_state.database_connection).await - { - Ok(role_permission) => (StatusCode::OK, Json(serde_json::json!(role_permission))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn update( - State(app_state): State, - Json(update_role): Json, -) -> impl IntoResponse { - match RoutingPermission::update( - &update_role.routing_id, - &update_role.permission_id, - &app_state.database_connection, - ) - .await - { - Ok(role_permission) => ( - StatusCode::ACCEPTED, - Json(serde_json::json!(role_permission)), - ), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn delete_( - State(app_state): State, - Path((routing_id, permission_id)): Path<(i64, i64)>, -) -> impl IntoResponse { - match RoutingPermission::delete(&routing_id, &permission_id, &app_state.database_connection) - .await - { - Ok(role_permission) => ( - StatusCode::NO_CONTENT, - Json(serde_json::json!(role_permission)), - ), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn read_all_for_routing( - State(app_state): State, - Path(routing_id): Path, -) -> impl IntoResponse { - match RoutingPermission::read_all_for_routing(&routing_id, &app_state.database_connection).await - { - Ok(role_permissions) => (StatusCode::OK, Json(serde_json::json!(role_permissions))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -} - -async fn delete_all_for_routing( - State(app_state): State, - Path(routing_id): Path, -) -> impl IntoResponse { - match RoutingPermission::delete_all_for_routing(&routing_id, &app_state.database_connection) - .await - { - Ok(role_permissions) => (StatusCode::OK, Json(serde_json::json!(role_permissions))), - Err(err_val) => ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!(err_val.to_string())), - ), - } -}