search method

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

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

Implementation

@override
FutureOr<List<FacilityModel>> search(
  FacilitySearchModel query, {
  int? offSet,
  int? limit,
}) async {
  int defaultBatchSize = limit ?? 100; // Default batch size for fetching data
  int currentOffset = offSet ?? 0;

  List<FacilityModel> allResults = [];

  //To fetch the totalCount from the first Response
  bool flag = true;

  //Total number of records
  var totalCount = 0;

  do {
    Response response;

    //Execute the request
    try {
      response = await executeFuture(
        future: () async {
          return await dio.post(
            searchPath,
            queryParameters: {
              'offset': currentOffset,
              'limit': defaultBatchSize,
              'tenantId': DigitDataModelSingleton().tenantId,
              if (query.isDeleted ?? false) 'includeDeleted': query.isDeleted,
            },
            data: {
              isPlural ? entityNamePlural : entityName:
                  isPlural ? [query.toMap()] : query.toMap(),
            },
          );
        },
      );
    } catch (error) {
      break; // Break out of the loop if an error occurs
    }

    final responseMap = response.data;

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

    String key = (isSearchResponsePlural) ? entityNamePlural : entityName;

    //Check whether the response contains valid key and totalCount
    if (!responseMap.containsKey(key) ||
        !responseMap.containsKey('TotalCount')) {
      throw InvalidApiResponseException(
        data: query.toMap(),
        path: searchPath,
        response: responseMap,
      );
    }

    //Fetch the totalCount of records only from the first response
    if (flag && responseMap.containsKey('TotalCount')) {
      totalCount = responseMap['TotalCount'];
      flag = false;
    }

    final entityResponse = await responseMap[key];

    if (entityResponse is! List) {
      throw InvalidApiResponseException(
        data: query.toMap(),
        path: searchPath,
        response: responseMap,
      );
    }

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

    List<FacilityModel> currentBatch;

    try {
      currentBatch = entityList
          .map((e) => MapperContainer.globals.fromMap<FacilityModel>(e))
          .toList();
    } catch (e) {
      rethrow;
    }

    allResults.addAll(currentBatch);
    currentOffset += defaultBatchSize;
    totalCount -= defaultBatchSize;

    //If remaining record is less than defaultBatchSize, adjust the Batch size
    if (totalCount < defaultBatchSize) defaultBatchSize = totalCount;
  } while (totalCount > 0);

  return allResults;
}