Skip to content

Instantly share code, notes, and snippets.

@Ozsie
Last active April 15, 2018 11:51
Show Gist options
  • Save Ozsie/1be6ca272bc44cc2bfa9af0400ee1f06 to your computer and use it in GitHub Desktop.
Save Ozsie/1be6ca272bc44cc2bfa9af0400ee1f06 to your computer and use it in GitHub Desktop.
Item handling functions
package com.github.ozsie.webfluxtest.handlers
import com.github.ozsie.webfluxtest.ItemRepository
import com.github.ozsie.webfluxtest.model.Item
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
class ItemHandler(private val itemRepository: ItemRepository) {
@Suppress("UNUSED_PARAMETER")
fun getAllItems(request: ServerRequest) = ServerResponse.ok()
.contentType(APPLICATION_JSON)
.body(itemRepository.findAll(), Item::class.java)
fun getItem(request: ServerRequest) = ServerResponse.ok()
.contentType(APPLICATION_JSON)
.body(itemRepository.findById(request.pathVariable("id")), Item::class.java)
.switchIfEmpty(ServerResponse.notFound().build())
fun updateItem(request: ServerRequest) = request.bodyToMono(Item::class.java)
.zipWith(this.itemRepository.findById(request.pathVariable("id")), { item, existingItem ->
Item(existingItem.id, item.value)
}).flatMap(::saveAndRespond).switchIfEmpty(ServerResponse.notFound().build())
fun addItem(request: ServerRequest) = request.bodyToMono(Item::class.java).flatMap(::saveAndRespond)
private fun saveAndRespond(item: Item) = ServerResponse.ok()
.contentType(APPLICATION_JSON)
.body(itemRepository.save(item), Item::class.java)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment