-
Notifications
You must be signed in to change notification settings - Fork 4
/
TestHandler.kt
26 lines (22 loc) · 1.01 KB
/
TestHandler.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.yunikov.api.test
import com.yunikov.api.errors.ErrorHandler
import com.yunikov.domain.test.TestService
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import reactor.core.publisher.Mono
@Component
class TestHandler(private val testService: TestService,
private val errorHandler: ErrorHandler) {
fun test(request: ServerRequest): Mono<ServerResponse> {
return request.principal()
.flatMap { principal -> testService.test(principal.name) }
.map { result -> TestResponse(result) }
.flatMap { ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(Mono.just(it), TestResponse::class.java)
}
.onErrorResume({ error -> errorHandler.handleAndRespond(error) })
}
}