Update Node

- add error handing for
  - foreign key constraint
  - unique constraint
- Update nodes database
This commit is contained in:
2025-03-05 15:58:31 +01:00
parent 1aba25b9b3
commit 10b95057e5
6 changed files with 41 additions and 5 deletions

View File

@@ -4,8 +4,10 @@ import ltd.hlaeja.library.deviceRegistry.Node
import ltd.hlaeja.service.NodeService
import ltd.hlaeja.util.toEntity
import ltd.hlaeja.util.toNodeResponse
import org.springframework.http.HttpStatus.CREATED
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
@RestController
@@ -14,6 +16,7 @@ class NodeController(
) {
@PostMapping("/node")
@ResponseStatus(CREATED)
suspend fun addNode(
@RequestBody request: Node.Request,
): Node.Response = nodeService.addNode(request.toEntity()).toNodeResponse()

View File

@@ -4,6 +4,8 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.UUID
import ltd.hlaeja.entity.NodeEntity
import ltd.hlaeja.repository.NodeRepository
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.http.HttpStatus.BAD_REQUEST
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException
@@ -17,8 +19,11 @@ class NodeService(
suspend fun addNode(
node: NodeEntity,
): NodeEntity = nodeRepository.save(node)
.also { log.debug { "Added node ${it.id}" } }
): NodeEntity = try {
nodeRepository.save(node).also { log.debug { "Added node ${it.id}" } }
} catch (exception: DataIntegrityViolationException) {
throw ResponseStatusException(BAD_REQUEST, null, exception)
}
suspend fun getNodeFromDevice(
device: UUID,