Skip to content

Commit

Permalink
change *Admin FromRequest implementations to use HashMap of path …
Browse files Browse the repository at this point in the history
…variables
  • Loading branch information
KavikaPalletenne committed Aug 2, 2024
1 parent 673f2dd commit 7c5c326
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
12 changes: 6 additions & 6 deletions backend/server/src/handler/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ pub struct ApplicationHandler;

impl ApplicationHandler {
pub async fn get(
Path(id): Path<i64>,
Path(application_id): Path<i64>,
_admin: ApplicationAdmin,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let application = Application::get(id, &mut transaction.tx).await?;
let application = Application::get(application_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(application)))
}

pub async fn set_status(
State(state): State<AppState>,
Path(id): Path<i64>,
Path(application_id): Path<i64>,
_admin: ApplicationAdmin,
Json(data): Json<ApplicationStatus>,
) -> Result<impl IntoResponse, ChaosError> {
Application::set_status(id, data, &state.db).await?;
Application::set_status(application_id, data, &state.db).await?;
Ok((StatusCode::OK, "Status successfully updated"))
}

pub async fn set_private_status(
State(state): State<AppState>,
Path(id): Path<i64>,
Path(application_id): Path<i64>,
_admin: ApplicationAdmin,
Json(data): Json<ApplicationStatus>,
) -> Result<impl IntoResponse, ChaosError> {
Application::set_private_status(id, data, &state.db).await?;
Application::set_private_status(application_id, data, &state.db).await?;
Ok((StatusCode::OK, "Private Status successfully updated"))
}

Expand Down
34 changes: 17 additions & 17 deletions backend/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,72 +73,72 @@ async fn main() -> Result<()> {
)
.route("/api/v1/organisation", post(OrganisationHandler::create))
.route(
"/api/v1/organisation/:id",
"/api/v1/organisation/:organisation_id",
get(OrganisationHandler::get).delete(OrganisationHandler::delete),
)
.route(
"/api/v1/organisation/:id/campaign",
"/api/v1/organisation/:organisation_id/campaign",
post(OrganisationHandler::create_campaign),
)
.route(
"/api/v1/organisation/:id/campaigns",
"/api/v1/organisation/:organisation_id/campaigns",
get(OrganisationHandler::get_campaigns),
)
.route(
"/api/v1/organisation/:id/logo",
"/api/v1/organisation/:organisation_id/logo",
patch(OrganisationHandler::update_logo),
)
.route(
"/api/v1/organisation/:id/member",
"/api/v1/organisation/:organisation_id/member",
get(OrganisationHandler::get_members)
.put(OrganisationHandler::update_members)
.delete(OrganisationHandler::remove_member),
)
.route(
"/api/v1/organisation/:id/admin",
"/api/v1/organisation/:organisation_id/admin",
get(OrganisationHandler::get_admins)
.put(OrganisationHandler::update_admins)
.delete(OrganisationHandler::remove_admin),
)
.route(
"/api/v1/campaign/:id/role",
"/api/v1/campaign/:campaign_id/role",
post(CampaignHandler::create_role),
)
.route(
"/api/v1/campaign/:id/roles",
"/api/v1/campaign/:campaign_id/roles",
get(CampaignHandler::get_roles),
)
.route(
"/api/v1/campaign/:id/applications",
"/api/v1/campaign/:campaign_id/applications",
get(CampaignHandler::get_applications),
)
.route(
"/api/v1/role/:id",
"/api/v1/role/:role_id",
get(RoleHandler::get)
.put(RoleHandler::update)
.delete(RoleHandler::delete),
)
.route(
"/api/v1/role/:id/applications",
"/api/v1/role/:role_id/applications",
get(RoleHandler::get_applications)
)
.route(
"/api/v1/campaign/:id",
"/api/v1/campaign/:campaign_id",
get(CampaignHandler::get)
.put(CampaignHandler::update)
.delete(CampaignHandler::delete),
)
.route("/api/v1/campaign", get(CampaignHandler::get_all))
.route(
"/api/v1/campaign/:id/banner",
"/api/v1/campaign/:campaign_id/banner",
patch(CampaignHandler::update_banner),
)
.route("api/v1/campaign/:id/application",
.route("api/v1/campaign/:campaign_id/application",
post(CampaignHandler::create_application)
)
.route("api/v1/application/:id", get(ApplicationHandler::get))
.route("api/v1/application/:id/status", patch(ApplicationHandler::set_status))
.route("api/v1/application/:id/private", patch(ApplicationHandler::set_private_status))
.route("api/v1/application/:application_id", get(ApplicationHandler::get))
.route("api/v1/application/:application_id/status", patch(ApplicationHandler::set_status))
.route("api/v1/application/:application_id/private", patch(ApplicationHandler::set_private_status))
.with_state(state);

let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
Expand Down
27 changes: 17 additions & 10 deletions backend/server/src/models/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use crate::models::app::AppState;
use crate::models::error::ChaosError;
use crate::service::application::user_is_application_admin;
Expand Down Expand Up @@ -137,10 +138,12 @@ where
let pool = &app_state.db;
let user_id = claims.sub;

let Path(organisation_id) = parts
.extract::<Path<i64>>()
let organisation_id = *parts
.extract::<Path<HashMap<String ,i64>>>()
.await
.map_err(|_| ChaosError::BadRequest)?;
.map_err(|_| ChaosError::BadRequest)?
.get("organisation_id")
.ok_or(ChaosError::BadRequest)?;

user_is_organisation_admin(user_id, organisation_id, pool).await?;

Expand Down Expand Up @@ -177,10 +180,12 @@ where
let pool = &app_state.db;
let user_id = claims.sub;

let Path(campaign_id) = parts
.extract::<Path<i64>>()
let campaign_id = *parts
.extract::<Path<HashMap<String, i64>>>()
.await
.map_err(|_| ChaosError::BadRequest)?;
.map_err(|_| ChaosError::BadRequest)?
.get("campaign_id")
.ok_or(ChaosError::BadRequest)?;

user_is_campaign_admin(user_id, campaign_id, pool).await?;

Expand Down Expand Up @@ -217,12 +222,14 @@ where
let pool = &app_state.db;
let user_id = claims.sub;

let Path(campaign_id) = parts
.extract::<Path<i64>>()
let role_id = *parts
.extract::<Path<HashMap<String, i64>>>()
.await
.map_err(|_| ChaosError::BadRequest)?;
.map_err(|_| ChaosError::BadRequest)?
.get("role_id")
.ok_or(ChaosError::BadRequest)?;

user_is_role_admin(user_id, campaign_id, pool).await?;
user_is_role_admin(user_id, role_id, pool).await?;

Ok(RoleAdmin { user_id })
}
Expand Down

0 comments on commit 7c5c326

Please sign in to comment.