search method

  1. @override
FutureOr<List<BoundaryModel>> search(
  1. BoundarySearchModel query, {
  2. int? offSet,
  3. int? limit,
})
override

The search method searches for entities that match the given query.

Implementation

@override
FutureOr<List<BoundaryModel>> search(
  BoundarySearchModel query, {
  int? offSet,
  int? limit,
}) async {
  Response response;

  response = await executeFuture(
    future: () async {
      return await dio.post(
        searchPath,
        queryParameters: {
          'tenantId': DigitDataModelSingleton().tenantId,
          'includeChildren': true,
          'hierarchyType': DigitDataModelSingleton().hierarchyType,
          ...query.toMap(),
        },
        data: {},
      );
    },
  );

  final responseMap = (response.data);

  if (responseMap is! Map<String, dynamic>) {
    throw InvalidApiResponseException(
      data: query.toMap(),
      path: searchPath,
      response: responseMap,
    );
  }

  if (!responseMap.containsKey(
    (isSearchResponsePlural || entityName == 'ServiceDefinition')
        ? entityNamePlural
        : entityName,
  )) {
    throw InvalidApiResponseException(
      data: query.toMap(),
      path: searchPath,
      response: responseMap,
    );
  }

  final entityResponse = await responseMap[
      (isSearchResponsePlural || entityName == 'ServiceDefinition')
          ? entityNamePlural
          : entityName];
  if (entityResponse is! List) {
    throw InvalidApiResponseException(
      data: query.toMap(),
      path: searchPath,
      response: responseMap,
    );
  }

  final entityList = entityResponse.whereType<Map<String, dynamic>>();

  final boundaryRoot = entityList.firstOrNull;

  if (boundaryRoot == null) return [];
  if (!boundaryRoot.containsKey('boundary')) return [];

  final boundaryList = entityList.first['boundary'];

  /// We can combine both into one function. But better to keep it separate
  /// to help with readability
  List<BoundaryModel> boundaryModelList = _castToBoundaryModel(
    List.castFrom(boundaryList),
  );
  boundaryModelList = _flattenBoundaryMap(
    boundaryModelList,
    i: 1,
  );

  return boundaryModelList;
}