-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Initial draft TODO: TESTING + more clarification * rearranged import * clear errors * post testing * feat(db): add indexes for foreign key reference columns * fix(db): index naming and missing semicolons * dep(backend): update axum * fix(backend): remove testing jwt handlers * feat(backend): Add custom jwt validator and header * feat(backend): basic error handling enum * fix(backend): ran cargo fmt * fix(backend): remove unused imports * CRUD operations - awaiting Campaign - haven't enforced db safety * implement feedback * Update rust.yml to include 224 branch * logic and style fixes * Change to using `thiserror` * add organisation_role type to db * update migration timestamps to be `NOT NULL` * change sqlx `time` to `chrono` * integrate organisations crud with error handling * return member role with org members * update sqlx type name for `UserRole` * simplify handlers to use new error type * removed unused imports * added `OrganisationAdmin` extractor * use `Transaction` when doing multiple queries * cargo fmt * add org route to app * move `Organisation` service functions into `Organisation` struct * moved org handlers into `OrganisationHandler` struct * ran cargo fmt * add S3 url generation to logo update * fixed error renaming * add routes to `main.rs` * cargo fmt --------- Co-authored-by: Alexander <[email protected]> Co-authored-by: Kavika <[email protected]>
- Loading branch information
1 parent
1405e97
commit 5fe7add
Showing
24 changed files
with
762 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod auth; | ||
pub mod organisation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
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::organisation::{AdminToRemove, AdminUpdateList, NewOrganisation, Organisation}; | ||
use crate::models::transaction::DBTransaction; | ||
use crate::service; | ||
use axum::extract::{Json, Path, State}; | ||
use axum::http::StatusCode; | ||
use axum::response::IntoResponse; | ||
|
||
pub struct OrganisationHandler; | ||
|
||
impl OrganisationHandler { | ||
pub async fn create( | ||
State(state): State<AppState>, | ||
_user: SuperUser, | ||
mut transaction: DBTransaction<'_>, | ||
Json(data): Json<NewOrganisation>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Organisation::create( | ||
data.admin, | ||
data.name, | ||
state.snowflake_generator, | ||
&mut transaction.tx, | ||
) | ||
.await?; | ||
|
||
transaction.tx.commit().await?; | ||
Ok((StatusCode::OK, "Successfully created organisation")) | ||
} | ||
|
||
pub async fn get( | ||
State(state): State<AppState>, | ||
Path(id): Path<i64>, | ||
_user: AuthUser, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let org = Organisation::get(id, &state.db).await?; | ||
Ok((StatusCode::OK, Json(org))) | ||
} | ||
|
||
pub async fn delete( | ||
State(state): State<AppState>, | ||
Path(id): Path<i64>, | ||
_user: SuperUser, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Organisation::delete(id, &state.db).await?; | ||
Ok((StatusCode::OK, "Successfully deleted organisation")) | ||
} | ||
|
||
pub async fn get_admins( | ||
State(state): State<AppState>, | ||
Path(id): Path<i64>, | ||
_user: SuperUser, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let members = Organisation::get_admins(id, &state.db).await?; | ||
Ok((StatusCode::OK, Json(members))) | ||
} | ||
|
||
pub async fn get_members( | ||
State(state): State<AppState>, | ||
Path(id): Path<i64>, | ||
_admin: OrganisationAdmin, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let members = Organisation::get_members(id, &state.db).await?; | ||
Ok((StatusCode::OK, Json(members))) | ||
} | ||
|
||
pub async fn update_admins( | ||
State(state): State<AppState>, | ||
Path(id): Path<i64>, | ||
_super_user: SuperUser, | ||
mut transaction: DBTransaction<'_>, | ||
Json(request_body): Json<AdminUpdateList>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Organisation::update_admins(id, request_body.members, &mut transaction.tx).await?; | ||
|
||
transaction.tx.commit().await?; | ||
Ok((StatusCode::OK, "Successfully updated organisation members")) | ||
} | ||
|
||
pub async fn update_members( | ||
State(state): State<AppState>, | ||
mut transaction: DBTransaction<'_>, | ||
Path(id): Path<i64>, | ||
_admin: OrganisationAdmin, | ||
Json(request_body): Json<AdminUpdateList>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Organisation::update_members(id, request_body.members, &mut transaction.tx).await?; | ||
|
||
transaction.tx.commit().await?; | ||
Ok((StatusCode::OK, "Successfully updated organisation members")) | ||
} | ||
|
||
pub async fn remove_admin( | ||
State(state): State<AppState>, | ||
Path(id): Path<i64>, | ||
_super_user: SuperUser, | ||
Json(request_body): Json<AdminToRemove>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Organisation::remove_admin(id, request_body.user_id, &state.db).await?; | ||
|
||
Ok(( | ||
StatusCode::OK, | ||
"Successfully removed member from organisation", | ||
)) | ||
} | ||
|
||
pub async fn remove_member( | ||
State(state): State<AppState>, | ||
Path(id): Path<i64>, | ||
_admin: OrganisationAdmin, | ||
Json(request_body): Json<AdminToRemove>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Organisation::remove_member(id, request_body.user_id, &state.db).await?; | ||
|
||
Ok(( | ||
StatusCode::OK, | ||
"Successfully removed member from organisation", | ||
)) | ||
} | ||
|
||
pub async fn update_logo( | ||
State(state): State<AppState>, | ||
Path(id): Path<i64>, | ||
_admin: OrganisationAdmin, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let logo_url = Organisation::update_logo(id, &state.db, &state.storage_bucket).await?; | ||
Ok((StatusCode::OK, Json(logo_url))) | ||
} | ||
|
||
pub async fn get_campaigns( | ||
State(state): State<AppState>, | ||
Path(id): Path<i64>, | ||
_user: AuthUser, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let campaigns = Organisation::get_campaigns(id, &state.db).await?; | ||
|
||
Ok((StatusCode::OK, Json(campaigns))) | ||
} | ||
|
||
pub async fn create_campaign( | ||
State(mut state): State<AppState>, | ||
_admin: OrganisationAdmin, | ||
Json(request_body): Json<models::campaign::Campaign>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Organisation::create_campaign( | ||
request_body.name, | ||
request_body.description, | ||
request_body.starts_at, | ||
request_body.ends_at, | ||
&state.db, | ||
&mut state.snowflake_generator, | ||
) | ||
.await?; | ||
|
||
Ok((StatusCode::OK, "Successfully created campaign")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.