comment event endpoint

This commit is contained in:
Mitchell Marino 2023-04-16 23:27:28 -05:00
parent 234ae29e96
commit e0ce62ddb7

View File

@ -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<AppState>,
Query(get_event_query): Query<GetEventQuery>,
) -> 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)
})),