Skip to content

Commit

Permalink
feat:added methods for delte and update role;added handler for role a…
Browse files Browse the repository at this point in the history
…nd added some routes into main regarding role
  • Loading branch information
Alex_Miao_WSL committed Jul 16, 2024
1 parent 4726958 commit 11e8f01
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/server/src/handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod auth;
pub mod organisation;
pub mod role;
45 changes: 45 additions & 0 deletions backend/server/src/handler/role.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::models;
use crate::models::app::AppState;
use crate::models::auth::SuperUser;
use crate::models::auth::{AuthUser, OrganisationAdmin};
use crate::models::error::ChaosError;
use crate::models::role::{Role, RoleUpdate};
use crate::models::transaction::DBTransaction;
use crate::service;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;

pub struct RoleHandler;

impl RoleHandler {
pub async fn get(
State(state): State<AppState>,
Path(id): Path<i32>,
_user: AuthUser,
) -> Result<impl IntoResponse, ChaosError> {
let role = Role::get(id, &state.db).await?;
Ok((StatusCode::OK, Json(role)))
}

pub async fn delete(
State(state): State<AppState>,
Path(id): Path<i32>,
_admin: OrganisationAdmin,
) -> Result<impl IntoResponse, ChaosError> {
Role::delete(id, &state.db).await?;
Ok((StatusCode::OK, "Successfully deleted role"))
}

pub async fn update(
State(state): State<AppState>,
Path(id): Path<i32>,
_admin: OrganisationAdmin,
mut transaction: DBTransaction<'_>,
Json(data): Json<RoleUpdate>,
) -> Result<impl IntoResponse, ChaosError> {
Role::update(id, data, &mut transaction.tx,).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, "Successfully updated role"))
}
}
7 changes: 7 additions & 0 deletions backend/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::models::storage::Storage;
use anyhow::Result;
use axum::routing::{get, patch, post, put};
use axum::Router;
use handler::role::RoleHandler;
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation};
use models::app::AppState;
use snowflake::SnowflakeIdGenerator;
Expand Down Expand Up @@ -89,6 +90,12 @@ async fn main() -> Result<()> {
.put(OrganisationHandler::update_admins)
.delete(OrganisationHandler::remove_admin),
)
.route(
"/api/v1/role/:id",
get(RoleHandler::get)
.put(RoleHandler::update)
.delete(RoleHandler::delete)
)
.with_state(state);

let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
Expand Down
39 changes: 39 additions & 0 deletions backend/server/src/models/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,43 @@ impl Role {

Ok(role)
}

pub async fn delete(id: i32, pool: &Pool<Postgres>) -> Result<(), ChaosError> {
sqlx::query!(
"
DELETE FROM campaign_roles WHERE id = $1
",
id
)
.execute(pool)
.await?;

Ok(())
}

pub async fn update(
id: i32,
role_data: RoleUpdate,
transaction: &mut Transaction<'_, Postgres>,
) -> Result<(), ChaosError> {
sqlx::query!(
"
UPDATE campaign_roles
SET (name, description, min_available, max_available, finalised) = ($2, $3, $4, $5, $6)
WHERE id = $1;
",
id,
role_data.name,
role_data.description,
role_data.min_available,
role_data.max_avaliable,
role_data.finalised
)
.execute(transaction.deref_mut())
.await?;

Ok(())
}


}

0 comments on commit 11e8f01

Please sign in to comment.