Skip to content

Commit

Permalink
Response200 struct implemented + Errors improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac Ecob committed Aug 1, 2024
1 parent 4fa8c46 commit d2403c4
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 18 deletions.
23 changes: 9 additions & 14 deletions backend/server/src/handler/organisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ 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::models::response::Response200;
use crate::service;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
//use http::status::StatusCode;
use aide::axum::IntoApiResponse;


pub struct OrganisationHandler;

impl OrganisationHandler {
Expand All @@ -29,7 +30,7 @@ impl OrganisationHandler {
.await?;

transaction.tx.commit().await?;
Ok((StatusCode::OK, "Successfully created organisation"))
Ok(Response200::<"Succesfully created organisation"> {})
}

pub async fn get(
Expand All @@ -47,7 +48,7 @@ impl OrganisationHandler {
_user: SuperUser,
) -> Result<impl IntoApiResponse, ChaosError> {
Organisation::delete(id, &state.db).await?;
Ok((StatusCode::OK, "Successfully deleted organisation"))
Ok(Response200::<"Succesfully deleted organisation"> {})
}

pub async fn get_admins(
Expand Down Expand Up @@ -78,7 +79,7 @@ impl OrganisationHandler {
Organisation::update_admins(id, request_body.members, &mut transaction.tx).await?;

transaction.tx.commit().await?;
Ok((StatusCode::OK, "Successfully updated organisation members"))
Ok(Response200::<"Successfully updated organisation members"> {})
}

pub async fn update_members(
Expand All @@ -91,7 +92,7 @@ impl OrganisationHandler {
Organisation::update_members(id, request_body.members, &mut transaction.tx).await?;

transaction.tx.commit().await?;
Ok((StatusCode::OK, "Successfully updated organisation members"))
Ok(Response200::<"Successfully updated organisation members"> {})
}

pub async fn remove_admin(
Expand All @@ -102,10 +103,7 @@ impl OrganisationHandler {
) -> Result<impl IntoApiResponse, ChaosError> {
Organisation::remove_admin(id, request_body.user_id, &state.db).await?;

Ok((
StatusCode::OK,
"Successfully removed member from organisation",
))
Ok(Response200::<"Successfully removed member from organisation"> {})
}

pub async fn remove_member(
Expand All @@ -116,10 +114,7 @@ impl OrganisationHandler {
) -> Result<impl IntoApiResponse, ChaosError> {
Organisation::remove_member(id, request_body.user_id, &state.db).await?;

Ok((
StatusCode::OK,
"Successfully removed member from organisation",
))
Ok(Response200::<"Successfully removed member from organisation"> {})
}

pub async fn update_logo(
Expand Down Expand Up @@ -156,6 +151,6 @@ impl OrganisationHandler {
)
.await?;

Ok((StatusCode::OK, "Successfully created campaign"))
Ok(Response200::<"Successfully created campaign"> {})
}
}
2 changes: 2 additions & 0 deletions backend/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(adt_const_params)]

use crate::handler::auth::google_callback;
use crate::handler::organisation::OrganisationHandler;
use crate::models::storage::Storage;
Expand Down
20 changes: 16 additions & 4 deletions backend/server/src/models/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,22 @@ impl OperationOutput for ChaosError {
operation: &mut Operation,
) -> Vec<(Option<u16>, aide::openapi::Response)> {
Vec::from([
(Some(400), Default::default()), // Bad request
(Some(401), Default::default()), // Unauthorized
(Some(403), Default::default()), // Forbidden
(Some(500), Default::default()), // Internal Server Error
(Some(400), aide::openapi::Response { // bad request
description: String::from("Bad request"),
..Default::default()
}),
(Some(401), aide::openapi::Response { // Unauthorized
description: String::from("Unauthorized"),
..Default::default()
}),
(Some(403), aide::openapi::Response { // Forbidden operation
description: String::from("Forbidden operation"),
..Default::default()
}),
(Some(500), aide::openapi::Response { // Internal server error
description: String::from("Internal server error"),
..Default::default()
}),
])
}
}
1 change: 1 addition & 0 deletions backend/server/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod organisation;
pub mod storage;
pub mod transaction;
pub mod user;
pub mod response;
31 changes: 31 additions & 0 deletions backend/server/src/models/response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use axum::response::{IntoResponse, Response};
use aide::OperationOutput;
use aide:: gen;
use aide::openapi::Operation;
use axum::http::StatusCode;


pub struct Response200<const MSG: &'static str> {}

/// Implementation for converting errors into responses. Manages error code and message returned.
impl<const MSG: &'static str> IntoResponse for Response200<MSG> {
fn into_response(self) -> Response {
(StatusCode::OK, MSG).into_response()
}
}

impl<const MSG: &'static str> OperationOutput for Response200<MSG> {
type Inner = Self;

fn inferred_responses(
ctx: &mut gen::GenContext,
operation: &mut Operation,
) -> Vec<(Option<u16>, aide::openapi::Response)> {
Vec::from([
(Some(200), aide::openapi::Response {
description: MSG.to_string(),
..Default::default()
})
])
}
}
34 changes: 34 additions & 0 deletions backend/server/src/new_org.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
struct Response200<const MSG: &'static str> {}

/*
impl OperationOutput for CreateRes {
....
}
impl IntoResponse for CreateRes {
....
}
*/

//Use const-generics?? I might be cooking

impl OrganisationHandler {
pub async fn create(
State(state): State<AppState>,
_user: SuperUser,
mut transaction: DBTransaction<'_>,
Json(data): Json<NewOrganisation>,
) -> Result<impl IntoApiResponse, ChaosError> {
Organisation::create(
data.admin,
data.name,
state.snowflake_generator,
&mut transaction.tx,
)
.await?;

transaction.tx.commit().await?;
Ok((StatusCode::OK, Response200::<"Succesfully created organisation!"> {})
}
}

0 comments on commit d2403c4

Please sign in to comment.