Function create_task

Source
pub async fn create_task(
    __arg0: State<AppState>,
    __arg1: Path<(i64, i64)>,
    __arg2: Json<CreateTaskRequest>,
) -> impl IntoResponse
Expand description

POST /api/modules/{module_id}/assignments/{assignment_id}/tasks

Create a new task for a given assignment. Accessible to users with Lecturer or Admin roles assigned to the module.

Each task must have a unique task_number within the assignment. The name field defines a short, human-readable title for the task, while the command field defines how the task will be executed during evaluation (e.g., test commands, build commands).

§Path Parameters

  • module_id (i64): The ID of the module containing the assignment
  • assignment_id (i64): The ID of the assignment to add the task to

§Request Body

{
  "task_number": 1,
  "name": "Unit Tests",
  "command": "cargo test --lib"
}

§Request Body Fields

  • task_number (i64, required): Unique sequential number for the task within the assignment
  • name (string, required): Short descriptive name for the task (e.g., “Compile”, “Unit Tests”)
  • command (string, required): Command to execute for this task (e.g., test commands, build scripts)

§Example Request

curl -X POST http://localhost:3000/api/modules/1/assignments/2/tasks \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "task_number": 1,
    "name": "Unit Tests",
    "command": "cargo test --lib"
  }'

§Success Response (201 Created)

{
  "success": true,
  "message": "Task created successfully",
  "data": {
    "id": 123,
    "task_number": 1,
    "name": "Unit Tests",
    "command": "cargo test --lib",
    "created_at": "2024-01-01T00:00:00Z",
    "updated_at": "2024-01-01T00:00:00Z"
  }
}

§Error Responses

400 Bad Request - Invalid JSON body

{
  "success": false,
  "message": "Invalid JSON body"
}

403 Forbidden - Insufficient permissions

{
  "success": false,
  "message": "Access denied"
}

404 Not Found - Assignment or module not found

{
  "success": false,
  "message": "Assignment or module not found"
}

422 Unprocessable Entity - Validation errors

{
  "success": false,
  "message": "Invalid task_number, name, or command"
}

or

{
  "success": false,
  "message": "task_number must be unique"
}

500 Internal Server Error - Database or server error

{
  "success": false,
  "message": "Failed to create task"
}

§Validation Rules

  • task_number must be greater than 0
  • name must not be empty or whitespace-only
  • command must not be empty or whitespace-only
  • task_number must be unique within the assignment
  • Assignment must exist and belong to the specified module

§Notes

  • Tasks are executed in order of their task_number during assignment evaluation
  • The command field supports any shell command that can be executed in the evaluation environment
  • Task creation is restricted to users with appropriate module permissions