Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@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
You can’t perform that action at this time.