Skip to content

Commit

Permalink
feat: add delete + fix invalidRequest return
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasBousselin committed Sep 3, 2024
1 parent 9497e26 commit 98a8f7d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import com.egm.stellio.search.csr.model.ContextSourceRegistration.Companion.dese
import com.egm.stellio.search.csr.model.serialize
import com.egm.stellio.search.csr.service.ContextSourceRegistrationService
import com.egm.stellio.shared.config.ApplicationProperties
import com.egm.stellio.shared.model.APIException
import com.egm.stellio.shared.model.AccessDeniedException
import com.egm.stellio.shared.model.AlreadyExistsException
import com.egm.stellio.shared.model.ResourceNotFoundException
import com.egm.stellio.shared.model.*
import com.egm.stellio.shared.util.*
import com.egm.stellio.shared.util.JsonUtils.deserializeAsMap
import com.egm.stellio.shared.web.BaseHandler
Expand Down Expand Up @@ -128,6 +125,23 @@ class ContextSourceRegistrationHandler(
{ it }
)

/**
* Implements 6.9.3.3 - Delete ContextSourceRegistration
*/
@DeleteMapping("/{contextSourceRegistrationId}")
suspend fun delete(@PathVariable contextSourceRegistrationId: URI): ResponseEntity<*> = either {
checkContextSourceRegistrationExists(contextSourceRegistrationId).bind()

val sub = getSubFromSecurityContext()
checkIsAllowed(contextSourceRegistrationId, sub).bind()
contextSourceRegistrationService.delete(contextSourceRegistrationId).bind()

ResponseEntity.status(HttpStatus.NO_CONTENT).build<String>()
}.fold(
{ it.toErrorResponse() },
{ it }
)

private suspend fun checkContextSourceRegistrationExists(
contextSourceRegistrationId: URI
): Either<APIException, Unit> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import com.egm.stellio.shared.model.*
import com.egm.stellio.shared.util.*
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_CSR_TERM
import io.r2dbc.postgresql.codec.Json
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate
import org.springframework.data.relational.core.query.Criteria.where
import org.springframework.data.relational.core.query.Query.query
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.r2dbc.core.bind
import org.springframework.stereotype.Component
Expand All @@ -25,6 +28,7 @@ import java.util.regex.Pattern
@Component
class ContextSourceRegistrationService(
private val databaseClient: DatabaseClient,
private val r2dbcEntityTemplate: R2dbcEntityTemplate
) {

suspend fun validateNewContextSourceRegistration(
Expand Down Expand Up @@ -170,6 +174,11 @@ class ContextSourceRegistrationService(
}
}

suspend fun delete(id: URI): Either<APIException, Unit> =
r2dbcEntityTemplate.delete(ContextSourceRegistration::class.java)
.matching(query(where("id").`is`(id)))
.execute()

suspend fun getContextSourceRegistrations(
limit: Int,
offset: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.egm.stellio.shared.util

import com.egm.stellio.shared.model.InvalidRequestException
import com.egm.stellio.shared.model.BadRequestDataException
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_JSON_TERM
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_VALUE_TERM
import com.fasterxml.jackson.annotation.JsonInclude
Expand Down Expand Up @@ -43,7 +43,7 @@ object JsonUtils {
mapper.typeFactory.constructMapLikeType(Map::class.java, String::class.java, Any::class.java)
)
} catch (e: JsonProcessingException) {
throw InvalidRequestException(e.message!!)
throw BadRequestDataException(e.message!!)
}

fun String.deserializeAsMap(): Map<String, Any> =
Expand All @@ -61,7 +61,7 @@ object JsonUtils {
mapper.typeFactory.constructCollectionType(MutableList::class.java, Map::class.java)
)
} catch (e: JsonProcessingException) {
throw InvalidRequestException(e.message!!)
throw BadRequestDataException(e.message!!)
}

fun String.deserializeAsList(): List<Map<String, Any>> =
Expand Down

0 comments on commit 98a8f7d

Please sign in to comment.