Created
June 19, 2018 12:26
-
-
Save lievendoclo/88d3e6f4e300affb617c114f8670433f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.activemq.command.ActiveMQTextMessage | |
import org.springframework.context.event.EventListener | |
import org.springframework.http.HttpStatus | |
import org.springframework.http.ResponseEntity | |
import org.springframework.jms.core.JmsTemplate | |
import org.springframework.jms.core.MessageCreator | |
import org.springframework.web.bind.annotation.GetMapping | |
import org.springframework.web.bind.annotation.PostMapping | |
import javax.jms.Queue | |
import javax.jms.TextMessage | |
// rest infra | |
internal class RestController(private val useCase: UseCase, private val presenter: JsonPresenter) { | |
@PostMapping | |
fun callSomething(body: String): ResponseEntity<*> { | |
return useCase.doStuff(UseCaseRequest(body), presenter) | |
} | |
} | |
data class JsonResult (val jsonValue: String) | |
@Component | |
internal class JsonPresenter : Presenter<ResponseEntity<*>> { | |
override fun present(value: UseCaseResult) = ResponseEntity.ok(JsonResult(value.value)) | |
@ExceptionHandler(UserException::class.java) | |
fun handleUserException(userException: UserException) = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(failReason) | |
} | |
class UserException(failReason: String) : RuntimeException | |
// app api | |
data class UseCaseRequest (val value: String) | |
data class UseCaseResult (val value: String) | |
interface Presenter<T> { | |
fun present(value: UseCaseResult) : T | |
} | |
interface UseCase { | |
<T> fun doStuff(request: UseCaseRequest, presenter: Presenter<T>) : T | |
} | |
// app impl (ommitted interface) | |
internal class UseCaseImpl : UseCase { | |
override <T> fun doStuff(request: UseCaseRequest, presenter: Presenter<T>) : T { | |
if (request.value == "shizzle"){ | |
throw new UserException("We don't take no shizzle") | |
} | |
presenter.present(UseCaseResult("Shizzle 4 life")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment