search method

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

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

Implementation

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

    final results = await (selectQuery
          ..where(
            buildAnd(
              [
                if (query.id != null)
                  sql.facility.id.isIn(query.id!)
                else
                  const Constant(true),
                if (query.isPermanent != null)
                  sql.facility.isPermanent.equals(
                    query.isPermanent!,
                  ),
              ],
            ),
          ))
        .get();

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

      return FacilityModel(
        id: facility.id,
        rowVersion: facility.rowVersion,
        tenantId: facility.tenantId,
        isDeleted: facility.isDeleted,
        isPermanent: facility.isPermanent,
        storageCapacity: facility.storageCapacity,
        usage: facility.usage,
        address: address == null
            ? null
            : AddressModel(
                id: address.id,
                relatedClientReferenceId: facility.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,
                rowVersion: address.rowVersion,
              ),
      );
    }).toList();
  });
}