routeGetOne method

WebRoute routeGetOne(
  1. String path, {
  2. required WebRequest rq,
  3. List<String> methods = const [RequestMethods.GET],
  4. Future<ApiDoc>? apiDoc()?,
  5. WaAuthController? auth,
  6. List<String> extraPath = const [],
  7. List<String> excludePaths = const [],
  8. List<String> hosts = const ['*'],
  9. Map<String, Object?> params = const {},
  10. List<String> permissions = const [],
  11. List<int> ports = const [],
})

Creates a route for retrieving a single document by its ID.

This method generates a REST API endpoint that handles fetching individual documents from the collection. The route expects a document ID in the URL path and processes GET requests to return the specified document.

The retrieval operation:

  • Validates the document ID format
  • Fetches the document if it exists
  • Processes the document through form validation for consistent formatting

Parameters:

  • path - URL path for this route (should include {id} placeholder)
  • rq - Web request instance
  • methods - HTTP methods to accept (default: GET)
  • apiDoc - Optional API documentation generator
  • auth - Optional authentication controller
  • extraPath - Additional path segments to match
  • excludePaths - Path segments to exclude from matching
  • hosts - Host names to match (default: all hosts)
  • params - Additional route parameters
  • permissions - Required permissions for access
  • ports - Specific ports to match

Returns a WebRoute configured for retrieving individual documents.

Success response (200):

{
  "success": true,
  "message": "ok",
  "data": {"_id": "...", "name": "...", "email": "..."}
}

Error responses:

  • 404: Document not found
  • 502: Missing or invalid ID

Implementation

WebRoute routeGetOne(
  String path, {
  required WebRequest rq,
  List<String> methods = const [RequestMethods.GET],
  Future<ApiDoc>? Function()? apiDoc,
  WaAuthController<dynamic>? auth,
  List<String> extraPath = const [],
  List<String> excludePaths = const [],
  List<String> hosts = const ['*'],
  Map<String, Object?> params = const {},
  List<String> permissions = const [],
  List<int> ports = const [],
}) {
  Future<String> index() async {
    var id = rq.getParam('id', def: '').toString();

    if (id.isEmpty) {
      return rq.renderData(
        data: {
          'success': false,
          'message': 'id is required',
        },
        status: 502,
      );
    }

    var data = await getById(id);
    if (data == null) {
      return rq.renderData(
        data: {
          'success': false,
          'message': 'id not found',
        },
        status: 404,
      );
    }

    return rq.renderData(data: {
      'success': true,
      'data': data,
      'message': 'ok',
    });
  }

  return WebRoute(
    path: path,
    methods: methods,
    rq: rq,
    apiDoc: apiDoc,
    auth: auth,
    excludePaths: excludePaths,
    extraPath: extraPath,
    hosts: hosts,
    params: params,
    permissions: permissions,
    ports: ports,
    index: index,
  );
}