comment event endpoint
This commit is contained in:
parent
234ae29e96
commit
e0ce62ddb7
@ -262,16 +262,22 @@ pub async fn get_event(
|
|||||||
.into_response()
|
.into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deletes an event from the database.
|
||||||
|
///
|
||||||
|
/// If you are a teacher, this only works on your own events.
|
||||||
|
/// If you are an admin, this works on any event.
|
||||||
pub async fn delete_event(
|
pub async fn delete_event(
|
||||||
AuthBearer(token): AuthBearer,
|
AuthBearer(token): AuthBearer,
|
||||||
State(app_state): State<AppState>,
|
State(app_state): State<AppState>,
|
||||||
Query(get_event_query): Query<GetEventQuery>,
|
Query(get_event_query): Query<GetEventQuery>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
|
// validate the JWT
|
||||||
let token_data = match handle_token(token, &app_state, Role::Teacher) {
|
let token_data = match handle_token(token, &app_state, Role::Teacher) {
|
||||||
Err(err) => return err,
|
Err(err) => return err,
|
||||||
Ok(token_data) => token_data,
|
Ok(token_data) => token_data,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// query the database to delete the record
|
||||||
let result = query!(
|
let result = query!(
|
||||||
r#"
|
r#"
|
||||||
DELETE FROM events
|
DELETE FROM events
|
||||||
@ -286,19 +292,22 @@ pub async fn delete_event(
|
|||||||
.execute(&app_state.db_pool)
|
.execute(&app_state.db_pool)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
// handle any possible error from the database
|
||||||
match result {
|
match result {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
if result.rows_affected() != 1 {
|
if result.rows_affected() == 1 {
|
||||||
|
(StatusCode::OK, Json(json!({})))
|
||||||
|
} else {
|
||||||
|
// no reccord could be deleted
|
||||||
(
|
(
|
||||||
StatusCode::NOT_FOUND,
|
StatusCode::NOT_FOUND,
|
||||||
Json(json!({
|
Json(json!({
|
||||||
"error": format!("Event {} not found.", get_event_query.id)
|
"error": format!("Event {} not found.", get_event_query.id)
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
(StatusCode::OK, Json(json!({})))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// unknown database error
|
||||||
Err(err) => (
|
Err(err) => (
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Json(json!({
|
Json(json!({
|
||||||
@ -339,7 +348,7 @@ pub async fn create_event(
|
|||||||
match result {
|
match result {
|
||||||
Ok(record) => (StatusCode::OK, Json(json!({ "data": record.id }))).into_response(),
|
Ok(record) => (StatusCode::OK, Json(json!({ "data": record.id }))).into_response(),
|
||||||
Err(err) => (
|
Err(err) => (
|
||||||
StatusCode::BAD_REQUEST,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Json(json!({
|
Json(json!({
|
||||||
"error": format!("Unknown error creating event: {:?}", err)
|
"error": format!("Unknown error creating event: {:?}", err)
|
||||||
})),
|
})),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user