-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04e1b0e
commit 4efedd9
Showing
9 changed files
with
439 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/no/digdir/service_catalog/model/JsonPatchOperation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package no.digdir.service_catalog.model | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
import com.fasterxml.jackson.annotation.JsonValue | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
data class JsonPatchOperation ( | ||
val op: OpEnum, | ||
val path: String, | ||
val value: Any? = null, | ||
val from: String? = null | ||
) | ||
|
||
enum class OpEnum(val value: String) { | ||
ADD("add"), | ||
REMOVE("remove"), | ||
REPLACE("replace"), | ||
MOVE("move"), | ||
COPY("copy"); | ||
|
||
@JsonValue | ||
fun jsonValue(): String = value | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/no/digdir/service_catalog/service/JsonPatchUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package no.digdir.service_catalog.service | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import jakarta.json.Json | ||
import jakarta.json.JsonException | ||
import no.digdir.service_catalog.model.JsonPatchOperation | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.server.ResponseStatusException | ||
import java.io.StringReader | ||
|
||
inline fun <reified T> patchOriginal(original: T, operations: List<JsonPatchOperation>): T { | ||
validateOperations(operations) | ||
try { | ||
return applyPatch(original, operations) | ||
} catch (ex: Exception) { | ||
when (ex) { | ||
is JsonException -> throw ResponseStatusException(HttpStatus.BAD_REQUEST, ex.message) | ||
is JsonProcessingException -> throw ResponseStatusException(HttpStatus.BAD_REQUEST, ex.message) | ||
is IllegalArgumentException -> throw ResponseStatusException(HttpStatus.BAD_REQUEST, ex.message) | ||
else -> throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, ex.message) | ||
} | ||
} | ||
} | ||
|
||
inline fun <reified T> applyPatch(originalObject: T, operations: List<JsonPatchOperation>): T { | ||
if (operations.isNotEmpty()) { | ||
with(jacksonObjectMapper()) { | ||
val changes = Json.createReader(StringReader(writeValueAsString(operations))).readArray() | ||
val original = Json.createReader(StringReader(writeValueAsString(originalObject))).readObject() | ||
|
||
return Json.createPatch(changes).apply(original) | ||
.let { readValue(it.toString()) } | ||
} | ||
} | ||
return originalObject | ||
} | ||
|
||
fun validateOperations(operations: List<JsonPatchOperation>) { | ||
val invalidPaths = listOf("/id", "/catalogId") | ||
if (operations.any { it.path in invalidPaths }) { | ||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Patch of paths $invalidPaths is not permitted") | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/no/digdir/service_catalog/service/PublicServiceService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
package no.digdir.service_catalog.service | ||
|
||
import no.digdir.service_catalog.model.JsonPatchOperation | ||
import no.digdir.service_catalog.model.PublicService | ||
import no.digdir.service_catalog.mongodb.PublicServiceRepository | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class PublicServiceService(private val publicServiceRepository: PublicServiceRepository) { | ||
private val logger = LoggerFactory.getLogger(PublicServiceService::class.java) | ||
|
||
fun findPublicServicesByCatalogId(catalogId: String) = | ||
publicServiceRepository.getByCatalogId(catalogId) | ||
|
||
fun findPublicServiceById(id: String, catalogId: String) = | ||
publicServiceRepository | ||
.findByIdOrNull(id) | ||
?.takeIf { it.catalogId == catalogId } | ||
|
||
fun patchPublicService(id: String, catalogId: String, operations: List<JsonPatchOperation>): PublicService? = | ||
try { | ||
findPublicServiceById(id, catalogId) | ||
?.let { patchOriginal(it, operations)} | ||
?.let { publicServiceRepository.save(it) } | ||
} catch (ex: Exception) { | ||
logger.error("Failed to update code-list with id $id in catalog $catalogId", ex) | ||
throw ex | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.