search method
The search
method searches for entities that match the given query.
Implementation
@override
FutureOr<List<HouseholdModel>> search(
HouseholdSearchModel query, [
String? userId,
]) async {
return retryLocalCallOperation<List<HouseholdModel>>(() async {
final selectQuery = sql.select(sql.household).join(
[
leftOuterJoin(
sql.address,
sql.address.relatedClientReferenceId.equalsExp(
sql.household.clientReferenceId,
),
),
],
);
(selectQuery
..where(
buildAnd(
[
if (query.clientReferenceId != null)
sql.household.clientReferenceId.isIn(query.clientReferenceId!),
if (query.id != null)
sql.household.id.isIn(
query.id!,
),
if (query.tenantId != null)
sql.household.tenantId.equals(
query.tenantId!,
),
if (userId != null)
sql.household.auditCreatedBy.equals(
userId,
),
if (query.latitude != null &&
query.longitude != null &&
query.maxRadius != null &&
query.isProximityEnabled == true)
CustomExpression<bool>('''
(6371393 * acos(
cos(${query.latitude! * math.pi / 180.0}) * cos((address.latitude * ${math.pi / 180.0}))
* cos((address.longitude * ${math.pi / 180.0}) - ${query.longitude! * math.pi / 180.0})
+ sin(${query.latitude! * math.pi / 180.0}) * sin((address.latitude * ${math.pi / 180.0}))
)) <= ${query.maxRadius!}
'''),
],
),
));
final results = await selectQuery.get();
return results
.map((e) {
final household = e.readTable(sql.household);
final address = e.readTableOrNull(sql.address);
return HouseholdModel(
id: household.id,
tenantId: household.tenantId,
clientReferenceId: household.clientReferenceId,
memberCount: household.memberCount,
rowVersion: household.rowVersion,
isDeleted: household.isDeleted,
additionalFields: household.additionalFields != null &&
household.additionalFields.toString().isNotEmpty
? HouseholdAdditionalFieldsMapper.fromJson(
household.additionalFields.toString())
: null,
auditDetails: (household.auditCreatedBy != null &&
household.auditCreatedTime != null)
? AuditDetails(
createdBy: household.auditCreatedBy!,
createdTime: household.auditCreatedTime!,
lastModifiedBy: household.auditModifiedBy,
lastModifiedTime: household.auditModifiedTime,
)
: null,
clientAuditDetails: (household.clientCreatedBy != null &&
household.clientCreatedTime != null)
? ClientAuditDetails(
createdBy: household.clientCreatedBy!,
createdTime: household.clientCreatedTime!,
lastModifiedBy: household.clientModifiedBy,
lastModifiedTime: household.clientModifiedTime,
)
: null,
address: address == null
? null
: AddressModel(
id: address.id,
relatedClientReferenceId: household.clientReferenceId,
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,
locality: address.localityBoundaryCode != null
? LocalityModel(
code: address.localityBoundaryCode!,
name: address.localityBoundaryName,
)
: null,
type: address.type,
rowVersion: address.rowVersion,
auditDetails: (household.auditCreatedBy != null &&
household.auditCreatedBy != null)
? AuditDetails(
createdBy: household.auditCreatedBy!,
createdTime: household.auditCreatedTime!,
lastModifiedBy: household.auditModifiedBy,
lastModifiedTime: household.auditModifiedTime,
)
: null,
clientAuditDetails: (household.clientCreatedBy != null &&
household.clientCreatedTime != null)
? ClientAuditDetails(
createdBy: household.clientCreatedBy!,
createdTime: household.clientCreatedTime!,
lastModifiedBy: household.clientModifiedBy,
lastModifiedTime: household.clientModifiedTime,
)
: null,
),
);
})
.where((element) => element.isDeleted != true)
.toList();
});
}