dany giguere
02/10/2024, 9:14 PM@PostMapping("/posts")
suspend fun create(@Valid @RequestBody postDto: PostDto): ResponseEntity<PostDto> {
val response = postService.create(postDto)
return if (response != null) ResponseEntity.ok(response)
else ResponseEntity.notFound().build()
}
then:
@Service
class PostService(val repository: PostRepository) {
...
suspend fun create(postDto: PostDto): PostDto? =
repository.create(postDto)
and in the repo I have :
suspend fun create(postDto: PostDto): PostDto? =
databaseClient.sql("INSERT INTO posts (title, description) VALUES (:title, :description)")
.filter { statement, _ -> statement.returnGeneratedValues("id").execute() }
.bind("title", postDto.title)
.bind("description", postDto.description)
.fetch()
.first()
.map { row ->
val id = row["id"] as Long
val postEntity = postDto.toEntity().copy(id = id)
postEntity.toDto()
}
.awaitSingleOrNull()
In the controller method, I’m not sure ResponseEntity<PostDto>
is non blocking. Anyone knows ? I’m not sure my exceptionHandler nor my validator are non blocking. The code is available on this repo here: https://github.com/danygiguere/spring-boot-3-reactive-with-kotlin-coroutinesJohan
02/11/2024, 8:47 PMdany giguere
02/11/2024, 10:03 PMJohan
02/12/2024, 7:00 AM