diff --git a/backend/server/src/handler/campaign.rs b/backend/server/src/handler/campaign.rs index d621c76d..48e15286 100644 --- a/backend/server/src/handler/campaign.rs +++ b/backend/server/src/handler/campaign.rs @@ -1,6 +1,6 @@ use crate::models; use crate::models::app::AppState; -use crate::models::auth::{AuthUser, OrganisationAdmin}; +use crate::models::auth::AuthUser; use crate::models::auth::CampaignAdmin; use crate::models::campaign::Campaign; use crate::models::error::ChaosError; @@ -62,7 +62,7 @@ impl CampaignHandler { _admin: CampaignAdmin, Json(data): Json, ) -> Result { - Role::create(id, data, &state.db).await?; + Role::create(id, data, &state.db, state.snowflake_generator).await?; Ok((StatusCode::OK, "Successfully created role")) } diff --git a/backend/server/src/handler/role.rs b/backend/server/src/handler/role.rs index 365f55c4..ff569cc6 100644 --- a/backend/server/src/handler/role.rs +++ b/backend/server/src/handler/role.rs @@ -1,5 +1,5 @@ use crate::models::app::AppState; -use crate::models::auth::{AuthUser, OrganisationAdmin, RoleAdmin}; +use crate::models::auth::{AuthUser, RoleAdmin}; use crate::models::error::ChaosError; use crate::models::role::{Role, RoleUpdate}; use axum::extract::{Json, Path, State}; @@ -11,7 +11,7 @@ pub struct RoleHandler; impl RoleHandler { pub async fn get( State(state): State, - Path(id): Path, + Path(id): Path, _user: AuthUser, ) -> Result { let role = Role::get(id, &state.db).await?; @@ -20,7 +20,7 @@ impl RoleHandler { pub async fn delete( State(state): State, - Path(id): Path, + Path(id): Path, _admin: RoleAdmin, ) -> Result { Role::delete(id, &state.db).await?; @@ -29,11 +29,11 @@ impl RoleHandler { pub async fn update( State(state): State, - Path(id): Path, + Path(id): Path, _admin: RoleAdmin, Json(data): Json, ) -> Result { Role::update(id, data, &state.db).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 a062368e..7b27e12b 100644 --- a/backend/server/src/main.rs +++ b/backend/server/src/main.rs @@ -92,18 +92,18 @@ async fn main() -> Result<()> { .delete(OrganisationHandler::remove_admin), ) .route( - "/api/v1/campaign/:id/role", - post(CampaignHandler::create_role) + "/api/v1/campaign/:id/role", + post(CampaignHandler::create_role), ) .route( "/api/v1/campaign/:id/roles", - get(CampaignHandler::get_roles) + get(CampaignHandler::get_roles), ) .route( - "/api/v1/role/:id", + "/api/v1/role/:id", get(RoleHandler::get) .put(RoleHandler::update) - .delete(RoleHandler::delete) + .delete(RoleHandler::delete), ) .route( "/api/v1/campaign/:id", diff --git a/backend/server/src/models/auth.rs b/backend/server/src/models/auth.rs index f0b04b43..99ef8c85 100644 --- a/backend/server/src/models/auth.rs +++ b/backend/server/src/models/auth.rs @@ -4,13 +4,13 @@ use crate::service::auth::is_super_user; use crate::service::campaign::user_is_campaign_admin; use crate::service::jwt::decode_auth_token; use crate::service::organisation::user_is_organisation_admin; +use crate::service::role::user_is_role_admin; use axum::extract::{FromRef, FromRequestParts, Path}; use axum::http::request::Parts; use axum::response::{IntoResponse, Redirect, Response}; use axum::{async_trait, RequestPartsExt}; use axum_extra::{headers::Cookie, TypedHeader}; use serde::{Deserialize, Serialize}; -use crate::service::role::user_is_role_admin; #[derive(Deserialize, Serialize)] pub struct AuthRequest { @@ -225,4 +225,4 @@ where Ok(RoleAdmin { user_id }) } -} \ No newline at end of file +} diff --git a/backend/server/src/models/mod.rs b/backend/server/src/models/mod.rs index 51e9d204..76102f22 100644 --- a/backend/server/src/models/mod.rs +++ b/backend/server/src/models/mod.rs @@ -3,7 +3,7 @@ pub mod auth; pub mod campaign; pub mod error; pub mod organisation; +pub mod role; pub mod storage; pub mod transaction; pub mod user; -pub mod role; diff --git a/backend/server/src/models/role.rs b/backend/server/src/models/role.rs index 45d46ed9..1d5386c7 100644 --- a/backend/server/src/models/role.rs +++ b/backend/server/src/models/role.rs @@ -1,7 +1,7 @@ +use crate::models::error::ChaosError; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use snowflake::SnowflakeIdGenerator; -use crate::models::error::ChaosError; use sqlx::{FromRow, Pool, Postgres}; #[derive(Deserialize, Serialize, Clone, FromRow, Debug)] @@ -35,7 +35,6 @@ pub struct RoleDetails { pub finalised: bool, } - impl Role { pub async fn create( campaign_id: i64, @@ -47,9 +46,9 @@ impl Role { sqlx::query!( " - INSERT INTO campaign_roles (id, campaign_id, name, description, min_available, max_available, finalised) + INSERT INTO campaign_roles (id, campaign_id, name, description, min_available, max_available, finalised) VALUES ($1, $2, $3, $4, $5, $6, $7) - ", + ", id, campaign_id, role_data.name, @@ -64,14 +63,14 @@ impl Role { Ok(()) } - pub async fn get(id: i32, pool: &Pool) -> Result { + pub async fn get(id: i64, pool: &Pool) -> Result { let role = sqlx::query_as!( RoleDetails, " - SELECT name, description, min_available, max_available, finalised + SELECT name, description, min_available, max_available, finalised FROM campaign_roles WHERE id = $1 - ", + ", id ) .fetch_one(pool) @@ -80,11 +79,11 @@ impl Role { Ok(role) } - pub async fn delete(id: i32, pool: &Pool) -> Result<(), ChaosError> { + pub async fn delete(id: i64, pool: &Pool) -> Result<(), ChaosError> { sqlx::query!( " - DELETE FROM campaign_roles WHERE id = $1 - ", + DELETE FROM campaign_roles WHERE id = $1 + ", id ) .execute(pool) @@ -94,16 +93,16 @@ impl Role { } pub async fn update( - id: i32, + id: i64, role_data: RoleUpdate, - pool: &Pool + pool: &Pool, ) -> Result<(), ChaosError> { sqlx::query!( " - UPDATE campaign_roles - SET (name, description, min_available, max_available, finalised) = ($2, $3, $4, $5, $6) - WHERE id = $1; - ", + 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, @@ -122,15 +121,15 @@ impl Role { */ pub async fn get_all_in_campaign( campaign_id: i64, - pool: &Pool + pool: &Pool, ) -> Result, ChaosError> { let roles = sqlx::query_as!( RoleDetails, " - SELECT name, description, min_available, max_available, finalised + SELECT name, description, min_available, max_available, finalised FROM campaign_roles WHERE campaign_id = $1 - ", + ", campaign_id ) .fetch_all(pool) @@ -138,6 +137,4 @@ impl Role { Ok(roles) } - - -} \ No newline at end of file +} diff --git a/backend/server/src/service/role.rs b/backend/server/src/service/role.rs index 3b06ab36..52329869 100644 --- a/backend/server/src/service/role.rs +++ b/backend/server/src/service/role.rs @@ -21,14 +21,14 @@ pub async fn user_is_role_admin( role_id, user_id ) - .fetch_one(pool) - .await? - .exists - .expect("`exists` should always exist in this query result"); + .fetch_one(pool) + .await? + .exists + .expect("`exists` should always exist in this query result"); if !is_admin { return Err(ChaosError::Unauthorized); } Ok(()) -} \ No newline at end of file +}