api/routes/modules/assignments/tasks/
delete.rs

1use axum::{
2    extract::{State, Path},
3    http::StatusCode,
4    response::IntoResponse,
5    Json,
6};
7use sea_orm::DbErr;
8use db::models::assignment_task;
9use util::state::AppState;
10use crate::response::ApiResponse;
11
12/// DELETE /api/modules/{module_id}/assignments/{assignment_id}/tasks/{task_id}
13///
14/// Delete a specific task from an assignment. Only accessible by lecturers or admins assigned to the module.
15///
16/// ### Path Parameters
17/// - `module_id` (i64): The ID of the module containing the assignment
18/// - `assignment_id` (i64): The ID of the assignment containing the task
19/// - `task_id` (i64): The ID of the task to delete
20///
21/// ### Responses
22///
23/// - `200 OK`
24/// ```json
25/// {
26///   "success": true,
27///   "message": "Task deleted successfully"
28/// }
29/// ```
30///
31/// - `404 Not Found`
32/// ```json
33/// {
34///   "success": false,
35///   "message": "Assignment or module not found" // or "Task not found"
36/// }
37/// ```
38///
39/// - `500 Internal Server Error`
40/// ```json
41/// {
42///   "success": false,
43///   "message": "Database error" // or "Failed to delete task"
44/// }
45/// ```
46///
47pub async fn delete_task(
48    State(app_state): State<AppState>,
49    Path((_, _, task_id)): Path<(i64, i64, i64)>,
50) -> impl IntoResponse {
51    let db = app_state.db();
52
53    match assignment_task::Model::delete(db, task_id).await {
54        Ok(_) => (
55            StatusCode::OK,
56            Json(ApiResponse::success((), "Task deleted successfully")),
57        )
58            .into_response(),
59        Err(DbErr::RecordNotFound(_)) => (
60            StatusCode::NOT_FOUND,
61            Json(ApiResponse::<()>::error("Task not found")),
62        )
63            .into_response(),
64        Err(_) => (
65            StatusCode::INTERNAL_SERVER_ERROR,
66            Json(ApiResponse::<()>::error("Failed to delete task")),
67        )
68            .into_response(),
69    }
70}