Function create_announcement

Source
pub async fn create_announcement(
    __arg0: State<AppState>,
    __arg1: Path<i64>,
    __arg2: Extension<AuthUser>,
    __arg3: Json<AnnouncementRequest>,
) -> impl IntoResponse
Expand description

POST /api/modules/{module_id}/announcements

Creates a new announcement for the specified module.

§AuthZ / AuthN

  • Requires a valid Bearer token (JWT).
  • Caller must be lecturer or assistant_lecturer on the target module (enforced by require_lecturer_or_assistant_lecturer route layer).

§Path Parameters

  • module_id — ID of the module to create the announcement under.

§Request Body

JSON matching AnnouncementRequest:

{
  "title": "Exam Schedule",
  "body": "The exam will be held next **Friday** at 09:00.",
  "pinned": true
}

§Example cURL

curl -X POST "https://your.api/api/modules/101/announcements" \
  -H "Authorization: Bearer <JWT>" \
  -H "Content-Type: application/json" \
  -d '{
        "title": "Exam Schedule",
        "body": "The exam will be held next **Friday** at 09:00.",
        "pinned": true
      }'

§Responses

  • 200 OK — Announcement created successfully. Returns the created record.
  • 400 BAD REQUEST — Malformed JSON.
  • 401 UNAUTHORIZED — Missing/invalid token.
  • 403 FORBIDDEN — Authenticated but not lecturer/assistant on this module.
  • 422 UNPROCESSABLE ENTITY — JSON is valid but required fields missing/invalid.
  • 500 INTERNAL SERVER ERROR — Database error.

§200 OK — Example

{
  "success": true,
  "data": {
    "id": 1234,
    "module_id": 101,
    "user_id": 55,
    "title": "Exam Schedule",
    "body": "The exam will be held next **Friday** at 09:00.",
    "pinned": true,
    "created_at": "2025-02-10T12:34:56Z",
    "updated_at": "2025-02-10T12:34:56Z"
  },
  "message": "Announcement created successfully"
}

§400 Bad Request — Example (invalid JSON)

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

§422 Unprocessable Entity — Example (missing fields)

{
  "success": false,
  "message": "Unprocessable Entity"
}

§403 Forbidden — Example

{
  "success": false,
  "message": "Forbidden"
}

§500 Internal Server Error — Example

{
  "success": false,
  "message": "Failed to create announcement: database error detail ..."
}