search method

  1. @override
FutureOr<List<ProjectModel>> search(
  1. ProjectSearchModel query, [
  2. String? userId
])
override

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

Implementation

@override
FutureOr<List<ProjectModel>> search(
  ProjectSearchModel query, [
  String? userId,
]) async {
  return retryLocalCallOperation<List<ProjectModel>>(() async {
    final selectQuery = sql.select(sql.project).join([
      leftOuterJoin(
        sql.address,
        sql.address.relatedClientReferenceId.equalsExp(
          sql.project.id,
        ),
      ),
    ]);

    final results = await (selectQuery
          ..where(
            buildAnd(
                [if (query.id != null) sql.project.id.equals(query.id!)]),
          ))
        .get();

    final targetResults = await (selectQuery
          ..where(buildAnd([
            if (query.id != null)
              sql.target.clientReferenceId.equals(query.id!),
          ])))
        .get();

    return results.map((e) {
      final data = e.readTable(sql.project);
      final address = e.readTableOrNull(sql.address);

      return ProjectModel(
        id: data.id,
        tenantId: data.tenantId,
        rowVersion: data.rowVersion,
        name: data.name,
        address: address == null
            ? null
            : AddressModel(
                id: address.id,
                relatedClientReferenceId: data.id,
                tenantId: address.tenantId,
                doorNo: address.doorNo,
                latitude: address.latitude,
                longitude: address.longitude,
                landmark: address.landmark,
                locationAccuracy: address.locationAccuracy,
                addressLine1: address.addressLine1,
                addressLine2: address.addressLine2,
                city: address.city,
                pincode: address.pincode,
                type: address.type,
                boundary: address.boundary,
                boundaryType: address.boundaryType,
                rowVersion: address.rowVersion,
              ),
        targets: targetResults.isEmpty
            ? null
            : targetResults
                .map((e) {
                  final target = e.readTableOrNull(sql.target);
                  if (target == null) return null;

                  return TargetModel(
                    id: target.id,
                    beneficiaryType: target.beneficiaryType,
                  );
                })
                .whereNotNull()
                .toList(),
      );
    }).toList();
  });
}