-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor how query params are deserialized
- Loading branch information
1 parent
84304b6
commit ac1113b
Showing
5 changed files
with
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub(crate) mod coaching_relationships; | ||
pub(crate) mod coaching_sessions; | ||
pub(crate) mod overarching_goals; |
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,52 @@ | ||
use crate::{extractors::authenticated_user::AuthenticatedUser, AppState}; | ||
use axum::{ | ||
extract::{Query, Request, State}, | ||
http::StatusCode, | ||
middleware::Next, | ||
response::IntoResponse, | ||
}; | ||
use entity::Id; | ||
use entity_api::coaching_session; | ||
use log::*; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub(crate) struct QueryParams { | ||
coaching_session_id: Id, | ||
} | ||
|
||
/// Checks that coaching relationship record associated with the coaching session | ||
/// referenced by `coaching_session_id exists and that the authenticated user is associated with it. | ||
/// Intended to be given to axum::middleware::from_fn_with_state in the router | ||
pub(crate) async fn index( | ||
State(app_state): State<AppState>, | ||
AuthenticatedUser(user): AuthenticatedUser, | ||
Query(params): Query<QueryParams>, | ||
request: Request, | ||
next: Next, | ||
) -> impl IntoResponse { | ||
match coaching_session::find_by_id_with_coaching_relationship( | ||
app_state.db_conn_ref(), | ||
params.coaching_session_id, | ||
) | ||
.await | ||
{ | ||
Ok((_coaching_session, coaching_relationship)) => { | ||
if coaching_relationship.coach_id == user.id | ||
|| coaching_relationship.coachee_id == user.id | ||
{ | ||
// User has access to coaching relationship | ||
next.run(request).await | ||
} else { | ||
// User does not have access to coaching relationship | ||
(StatusCode::UNAUTHORIZED, "UNAUTHORIZED").into_response() | ||
} | ||
} | ||
Err(e) => { | ||
error!("Error authorizing overarching goals index{:?}", e); | ||
|
||
(StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR").into_response() | ||
} | ||
|
||
} | ||
} |