search method

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

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

Implementation

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

  response = await executeFuture(
    future: () async {
      return await dio.post(
        searchPath,
        queryParameters: query.clientReferenceId != null &&
                (query.clientReferenceId ?? []).isNotEmpty
            ? {
                'tenantId': AttendanceSingleton().tenantId,
              }
            : {
                'tenantId': AttendanceSingleton().tenantId,
                ...query.toMap(),
              },
        data: query.clientReferenceId != null &&
                (query.clientReferenceId ?? []).isNotEmpty
            ? {
                EntityPlurals.getPluralForEntityName(entityName):
                    query.toMap(),
              }
            : {},
      );
    },
  );

  final responseMap = (response.data);

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

  if (!responseMap.containsKey(
    EntityPlurals.getPluralForEntityName(entityName),
  )) {
    throw InvalidApiResponseException(
      data: query.toMap(),
      path: searchPath,
      response: responseMap,
    );
  }

  final entityResponse =
      await responseMap[EntityPlurals.getPluralForEntityName(entityName)];

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

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

  return entityList.map((e) => AttendanceLogModelMapper.fromMap(e)).toList();
}