WayrApp Backend & Ecosystem Documentation - v1.0.0
    Preparing search index...

    Class ExerciseController

    HTTP controller class for comprehensive exercise management operations within the WayrApp content system.

    Provides RESTful API endpoints for exercise CRUD operations across all supported exercise types, maintains proper HTTP response formatting with comprehensive error handling, and integrates with Express middleware for authentication, validation, and error propagation.

    Index

    Constructors

    Methods

    • Creates a new exercise with comprehensive validation and type-specific data verification.

      Validates request body against CreateExerciseSchema, performs exercise type conversion for internal consistency (dash-to-underscore normalization), delegates exercise creation to the service layer, and returns a standardized API response with HTTP 201 status on successful creation.

      Parameters

      • req: Request

        Express request object containing exercise data in body

      • res: Response

        Express response object for sending HTTP response

      • next: NextFunction

        Express next function for error handling middleware

      Returns Promise<void>

      Resolves when response is sent or error is passed to middleware

      When request body validation fails against CreateExerciseSchema

      When service layer exercise creation fails or validation errors occur

      // POST /api/exercises
      // Request body: { "id": "exercise-translate-hello", "exercise_type": "translation", "data": { "source_text": "Hello", "target_text": "Hola" } }
      // Response: { "data": { exercise object }, "success": true, "timestamp": "2024-01-01T00:00:00.000Z" }
    • Retrieves a single exercise by its unique identifier with comprehensive validation.

      Extracts exercise ID from URL parameters, validates parameter presence, delegates exercise retrieval to the service layer, and returns a standardized API response with HTTP 200 status containing the exercise data with type-specific information.

      Parameters

      • req: Request

        Express request object containing exercise ID in params

      • res: Response

        Express response object for sending HTTP response

      • next: NextFunction

        Express next function for error handling middleware

      Returns Promise<void>

      Resolves when response is sent or error is passed to middleware

      When exercise ID is missing from URL parameters

      When service layer exercise retrieval fails or exercise not found

      // GET /api/exercises/exercise-translate-hello
      // Response: { "data": { exercise object with type-specific data }, "success": true, "timestamp": "2024-01-01T00:00:00.000Z" }
    • Retrieves paginated exercises across all types with comprehensive query support.

      Processes query parameters for pagination, sorting, and filtering options, delegates exercise retrieval to the service layer, and returns a standardized API response with HTTP 200 status. Includes pagination metadata in response headers for client consumption and supports filtering by exercise type through query parameters.

      Parameters

      • req: Request

        Express request object containing query parameters for pagination and filtering

      • res: Response

        Express response object for sending HTTP response with pagination headers

      • next: NextFunction

        Express next function for error handling middleware

      Returns Promise<void>

      Resolves when response is sent or error is passed to middleware

      When service layer exercise retrieval fails

      // GET /api/exercises?page=1&limit=10&sortBy=created_at&sortOrder=desc&exercise_type=translation
      // Response headers: X-Total-Count, X-Total-Pages, X-Current-Page, X-Has-Next, X-Has-Prev
      // Response: { "data": [exercise objects], "success": true, "timestamp": "2024-01-01T00:00:00.000Z" }
    • Retrieves paginated exercises filtered by a specific exercise type with query support.

      Extracts exercise type from URL parameters, validates parameter presence, processes query parameters for pagination and sorting, delegates type-specific exercise retrieval to the service layer, and returns a standardized API response with HTTP 200 status. Includes pagination metadata in response headers for client consumption.

      Parameters

      • req: Request

        Express request object containing exercise type in params and query options

      • res: Response

        Express response object for sending HTTP response with pagination headers

      • next: NextFunction

        Express next function for error handling middleware

      Returns Promise<void>

      Resolves when response is sent or error is passed to middleware

      When exercise type is missing from URL parameters

      When service layer exercise retrieval fails or invalid exercise type provided

      // GET /api/exercises/type/translation?page=1&limit=5&sortBy=created_at&sortOrder=asc
      // Response headers: X-Total-Count, X-Total-Pages, X-Current-Page, X-Has-Next, X-Has-Prev
      // Response: { "data": [translation exercise objects], "success": true, "timestamp": "2024-01-01T00:00:00.000Z" }
    • Updates an existing exercise with partial data and comprehensive validation.

      Extracts exercise ID from URL parameters, validates request body against UpdateExerciseSchema, performs exercise type conversion for internal consistency when type is updated, delegates exercise update to the service layer, and returns a standardized API response with HTTP 200 status containing updated exercise data.

      Parameters

      • req: Request

        Express request object containing exercise ID in params and update data in body

      • res: Response

        Express response object for sending HTTP response

      • next: NextFunction

        Express next function for error handling middleware

      Returns Promise<void>

      Resolves when response is sent or error is passed to middleware

      When exercise ID is missing from URL parameters

      When request body validation fails against UpdateExerciseSchema

      When service layer exercise update fails or exercise not found

      // PUT /api/exercises/exercise-translate-hello
      // Request body: { "data": { "source_text": "Hello world", "target_text": "Hola mundo" } }
      // Response: { "data": { updated exercise object }, "success": true, "timestamp": "2024-01-01T00:00:00.000Z" }
    • Permanently deletes an exercise and removes it from all lesson assignments.

      Extracts exercise ID from URL parameters, validates parameter presence, delegates exercise deletion to the service layer, and returns a standardized API response with HTTP 204 status indicating successful deletion without content. This operation may cascade to remove exercise assignments from lessons.

      Parameters

      • req: Request

        Express request object containing exercise ID in params

      • res: Response

        Express response object for sending HTTP response

      • next: NextFunction

        Express next function for error handling middleware

      Returns Promise<void>

      Resolves when response is sent or error is passed to middleware

      When exercise ID is missing from URL parameters

      When service layer exercise deletion fails or exercise not found

      // DELETE /api/exercises/exercise-translate-hello
      // Response: { "success": true, "message": "Exercise deleted successfully", "timestamp": "2024-01-01T00:00:00.000Z" }