Permalink
Find file
843f050 Jan 14, 2017
@sdeleuze @javamind
94 lines (79 sloc) 3.7 KB
package mixit.controller
import mixit.model.Role
import mixit.model.User
import mixit.repository.UserRepository
import org.springframework.http.MediaType.*
import org.springframework.web.reactive.function.BodyInserters.fromObject
import org.springframework.web.reactive.function.fromPublisher
import org.springframework.web.reactive.function.server.*
import org.springframework.web.reactive.function.server.RequestPredicates.*
import org.springframework.web.reactive.function.server.ServerResponse.created
import org.springframework.web.reactive.function.server.ServerResponse.ok
import java.net.URI
class UserController(val repository: UserRepository) : RouterFunction<ServerResponse> {
override fun route(request: ServerRequest) = route(request) {
accept(TEXT_HTML).apply {
(GET("/user/") or GET("/users/")) { findAllView() }
GET("/user/{login}") { findOneView() }
}
accept(APPLICATION_JSON).apply {
GET("/api/user/") { findAll() }
POST("/api/user/") { create() }
GET("/api/user/{login}") { findOne() }
GET("/api/staff/") { findStaff() }
GET("/api/staff/{login}") { findOneStaff() }
GET("/api/speaker/") { findSpeakers() }
GET("/api/speaker/{login}") { findOneSpeaker() }
GET("/api/sponsor/") { findSponsors() }
GET("/api/sponsor/{login}") { findOneSponsor() }
GET("/api/{event}/speaker/") { findSpeakersByEvent() }
}
}
fun findOneView() = HandlerFunction { req ->
repository.findOne(req.pathVariable("login"))
.then { u -> ok().render("user", mapOf(Pair("user", u))) }
}
fun findAllView() = HandlerFunction {
repository.findAll()
.collectList()
.then { u -> ok().render("users", mapOf(Pair("users", u))) }
}
fun findOne() = HandlerFunction { req ->
ok().contentType(APPLICATION_JSON_UTF8).body(
fromPublisher(repository.findOne(req.pathVariable("login"))))
}
fun findAll() = HandlerFunction {
ok().contentType(APPLICATION_JSON_UTF8).body(fromPublisher(repository.findAll()))
}
fun findStaff() = HandlerFunction {
ok().contentType(APPLICATION_JSON_UTF8).body(fromPublisher(repository.findByRole(Role.STAFF)))
}
fun findOneStaff() = HandlerFunction { req ->
ok().contentType(APPLICATION_JSON_UTF8).body(
fromPublisher(repository.findOneByRole(req.pathVariable("login"), Role.STAFF)))
}
fun findSpeakers() = HandlerFunction {
ok().contentType(APPLICATION_JSON_UTF8).body(fromPublisher(repository.findByRole(Role.SPEAKER)))
}
fun findSpeakersByEvent() = HandlerFunction { req ->
ok().contentType(APPLICATION_JSON_UTF8).body(fromPublisher(repository.findByRoleAndEvent(Role.SPEAKER, req.pathVariable("event"))))
}
fun findOneSpeaker() = HandlerFunction { req ->
ok().contentType(APPLICATION_JSON_UTF8).body(
fromPublisher(repository.findOneByRole(req.pathVariable("login"), Role.SPEAKER)))
}
fun findSponsors() = HandlerFunction {
ok().contentType(APPLICATION_JSON_UTF8).body(fromPublisher(repository.findByRole(Role.SPONSOR)))
}
fun findOneSponsor() = HandlerFunction { req ->
ok().contentType(APPLICATION_JSON_UTF8).body(
fromPublisher(repository.findOneByRole(req.pathVariable("login"), Role.SPONSOR)))
}
fun create() = HandlerFunction { req ->
repository.save(req.bodyToMono<User>()).single()
.then { u -> created(URI.create("/api/user/${u.login}"))
.contentType(APPLICATION_JSON_UTF8)
.body(fromObject(u))
}
}
}