2024-08-16 10:50:57 +02:00
package at.eisibaer.jbear2.endpoint
2026-03-15 17:38:23 +01:00
import at.eisibaer.jbear2.dto.board.BoardDto
import at.eisibaer.jbear2.model.User
import at.eisibaer.jbear2.repository.FileRepository
import at.eisibaer.jbear2.repository.UserRepository
import at.eisibaer.jbear2.security.UserDetailsImpl
import at.eisibaer.jbear2.service.BoardService
2024-08-16 10:50:57 +02:00
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
2026-03-15 17:38:23 +01:00
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.web.bind.annotation.*
2024-08-16 10:50:57 +02:00
@RestController
@RequestMapping ( " /api/user " )
2026-03-15 17:38:23 +01:00
class UserEndpoint (
val boardService : BoardService ,
val userRepository : UserRepository ,
val fileRepository : FileRepository ,
) {
2024-08-16 10:50:57 +02:00
val log : Logger = LoggerFactory . getLogger ( UserEndpoint :: class . java ) ;
@GetMapping ( " /test/{pathVar} " )
fun testEndpoint ( @PathVariable pathVar : String , @RequestParam param1 : String ) : ResponseEntity < String > {
log . info ( " test Endpoint! " ) ;
return ResponseEntity . ok ( param1 ) ;
}
2024-08-18 16:26:57 +02:00
@GetMapping ( " /boards " )
2026-03-15 17:38:23 +01:00
fun getBoards ( ) : ResponseEntity < List < BoardDto > ? > {
log . info ( " Get all Board Endpoint " ) ;
val userDetails = SecurityContextHolder . getContext ( ) . authentication . principal as UserDetailsImpl ;
val user : User = userRepository . findUserByUsername ( userDetails . username ) ?: throw UsernameNotFoundException ( " Username ${userDetails.username} not found in DB " ) ;
return boardService . getBoardsByUser ( user ) ;
}
@GetMapping ( " /boards/{boardId} " )
fun getBoardById ( @PathVariable boardId : Long ) : ResponseEntity < BoardDto ? > {
log . info ( " Get Board Endpoint with id {} " , boardId ) ;
val userDetails : UserDetailsImpl = SecurityContextHolder . getContext ( ) . authentication . principal as UserDetailsImpl
val user : User = userRepository . findUserByUsername ( userDetails . username ) ?: throw UsernameNotFoundException ( " Username ${userDetails.username} not found in DB " ) ;
return boardService . getBoardByUserAndId ( user , boardId )
}
@PostMapping ( " /boards " )
fun saveNewBoard ( @RequestBody boardDto : BoardDto ) : ResponseEntity < * > {
log . info ( " Post Board Endpoint " ) ;
val userDetails : UserDetailsImpl = SecurityContextHolder . getContext ( ) . authentication . principal as UserDetailsImpl
val user : User = userRepository . findUserByUsername ( userDetails . username ) ?: throw UsernameNotFoundException ( " Username ${userDetails.username} not found in DB " ) ;
if ( boardDto . id != null ) {
return ResponseEntity . badRequest ( ) . body ( Unit ) ;
}
return boardService . saveBoardToUser ( user , boardDto ) ;
}
@PutMapping ( " /boards/{boardId} " )
fun saveExistingBoard ( @RequestBody boardDto : BoardDto , @PathVariable boardId : Long ) : ResponseEntity < * > {
log . info ( " Put Board Endpoint with id {} " , boardId ) ;
val userDetails : UserDetailsImpl = SecurityContextHolder . getContext ( ) . authentication . principal as UserDetailsImpl
val user : User = userRepository . findUserByUsername ( userDetails . username ) ?: throw UsernameNotFoundException ( " Username ${userDetails.username} not found in DB " ) ;
if ( boardDto . id == null || boardDto . id != boardId ) {
return ResponseEntity . badRequest ( ) . body ( " ID is either null or does not match " )
}
return boardService . saveBoardToUser ( user , boardDto ) ;
}
@DeleteMapping ( " /boards/{boardId} " )
fun saveNewBoard ( @PathVariable boardId : Long ) : ResponseEntity < * > {
log . info ( " Delete Board Endpoint with id {} " , boardId ) ;
val userDetails : UserDetailsImpl = SecurityContextHolder . getContext ( ) . authentication . principal as UserDetailsImpl
val user : User = userRepository . findUserByUsername ( userDetails . username ) ?: throw UsernameNotFoundException ( " Username ${userDetails.username} not found in DB " ) ;
return boardService . deleteBoardOfUser ( user , boardId ) ;
2024-08-18 16:26:57 +02:00
}
2024-08-16 10:50:57 +02:00
}