Update Node
- add error handing for - foreign key constraint - unique constraint - Update nodes database
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user