-
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.
- Loading branch information
1 parent
7a560e8
commit 3ed9515
Showing
7 changed files
with
184 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pub mod organisation; | |
pub mod rating; | ||
pub mod role; | ||
pub mod application; | ||
pub mod question; |
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,94 @@ | ||
use axum::extract::{Json, Path, State}; | ||
use axum::response::IntoResponse; | ||
use axum::http::StatusCode; | ||
use serde_json::json; | ||
use crate::models::app::AppState; | ||
use crate::models::auth::{AuthUser, CampaignAdmin, QuestionAdmin}; | ||
use crate::models::error::ChaosError; | ||
use crate::models::question::{NewQuestion, Question}; | ||
use crate::models::transaction::DBTransaction; | ||
|
||
pub struct QuestionHandler; | ||
|
||
impl QuestionHandler { | ||
pub async fn create( | ||
State(state): State<AppState>, | ||
Path(campaign_id): Path<i64>, | ||
_admin: CampaignAdmin, | ||
mut transaction: DBTransaction<'_>, | ||
Json(data): Json<NewQuestion>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let id = Question::create( | ||
campaign_id, | ||
data.title, | ||
data.description, | ||
data.common, | ||
data.roles, | ||
data.required, | ||
data.question_data, | ||
state.snowflake_generator, | ||
&mut transaction.tx, | ||
).await?; | ||
|
||
transaction.tx.commit().await?; | ||
|
||
Ok((StatusCode::OK, Json(json!({"id": id})))) | ||
} | ||
|
||
pub async fn get_all_by_campaign_and_role( | ||
Path((campaign_id, role_id)): Path<(i64, i64)>, | ||
_user: AuthUser, | ||
mut transaction: DBTransaction<'_>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let questions = Question::get_all_by_campaign_and_role( | ||
campaign_id, role_id, &mut transaction.tx | ||
).await?; | ||
|
||
transaction.tx.commit().await?; | ||
|
||
Ok((StatusCode::OK, Json(questions))) | ||
} | ||
|
||
pub async fn get_all_common_by_campaign( | ||
Path(campaign_id): Path<i64>, | ||
_user: AuthUser, | ||
mut transaction: DBTransaction<'_>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let questions = Question::get_all_common_by_campaign( | ||
campaign_id, &mut transaction.tx, | ||
).await?; | ||
|
||
transaction.tx.commit().await?; | ||
|
||
Ok((StatusCode::OK, Json(questions))) | ||
} | ||
|
||
pub async fn update( | ||
State(state): State<AppState>, | ||
Path(question_id): Path<i64>, | ||
_admin: QuestionAdmin, | ||
mut transaction: DBTransaction<'_>, | ||
Json(data): Json<NewQuestion>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Question::update( | ||
question_id, data.title, data.description, data.common, | ||
data.roles, data.required, data.question_data, | ||
&mut transaction.tx, state.snowflake_generator, | ||
).await?; | ||
|
||
Ok((StatusCode::OK, "Successfully updated question")) | ||
} | ||
|
||
pub async fn delete( | ||
Path(question_id): Path<i64>, | ||
_admin: QuestionAdmin, | ||
mut transaction: DBTransaction<'_>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Question::delete( | ||
question_id, | ||
&mut transaction.tx, | ||
).await?; | ||
|
||
Ok((StatusCode::OK, "Successfully deleted question")) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ pub mod organisation; | |
pub mod rating; | ||
pub mod role; | ||
pub mod application; | ||
pub mod question; |
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,32 @@ | ||
use sqlx::{Pool, Postgres}; | ||
use crate::models::error::ChaosError; | ||
|
||
pub async fn user_is_question_admin( | ||
user_id: i64, | ||
question_id: i64, | ||
pool: &Pool<Postgres>, | ||
) -> Result<(), ChaosError> { | ||
let is_admin = sqlx::query!( | ||
" | ||
SELECT EXISTS( | ||
SELECT 1 | ||
FROM questions q | ||
JOIN campaigns c on q.campaign_id = c.id | ||
JOIN organisation_members om on c.organisation_id = om.organisation_id | ||
WHERE q.id = $1 AND om.user_id = $2 AND om.role = 'Admin' | ||
) | ||
", | ||
question_id, | ||
user_id | ||
) | ||
.fetch_one(pool) | ||
.await? | ||
.exists | ||
.expect("`exists` should always exist in this query result"); | ||
|
||
if !is_admin { | ||
return Err(ChaosError::Unauthorized); | ||
} | ||
|
||
Ok(()) | ||
} |