85 lines
3.9 KiB
Kotlin
85 lines
3.9 KiB
Kotlin
package at.eisibaer.jbear2.endpoint
|
|
|
|
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
|
|
import org.slf4j.Logger
|
|
import org.slf4j.LoggerFactory
|
|
import org.springframework.http.ResponseEntity
|
|
import org.springframework.security.core.context.SecurityContextHolder
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException
|
|
import org.springframework.web.bind.annotation.*
|
|
|
|
@RestController
|
|
@RequestMapping("/api/user")
|
|
class UserEndpoint(
|
|
val boardService: BoardService,
|
|
val userRepository: UserRepository,
|
|
val fileRepository: FileRepository,
|
|
) {
|
|
|
|
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);
|
|
}
|
|
|
|
@GetMapping("/boards")
|
|
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 );
|
|
}
|
|
} |