search method

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

Implementation

@override
FutureOr<List<ProjectResourceModel>> search(
  ProjectResourceSearchModel query,
) async {
  return retryLocalCallOperation<List<ProjectResourceModel>>(() async {
    final selectQuery = sql.select(sql.projectResource).join([
      leftOuterJoin(
        sql.projectProductVariant,
        sql.projectProductVariant.productVariantId.equalsExp(
          sql.projectResource.resource,
        ),
      ),
    ]);

    final results = await (selectQuery
          ..where(buildAnd([
            if (query.projectId != null)
              sql.projectResource.projectId.isIn(query.projectId!),
          ])))
        .get();

    return results
        .map((e) {
          final projectResource = e.readTable(sql.projectResource);
          final projectProductVariant =
              e.readTableOrNull(sql.projectProductVariant);

          if (projectProductVariant == null) return null;

          return ProjectResourceModel(
            projectId: projectResource.projectId,
            rowVersion: projectResource.rowVersion,
            tenantId: projectResource.tenantId,
            id: projectResource.id,
            isDeleted: projectResource.isDeleted,
            resource: ProjectProductVariantModel(
              productVariantId: projectProductVariant.productVariantId,
              tenantId: projectProductVariant.tenantId,
              type: projectProductVariant.type,
              isBaseUnitVariant: projectProductVariant.isBaseUnitVariant,
            ),
          );
        })
        .whereNotNull()
        .toList();
  });
}