Skip to content

Commit

Permalink
[#132] fix : compile error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Ji-Ha committed Jun 25, 2022
1 parent 08b3bec commit 8c8d5e2
Showing 1 changed file with 65 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
package com.yapp.web2.domain.bookmark.service

import com.yapp.web2.domain.bookmark.entity.*
import com.yapp.web2.domain.bookmark.repository.BookmarkInterfaceRepository
import com.yapp.web2.domain.folder.entity.Folder
import com.yapp.web2.domain.folder.repository.FolderRepository
import com.yapp.web2.exception.custom.ObjectNotFoundException
import com.yapp.web2.exception.custom.BookmarkNotFoundException
import com.yapp.web2.security.jwt.JwtProvider
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class BookmarkInterfaceService(
private val bookmarkInterfaceRepository: BookmarkInterfaceRepository,
private val folderRepository: FolderRepository,
private val jwtProvider: JwtProvider
) {
fun moveBookmarkList(moveBookmarkDto: BookmarkDto.MoveBookmarkDto) {
val bookmarkIdList = moveBookmarkDto.bookmarkIdList
for (bookmarkId in bookmarkIdList)
moveBookmark(bookmarkId, moveBookmarkDto)
}

fun moveBookmark(bookmarkId: String, moveBookmarkDto: BookmarkDto.MoveBookmarkDto) {
val bookmark = getBookmarkIfPresent(bookmarkId)
val nextFolder = checkFolderAbsence(moveBookmarkDto.nextFolderId)

bookmark.folderId?.let {
if(isSameFolder(it, moveBookmarkDto.nextFolderId)) return
val beforeFolder = checkFolderAbsence(it)
updateBookmarkCountByFolderId(beforeFolder, -1)
}

bookmark.changeFolderInfo(nextFolder)
updateBookmarkCountByFolderId(nextFolder, 1)

bookmarkInterfaceRepository.save(bookmark)
}

@Transactional
protected fun updateBookmarkCountByFolderId(folder: Folder, count: Int) {
folder.bookmarkCount += count
}

private fun isSameFolder(prevFolderId: Long, nextFolderId: Long) = prevFolderId == nextFolderId

private fun getBookmarkIfPresent(bookmarkId: String): BookmarkInterface {
return bookmarkInterfaceRepository.findBookmarkInterfaceById(bookmarkId)
?: throw BookmarkNotFoundException()
}
private fun checkFolderAbsence(folderId: Long): Folder {
return when (val folder = folderRepository.findFolderById(folderId)) {
null -> throw ObjectNotFoundException()
else -> folder
}
}

fun remindToggleOn(token: String, bookmarkId: String) {
val account = jwtProvider.getAccountFromToken(token)
val bookmark = getBookmarkIfPresent(bookmarkId)
//package com.yapp.web2.domain.bookmark.service
//
//import com.yapp.web2.domain.bookmark.BookmarkDto
//import com.yapp.web2.domain.bookmark.entity.*
//import com.yapp.web2.domain.bookmark.repository.BookmarkInterfaceRepository
//import com.yapp.web2.domain.folder.entity.Folder
//import com.yapp.web2.domain.folder.repository.FolderRepository
//import com.yapp.web2.exception.custom.ObjectNotFoundException
//import com.yapp.web2.exception.custom.BookmarkNotFoundException
//import com.yapp.web2.security.jwt.JwtProvider
//import org.springframework.stereotype.Service
//import org.springframework.transaction.annotation.Transactional
//
//@Service
//class BookmarkInterfaceService(
// private val bookmarkInterfaceRepository: BookmarkInterfaceRepository,
// private val folderRepository: FolderRepository,
// private val jwtProvider: JwtProvider
//) {
// fun moveBookmarkList(moveBookmarkDto: BookmarkDto.MoveBookmarkDto) {
// val bookmarkIdList = moveBookmarkDto.bookmarkIdList
// for (bookmarkId in bookmarkIdList)
// moveBookmark(bookmarkId, moveBookmarkDto)
// }
//
// fun moveBookmark(bookmarkId: String, moveBookmarkDto: BookmarkDto.MoveBookmarkDto) {
// val bookmark = getBookmarkIfPresent(bookmarkId)
// val nextFolder = checkFolderAbsence(moveBookmarkDto.nextFolderId)
//
// bookmark.folderId?.let {
// if (isSameFolder(it, moveBookmarkDto.nextFolderId)) return
// val beforeFolder = checkFolderAbsence(it)
// updateBookmarkCountByFolderId(beforeFolder, -1)
// }
//
// bookmark.changeFolderInfo(nextFolder)
// updateBookmarkCountByFolderId(nextFolder, 1)
//
// bookmarkInterfaceRepository.save(bookmark)
// }
//
// @Transactional
// protected fun updateBookmarkCountByFolderId(folder: Folder, count: Int) {
// folder.bookmarkCount += count
// }
//
// private fun isSameFolder(prevFolderId: Long, nextFolderId: Long) = prevFolderId == nextFolderId
//
// private fun getBookmarkIfPresent(bookmarkId: String): BookmarkInterface {
// return bookmarkInterfaceRepository.findBookmarkInterfaceById(bookmarkId)
// ?: throw BookmarkNotFoundException()
// }
//
// private fun checkFolderAbsence(folderId: Long): Folder {
// return when (val folder = folderRepository.findFolderById(folderId)) {
// null -> throw ObjectNotFoundException()
// else -> folder
// }
// }
//
// fun remindToggleOn(token: String, bookmarkId: String) {
// val account = jwtProvider.getAccountFromToken(token)
// val bookmark = getBookmarkIfPresent(bookmarkId)
// }
//}

0 comments on commit 8c8d5e2

Please sign in to comment.