ProfileController.kt 1.6 KB
Newer Older
Julien Topçu's avatar
Julien Topçu committed
1
package org.craftsrecords.talkadvisor.infra.controller
Julien Topçu's avatar
Julien Topçu committed
2

Julien Topçu's avatar
Julien Topçu committed
3
4
5
import org.craftsrecords.talkadvisor.infra.resources.Preferences
import org.craftsrecords.talkadvisor.infra.resources.Profile
import org.craftsrecords.talkadvisor.infra.resources.toResource
Julien Topçu's avatar
Julien Topçu committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import org.craftsrecords.talkadvisor.recommendation.api.CreateProfile
import org.craftsrecords.talkadvisor.recommendation.profile.ProfileAlreadyExistsException
import org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpServletResponse.SC_CONFLICT

@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)
    }
}