MatchController.kt

  1. package delta.codecharacter.server.match

  2. import delta.codecharacter.core.MatchApi
  3. import delta.codecharacter.dtos.CreateMatchRequestDto
  4. import delta.codecharacter.dtos.MatchDto
  5. import delta.codecharacter.dtos.MatchModeDto
  6. import delta.codecharacter.server.exception.CustomException
  7. import delta.codecharacter.server.user.UserEntity
  8. import org.springframework.beans.factory.annotation.Autowired
  9. import org.springframework.http.HttpStatus
  10. import org.springframework.http.ResponseEntity
  11. import org.springframework.security.access.annotation.Secured
  12. import org.springframework.security.core.context.SecurityContextHolder
  13. import org.springframework.web.bind.annotation.RestController

  14. @RestController
  15. class MatchController(@Autowired private val matchService: MatchService) : MatchApi {

  16.     @Secured("ROLE_USER")
  17.     override fun createMatch(createMatchRequestDto: CreateMatchRequestDto): ResponseEntity<Unit> {
  18.         if (createMatchRequestDto.mode == MatchModeDto.AUTO) {
  19.             throw CustomException(HttpStatus.BAD_REQUEST, "Users cannot create auto match")
  20.         }
  21.         val user = SecurityContextHolder.getContext().authentication.principal as UserEntity
  22.         matchService.createMatch(user.id, createMatchRequestDto)
  23.         return ResponseEntity.ok().build()
  24.     }

  25.     @Secured("ROLE_USER")
  26.     override fun getTopMatches(): ResponseEntity<List<MatchDto>> {
  27.         return ResponseEntity.ok(matchService.getTopMatches())
  28.     }

  29.     @Secured("ROLE_USER")
  30.     override fun getUserMatches(): ResponseEntity<List<MatchDto>> {
  31.         val user = SecurityContextHolder.getContext().authentication.principal as UserEntity
  32.         return ResponseEntity.ok(matchService.getUserMatches(user.id))
  33.     }
  34. }