100 lines
3.5 KiB
Kotlin
100 lines
3.5 KiB
Kotlin
package at.eisibaer.jbear2.service
|
|
|
|
import at.eisibaer.jbear2.dto.board.BoardDto
|
|
import at.eisibaer.jbear2.model.Board
|
|
import at.eisibaer.jbear2.model.User
|
|
import at.eisibaer.jbear2.repository.BoardRepository
|
|
import at.eisibaer.jbear2.repository.FileRepository
|
|
import at.eisibaer.jbear2.repository.UserRepository
|
|
import at.eisibaer.jbear2.service.mapper.BoardMapper
|
|
import org.slf4j.Logger
|
|
import org.slf4j.LoggerFactory
|
|
import org.springframework.http.HttpStatus
|
|
import org.springframework.http.ResponseEntity
|
|
import org.springframework.stereotype.Service
|
|
import org.springframework.transaction.annotation.Transactional
|
|
import org.springframework.web.multipart.MultipartFile
|
|
|
|
@Service
|
|
class BoardServiceImpl (
|
|
private val fileService: FileService,
|
|
private val boardRepository: BoardRepository,
|
|
private val userRepository: UserRepository,
|
|
private val fileRepository: FileRepository,
|
|
private val boardMapper: BoardMapper,
|
|
) : BoardService {
|
|
|
|
val log: Logger = LoggerFactory.getLogger(BoardServiceImpl::class.java)
|
|
|
|
@Transactional
|
|
override fun getBoardsByUser(user: User): ResponseEntity<List<BoardDto>?> {
|
|
val boards = boardRepository.findAllByOwner(user)
|
|
return ResponseEntity.ok(boardMapper.toDto(boards))
|
|
|
|
}
|
|
|
|
@Transactional
|
|
override fun getBoardByUserAndId(user: User, boardId: Long): ResponseEntity<BoardDto?> {
|
|
val board = boardRepository.findByIdAndOwner(boardId, user) ?: return ResponseEntity.status(404).build()
|
|
|
|
return ResponseEntity.ok(boardMapper.toDto(board))
|
|
}
|
|
|
|
@Transactional
|
|
override fun saveBoardToUser( user: User, boardDto: BoardDto, files: List<MultipartFile>): ResponseEntity<BoardDto> {
|
|
val board = if( boardDto.id == null ){
|
|
Board(owner = user)
|
|
} else {
|
|
boardRepository.findByIdAndOwner(boardDto.id, user) ?: throw NoSuchElementException("Board not found for user " + user.username)
|
|
}
|
|
boardMapper.mapToEntity(board, boardDto, user, userRepository, fileRepository)
|
|
|
|
val savedBoard: Board
|
|
try {
|
|
savedBoard = boardRepository.save(board)
|
|
} catch (ex: Exception) {
|
|
log.error(ex.message, ex)
|
|
return ResponseEntity.badRequest().build()
|
|
}
|
|
|
|
try{
|
|
fileService.saveFiles(files, user, savedBoard)
|
|
} catch (ex: Exception) {
|
|
log.error(ex.message, ex)
|
|
return ResponseEntity.badRequest().build()
|
|
}
|
|
|
|
return ResponseEntity.ok(boardMapper.toDto(savedBoard))
|
|
}
|
|
|
|
override fun deleteBoardOfUser(user: User, boardId: Long): ResponseEntity<Unit> {
|
|
val board = boardRepository.findByIdAndOwner(boardId, user) ?: return ResponseEntity.status(HttpStatus.NOT_FOUND).build()
|
|
|
|
|
|
boardRepository.deleteById( boardId )
|
|
|
|
|
|
//TODO Delete Images
|
|
|
|
return ResponseEntity.ok().build()
|
|
}
|
|
|
|
private fun deleteFilesOfBoard(board: Board) {
|
|
try {
|
|
for (category in board.categories) {
|
|
for (boardEntry in category.boardEntries) {
|
|
if (boardEntry.answer != null && boardEntry.answer!!.image != null) {
|
|
fileService.deleteFile(boardEntry.answer!!.image!!)
|
|
}
|
|
for (question in boardEntry.questions) {
|
|
if (question.image != null) {
|
|
fileService.deleteFile(question.image!!)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (exception: Exception) {
|
|
log.error("Error occured",exception) //TODO
|
|
}
|
|
}
|
|
} |