diff --git a/backend/server/src/handler/mod.rs b/backend/server/src/handler/mod.rs index 163c4759..6d82bea2 100644 --- a/backend/server/src/handler/mod.rs +++ b/backend/server/src/handler/mod.rs @@ -1,2 +1,3 @@ pub mod auth; pub mod organisation; +pub mod role; diff --git a/backend/server/src/handler/role.rs b/backend/server/src/handler/role.rs new file mode 100644 index 00000000..4f092799 --- /dev/null +++ b/backend/server/src/handler/role.rs @@ -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, + Path(id): Path, + _user: AuthUser, + ) -> Result { + let role = Role::get(id, &state.db).await?; + Ok((StatusCode::OK, Json(role))) + } + + pub async fn delete( + State(state): State, + Path(id): Path, + _admin: OrganisationAdmin, + ) -> Result { + Role::delete(id, &state.db).await?; + Ok((StatusCode::OK, "Successfully deleted role")) + } + + pub async fn update( + State(state): State, + Path(id): Path, + _admin: OrganisationAdmin, + mut transaction: DBTransaction<'_>, + Json(data): Json, + ) -> Result { + Role::update(id, data, &mut transaction.tx,).await?; + transaction.tx.commit().await?; + Ok((StatusCode::OK, "Successfully updated role")) + } +} \ No newline at end of file diff --git a/backend/server/src/main.rs b/backend/server/src/main.rs index 38b2022c..d2f41502 100644 --- a/backend/server/src/main.rs +++ b/backend/server/src/main.rs @@ -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; @@ -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(); diff --git a/backend/server/src/models/role.rs b/backend/server/src/models/role.rs index c16b3951..219c73fc 100644 --- a/backend/server/src/models/role.rs +++ b/backend/server/src/models/role.rs @@ -77,4 +77,43 @@ impl Role { Ok(role) } + + pub async fn delete(id: i32, pool: &Pool) -> 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(()) + } + + } \ No newline at end of file