feat: ✨ permission routing
This commit is contained in:
parent
04b39ba9c9
commit
f1dce5a765
4 changed files with 152 additions and 24 deletions
|
@ -3,85 +3,79 @@ use sqlx::{Pool, Postgres};
|
|||
use crate::feature::permission::Permission;
|
||||
|
||||
pub async fn create(
|
||||
role_id: &i64,
|
||||
permission_id: &i64,
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Permission, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Permission,
|
||||
r#"
|
||||
INSERT INTO "role_permission"(role_id, permission_id)
|
||||
VALUES ($1, $2)
|
||||
INSERT INTO "permission"(name)
|
||||
VALUES ($1)
|
||||
RETURNING *
|
||||
"#,
|
||||
role_id,
|
||||
permission_id
|
||||
name,
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read(
|
||||
role_id: &i64,
|
||||
permission_id: &i64,
|
||||
id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Permission, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Permission,
|
||||
r#"
|
||||
SELECT * FROM "role_permission" WHERE "role_id" = $1 AND "permission_id" = $2
|
||||
SELECT * FROM "permission" WHERE "id" = $1
|
||||
"#,
|
||||
role_id,
|
||||
permission_id
|
||||
id,
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
role_id: &i64,
|
||||
permission_id: &i64,
|
||||
id: &i64,
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Permission, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Permission,
|
||||
r#"
|
||||
UPDATE "role_permission" SET "permission_id" = $2 WHERE "role_id" = $1
|
||||
UPDATE "permission" SET "name" = $2 WHERE "id" = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
role_id,
|
||||
permission_id
|
||||
id,
|
||||
name,
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
role_id: &i64,
|
||||
id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Permission, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Permission,
|
||||
r#"
|
||||
DELETE FROM "role_permission" WHERE "role_id" = $1
|
||||
DELETE FROM "permission" WHERE "id" = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
role_id
|
||||
id
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read_all(
|
||||
role_id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Vec<Permission>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Permission,
|
||||
r#"
|
||||
SELECT * FROM "role_permission" WHERE "role_id" = $1
|
||||
SELECT * FROM "permission"
|
||||
"#,
|
||||
role_id,
|
||||
)
|
||||
.fetch_all(database_connection)
|
||||
.await
|
||||
|
|
|
@ -1,7 +1,47 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::database::permission;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Permission {
|
||||
pub role_id: i64,
|
||||
pub permission_id: i64,
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Permission {
|
||||
pub async fn create(
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Permission, sqlx::Error> {
|
||||
permission::create(name, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn read(
|
||||
id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Permission, sqlx::Error> {
|
||||
permission::read(id, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
id: &i64,
|
||||
name: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Permission, sqlx::Error> {
|
||||
permission::update(id, name, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
id: &i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Permission, sqlx::Error> {
|
||||
permission::delete(id, database_connection).await
|
||||
}
|
||||
|
||||
pub async fn read_all(
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<Vec<Permission>, sqlx::Error> {
|
||||
permission::read_all(database_connection).await
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
93
src/routing/permission.rs
Normal file
93
src/routing/permission.rs
Normal file
|
@ -0,0 +1,93 @@
|
|||
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<AppState>) -> Router<AppState> {
|
||||
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<AppState>,
|
||||
Json(create_permission): Json<CreatePermission>,
|
||||
) -> 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<AppState>, Path(id): Path<i64>) -> 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<AppState>,
|
||||
Json(update_permission): Json<UpdatePermission>,
|
||||
) -> 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<AppState>, Path(id): Path<i64>) -> 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<AppState>) -> 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())),
|
||||
),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue