Skip to content

Instantly share code, notes, and snippets.

@beyondxscratch
Last active August 12, 2020 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beyondxscratch/87c9d7d928940825699700366266017e to your computer and use it in GitHub Desktop.
Save beyondxscratch/87c9d7d928940825699700366266017e to your computer and use it in GitHub Desktop.
@RestController
@RequestMapping(path = ["/profiles"])
class ProfileController(private val createProfile: CreateProfile) {
/**
* simulating an authentication context by passing the userId in a header,
* no need to set up all the security frameworks for this demo, but never do that in production !!!
*/
@PostMapping
fun createProfile(@RequestHeader("User-Id") userId: String, @RequestBody preferences: Preferences): ResponseEntity<Profile> {
val profile = createProfile.forUserWithPreferences(userId, preferences.toDomainObject()).toResource()
val location = linkTo(this::class.java).slash(profile).toUri()
return ResponseEntity.created(location).body(profile)
}
@ExceptionHandler(ProfileAlreadyExistsException::class)
fun handleProfileAlreadyExistsException(response: HttpServletResponse, exception: ProfileAlreadyExistsException) {
response.sendError(SC_CONFLICT, exception.message)
}
}
@RestController
@RequestMapping(path = ["/recommendations"])
class RecommendationController(private val recommendTalks: RecommendTalks) {
@PostMapping
fun createRecommendation(@RequestHeader("User-Id") user: String): ResponseEntity<Recommendation> {
val domainRecommendation = recommendTalks to user
val recommendation = domainRecommendation.toResource()
val location = linkTo(this::class.java).slash(recommendation).toUri()
return ResponseEntity.created(location).body(recommendation)
}
@ExceptionHandler(ProfileNotFoundException::class)
fun handleProfileNotFoundException(response: HttpServletResponse, exception: ProfileNotFoundException) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, exception.message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment