search method

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

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

Implementation

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

  try {
    response = await executeFuture(
      future: () async {
        return await dio.post(
          searchPath,
          queryParameters: {
            'offset': offSet ?? 0,
            'limit': limit ?? 100,
            'tenantId': DigitDataModelSingleton().tenantId,
            if (query.isDeleted ?? false) 'includeDeleted': query.isDeleted,
          },
          data: entityName == 'User'
              ? query.toMap()
              : {
                  isPlural
                          ? entityNamePlural
                          : entityName == 'ServiceDefinition'
                              ? 'ServiceDefinitionCriteria'
                              : entityName == 'Downsync'
                                  ? 'DownsyncCriteria'
                                  : entityName:
                      isPlural ? [query.toMap()] : query.toMap(),
                },
        );
      },
    );
  } catch (error) {
    return [];
  }

  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>>();
  var mapperRes = <D>[];
  try {
    mapperRes =
        entityList.map((e) => MapperContainer.globals.fromMap<D>(e)).toList();
  } catch (e) {
    rethrow;
  }

  return mapperRes;
}