From 389ff4042d36ccc17d2da538b813d5082d6b11af Mon Sep 17 00:00:00 2001 From: Benoit Orihuela Date: Thu, 30 Nov 2023 12:10:02 +0100 Subject: [PATCH] small refactoring and documentation --- .../stellio/search/web/BatchAPIResponses.kt | 2 ++ .../search/web/EntityOperationHandler.kt | 30 ++++++++----------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/web/BatchAPIResponses.kt b/search-service/src/main/kotlin/com/egm/stellio/search/web/BatchAPIResponses.kt index 3d5f6cf9b..552b52639 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/web/BatchAPIResponses.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/web/BatchAPIResponses.kt @@ -56,6 +56,8 @@ typealias JsonLdNgsiLdEntity = Pair fun List.extractNgsiLdEntities(): List = this.map { it.second } fun JsonLdNgsiLdEntity.entityId(): URI = this.second.id +// a temporary data class to hold the result of deserializing, expanding and transforming to NGSI-LD entities +// the entities received in a batch operation data class BatchEntityPreparation( val success: List = emptyList(), val errors: List> = emptyList() diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/web/EntityOperationHandler.kt b/search-service/src/main/kotlin/com/egm/stellio/search/web/EntityOperationHandler.kt index 8be1fe676..3b167748a 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/web/EntityOperationHandler.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/web/EntityOperationHandler.kt @@ -1,10 +1,7 @@ package com.egm.stellio.search.web -import arrow.core.Either -import arrow.core.Option -import arrow.core.left +import arrow.core.* import arrow.core.raise.either -import arrow.core.right import com.egm.stellio.search.authorization.AuthorizationService import com.egm.stellio.search.service.EntityEventService import com.egm.stellio.search.service.EntityOperationService @@ -309,21 +306,18 @@ class EntityOperationHandler( contentType: MediaType? ): BatchEntityPreparation = payload.map { - if (contentType == JSON_LD_MEDIA_TYPE) - expandJsonLdEntityF(it).mapLeft { apiException -> Pair(it[JSONLD_ID_TERM] as String, apiException) } - else - expandJsonLdEntityF(it, listOf(context ?: NGSILD_CORE_CONTEXT)) - .mapLeft { apiException -> Pair(it[JSONLD_ID_TERM] as String, apiException) } - }.map { - when (it) { - is Either.Left -> it.value.left() - is Either.Right -> { - when (val result = it.value.toNgsiLdEntity()) { - is Either.Left -> Pair(it.value.id, result.value).left() - is Either.Right -> Pair(it.value, result.value).right() - } + val jsonLdExpansionResult = + if (contentType == JSON_LD_MEDIA_TYPE) + expandJsonLdEntityF(it) + else + expandJsonLdEntityF(it, listOf(context ?: NGSILD_CORE_CONTEXT)) + jsonLdExpansionResult + .mapLeft { apiException -> Pair(it[JSONLD_ID_TERM] as String, apiException) } + .flatMap { jsonLdEntity -> + jsonLdEntity.toNgsiLdEntity() + .mapLeft { apiException -> Pair(it[JSONLD_ID_TERM] as String, apiException) } + .map { ngsiLdEntity -> Pair(jsonLdEntity, ngsiLdEntity) } } - } }.fold(BatchEntityPreparation()) { acc, entry -> when (entry) { is Either.Left -> acc.copy(errors = acc.errors.plus(entry.value))