diff --git a/src/events.rs b/src/events.rs index ee30062..fc1d774 100644 --- a/src/events.rs +++ b/src/events.rs @@ -262,16 +262,22 @@ pub async fn get_event( .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( AuthBearer(token): AuthBearer, State(app_state): State, Query(get_event_query): Query, ) -> impl IntoResponse { + // validate the JWT let token_data = match handle_token(token, &app_state, Role::Teacher) { Err(err) => return err, Ok(token_data) => token_data, }; + // query the database to delete the record let result = query!( r#" DELETE FROM events @@ -286,19 +292,22 @@ pub async fn delete_event( .execute(&app_state.db_pool) .await; + // handle any possible error from the database match 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, Json(json!({ "error": format!("Event {} not found.", get_event_query.id) })), ) - } else { - (StatusCode::OK, Json(json!({}))) } } + // unknown database error Err(err) => ( StatusCode::INTERNAL_SERVER_ERROR, Json(json!({ @@ -339,7 +348,7 @@ pub async fn create_event( match result { Ok(record) => (StatusCode::OK, Json(json!({ "data": record.id }))).into_response(), Err(err) => ( - StatusCode::BAD_REQUEST, + StatusCode::INTERNAL_SERVER_ERROR, Json(json!({ "error": format!("Unknown error creating event: {:?}", err) })),