From 6a693cf694c8771bac492cbf6214f78ea8d61d11 Mon Sep 17 00:00:00 2001 From: Simon Njoroge Date: Wed, 27 Nov 2024 19:27:42 +0300 Subject: [PATCH] Overload FhirEngine.get() --- .../com/google/android/fhir/FhirEngine.kt | 24 +++++++++++++++++++ .../android/fhir/impl/FhirEngineImpl.kt | 3 +++ .../google/android/fhir/testing/Utilities.kt | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/engine/src/main/java/com/google/android/fhir/FhirEngine.kt b/engine/src/main/java/com/google/android/fhir/FhirEngine.kt index 4b7ff2cb45..2ddf88ea0f 100644 --- a/engine/src/main/java/com/google/android/fhir/FhirEngine.kt +++ b/engine/src/main/java/com/google/android/fhir/FhirEngine.kt @@ -83,6 +83,17 @@ interface FhirEngine { @Throws(ResourceNotFoundException::class) suspend fun get(type: ResourceType, id: String): Resource + /** + * Loads multiple FHIR resources given [ResourceType] and logical IDs. + * + * @param type The type of the resource to load. + * @param ids The logical IDs of the resources. + * @return The list of requested FHIR resources. + * @throws ResourceNotFoundException if the resources are not found. + */ + @Throws(ResourceNotFoundException::class) + suspend fun get(type: ResourceType, vararg ids: String): List + /** * Updates one or more FHIR [Resource]s in the local storage. * @@ -227,6 +238,19 @@ suspend inline fun FhirEngine.get(id: String): R { return get(getResourceType(R::class.java), id) as R } +/** + * Retrieves FHIR resources of type [R] with the given [ids] from the local storage. + * + * @param R The type of the FHIR resource to retrieve. + * @param ids The logical IDs of the resources to retrieve. + * @return The list of requested FHIR resources. + * @throws ResourceNotFoundException if the resource is not found. + */ +@Throws(ResourceNotFoundException::class) +suspend inline fun FhirEngine.get(vararg ids: String): List { + return ids.map { id -> get(getResourceType(R::class.java), id) as R } +} + /** * Deletes a FHIR resource of type [R] with the given [id] from the local storage. * diff --git a/engine/src/main/java/com/google/android/fhir/impl/FhirEngineImpl.kt b/engine/src/main/java/com/google/android/fhir/impl/FhirEngineImpl.kt index 595f433d9d..daf1e039fc 100644 --- a/engine/src/main/java/com/google/android/fhir/impl/FhirEngineImpl.kt +++ b/engine/src/main/java/com/google/android/fhir/impl/FhirEngineImpl.kt @@ -52,6 +52,9 @@ internal class FhirEngineImpl(private val database: Database, private val contex override suspend fun get(type: ResourceType, id: String) = withContext(Dispatchers.IO) { database.select(type, id) } + override suspend fun get(type: ResourceType, vararg ids: String) = + withContext(Dispatchers.IO) { ids.map { id -> database.select(type, id) } } + override suspend fun update(vararg resource: Resource) = withContext(Dispatchers.IO) { database.update(*resource) } diff --git a/engine/src/main/java/com/google/android/fhir/testing/Utilities.kt b/engine/src/main/java/com/google/android/fhir/testing/Utilities.kt index 1b85a71382..7760d3e5be 100644 --- a/engine/src/main/java/com/google/android/fhir/testing/Utilities.kt +++ b/engine/src/main/java/com/google/android/fhir/testing/Utilities.kt @@ -151,6 +151,10 @@ internal object TestFhirEngineImpl : FhirEngine { return Patient() } + override suspend fun get(type: ResourceType, vararg ids: String): List { + return ids.map { resourceId -> Patient().apply { id = resourceId } } + } + override suspend fun delete(type: ResourceType, id: String) {} override suspend fun search(search: Search): List> {