Skip to content

Commit

Permalink
ran cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
KavikaPalletenne committed Jul 20, 2024
1 parent 4cc12f0 commit 52b3ae6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 42 deletions.
4 changes: 2 additions & 2 deletions backend/server/src/handler/campaign.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl CampaignHandler {
_admin: CampaignAdmin,
Json(data): Json<RoleUpdate>,
) -> Result<impl IntoResponse, ChaosError> {
Role::create(id, data, &state.db).await?;
Role::create(id, data, &state.db, state.snowflake_generator).await?;
Ok((StatusCode::OK, "Successfully created role"))
}

Expand Down
10 changes: 5 additions & 5 deletions backend/server/src/handler/role.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -11,7 +11,7 @@ pub struct RoleHandler;
impl RoleHandler {
pub async fn get(
State(state): State<AppState>,
Path(id): Path<i32>,
Path(id): Path<i64>,
_user: AuthUser,
) -> Result<impl IntoResponse, ChaosError> {
let role = Role::get(id, &state.db).await?;
Expand All @@ -20,7 +20,7 @@ impl RoleHandler {

pub async fn delete(
State(state): State<AppState>,
Path(id): Path<i32>,
Path(id): Path<i64>,
_admin: RoleAdmin,
) -> Result<impl IntoResponse, ChaosError> {
Role::delete(id, &state.db).await?;
Expand All @@ -29,11 +29,11 @@ impl RoleHandler {

pub async fn update(
State(state): State<AppState>,
Path(id): Path<i32>,
Path(id): Path<i64>,
_admin: RoleAdmin,
Json(data): Json<RoleUpdate>,
) -> Result<impl IntoResponse, ChaosError> {
Role::update(id, data, &state.db).await?;
Ok((StatusCode::OK, "Successfully updated role"))
}
}
}
10 changes: 5 additions & 5 deletions backend/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions backend/server/src/models/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -225,4 +225,4 @@ where

Ok(RoleAdmin { user_id })
}
}
}
2 changes: 1 addition & 1 deletion backend/server/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
41 changes: 19 additions & 22 deletions backend/server/src/models/role.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -35,7 +35,6 @@ pub struct RoleDetails {
pub finalised: bool,
}


impl Role {
pub async fn create(
campaign_id: i64,
Expand All @@ -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,
Expand All @@ -64,14 +63,14 @@ impl Role {
Ok(())
}

pub async fn get(id: i32, pool: &Pool<Postgres>) -> Result<RoleDetails, ChaosError> {
pub async fn get(id: i64, pool: &Pool<Postgres>) -> Result<RoleDetails, ChaosError> {
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)
Expand All @@ -80,11 +79,11 @@ impl Role {
Ok(role)
}

pub async fn delete(id: i32, pool: &Pool<Postgres>) -> Result<(), ChaosError> {
pub async fn delete(id: i64, pool: &Pool<Postgres>) -> Result<(), ChaosError> {
sqlx::query!(
"
DELETE FROM campaign_roles WHERE id = $1
",
DELETE FROM campaign_roles WHERE id = $1
",
id
)
.execute(pool)
Expand All @@ -94,16 +93,16 @@ impl Role {
}

pub async fn update(
id: i32,
id: i64,
role_data: RoleUpdate,
pool: &Pool<Postgres>
pool: &Pool<Postgres>,
) -> 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,
Expand All @@ -122,22 +121,20 @@ impl Role {
*/
pub async fn get_all_in_campaign(
campaign_id: i64,
pool: &Pool<Postgres>
pool: &Pool<Postgres>,
) -> Result<Vec<RoleDetails>, 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)
.await?;

Ok(roles)
}


}
}
10 changes: 5 additions & 5 deletions backend/server/src/service/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

0 comments on commit 52b3ae6

Please sign in to comment.