Function get_modules

Source
pub async fn get_modules(
    __arg0: State<AppState>,
    __arg1: Extension<AuthUser>,
    __arg2: Query<FilterReq>,
) -> impl IntoResponse
Expand description

GET /api/modules

Retrieves a paginated and optionally filtered list of modules.

§Arguments

The arguments are automatically extracted from query parameters via the FilterReq struct:

  • page: (Optional) The page number for pagination. Defaults to 1 if not provided. Minimum value is 1.
  • per_page: (Optional) The number of items per page. Defaults to 20. Maximum is 100. Minimum is 1.
  • query: (Optional) A general search string that filters modules by code or description.
  • code: (Optional) A filter to match specific module codes.
  • year: (Optional) A filter to match modules by academic year.
  • sort: (Optional) A comma-separated list of fields to sort by. Prefix with - for descending order (e.g., -year).

Allowed sort fields: "code", "created_at", "year", "credits", "description".

§Returns

Returns an HTTP response indicating the result:

  • 200 OK with a list of matching modules, paginated and wrapped in a standardized response format.
  • 400 BAD REQUEST if an invalid field is used for sorting.
  • 500 INTERNAL SERVER ERROR if a database error occurs while retrieving the modules.

The response body contains:

  • A paginated list of modules.
  • Metadata: current page, items per page, and total items.

§Example Response

  • 200 OK
{
  "success": true,
  "data": {
    "modules": [
      {
        "id": 1,
        "code": "CS101",
        "year": 2024,
        "description": "Introduction to Computer Science",
        "credits": 15,
        "created_at": "2024-01-15T10:00:00Z",
        "updated_at": "2024-01-15T10:00:00Z"
      }
    ],
    "page": 1,
    "per_page": 20,
    "total": 57
  },
  "message": "Modules retrieved successfully"
}
  • 400 Bad Request
{
  "success": false,
  "message": "Invalid field used for sorting"
}
  • 500 Internal Server Error
{
  "success": false,
  "message": "An internal server error occurred"
}