ishanra

Untitled

Apr 27th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. package org.openg2p.searchservice.api
  2.  
  3. import org.openg2p.searchservice.exceptions.ValidationException
  4. import org.openg2p.searchservice.services.SearchEngine
  5. import org.springframework.http.MediaType
  6. import org.springframework.http.ResponseEntity
  7. import org.springframework.web.bind.annotation.*
  8. import reactor.core.publisher.Flux
  9. import reactor.core.publisher.Mono
  10. import reactor.kotlin.core.publisher.toMono
  11. import javax.validation.Valid
  12.  
  13. @RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
  14. @RestController
  15. class ApiController constructor(private val searchEngine: SearchEngine) {
  16.  
  17. /**
  18. * Endpoint to ensure service is up and running.
  19. * @return
  20. */
  21. @GetMapping("/health")
  22. fun healthCheck(): Mono<ResponseEntity<String>> {
  23. return ResponseEntity.ok("OK").toMono()
  24. }
  25.  
  26. @GetMapping("/backends")
  27. fun backends(payload: Map<String, Any>): Mono<ResponseEntity<List<String>>> {
  28. return ResponseEntity.ok(searchEngine.backends()).toMono()
  29. }
  30.  
  31. @PostMapping("/index")
  32. fun index(@RequestBody payload: Map<String, Any>): Mono<ResponseEntity<Boolean>> {
  33. if ("id" !in payload)
  34. return Mono.error(ValidationException("Index payload requires and 'id' attribute"))
  35. return searchEngine.index(payload["id"]!! as String, payload)
  36. .map { ResponseEntity.ok(it) }
  37. }
  38.  
  39. @DeleteMapping("/index/{id}")
  40. fun delete(@PathVariable id: String): Mono<ResponseEntity<Boolean>> {
  41. return searchEngine.remove(id)
  42. .map { ResponseEntity.ok(it) }
  43. }
  44.  
  45. /**
  46. * Search for beneficiary
  47. */
  48. @PostMapping("/index/search", produces = [MediaType.APPLICATION_JSON_VALUE])
  49. fun search(@Valid @RequestBody query: Query): Mono<ResponseEntity<List<Response>>> {
  50. return searchEngine.search(query).flatMap { ResponseEntity.ok(it).toMono() }
  51. }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment