Skip to content

Commit

Permalink
[#132] refactor(공유 북마크)
Browse files Browse the repository at this point in the history
- 같은 북마크라면 던지는 예외를 SameBookmarkException으로 변경
- 북마크가 존재하지 않으면 던지는 예외를 BookmarkNotFoundException로 변경
- 권한이 충족되지 않은 경우에 NoPermissionException을 던지도록 변경
- moveBookmarkList에서 folder에 존재하는 북마크의 개수를 줄이는 메소드 refactor
- 동일한 공유폴더로 이동하는 게 아닐 떄 NotSameRootFolderException을 던지도록 변경
  • Loading branch information
Ji-Ha committed Jun 23, 2022
1 parent a59ec74 commit 03baa04
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import com.yapp.web2.domain.folder.entity.Authority
import com.yapp.web2.domain.folder.entity.Folder
import com.yapp.web2.domain.folder.repository.FolderRepository
import com.yapp.web2.exception.ObjectNotFoundException
import com.yapp.web2.exception.custom.BookmarkNotFoundException
import com.yapp.web2.exception.custom.FolderNotFoundException
import com.yapp.web2.exception.custom.SameBookmarkException
import com.yapp.web2.exception.custom.*
import com.yapp.web2.security.jwt.JwtProvider
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
class SharedBookmarkService(
private val bookmarkInterfaceRepository: BookmarkInterfaceRepository,
private val folderRepository: FolderRepository,
Expand All @@ -34,10 +33,9 @@ class SharedBookmarkService(
for (af in account.accountFolderList)
if (af.folder == folder && af.authority > Authority.NONE) return

throw RuntimeException("오류오류! 권한이 없습니다!")
throw NoPermissionException()
}

@Transactional
fun addBookmark(token: String, folderId: Long, bookmarkDto: BookmarkDto.AddBookmarkDto): BookmarkInterface {
val account = jwtProvider.getAccountFromToken(token)
checkAuthority(account, folderId)
Expand All @@ -58,14 +56,13 @@ class SharedBookmarkService(
}

fun checkSameBookmark(bookmarkInterface: BookmarkInterface, folderId: Long) {
val bookmarkList = bookmarkInterfaceRepository.findAllByFolderId(folderId) ?: throw RuntimeException()
val bookmarkList = bookmarkInterfaceRepository.findAllByFolderId(folderId) ?: throw SameBookmarkException()

for (savedBookmark in bookmarkList) {
if (savedBookmark.link == bookmarkInterface.link) throw SameBookmarkException()
}
}

@Transactional
fun deleteBookmark(token: String, dto: BookmarkDto.SharedBookmarkDeleteDto) {
val account = jwtProvider.getAccountFromToken(token)
checkAuthority(account, dto.folderId)
Expand All @@ -87,7 +84,7 @@ class SharedBookmarkService(
val account = jwtProvider.getAccountFromToken(token)
checkAuthority(account, dto.folderId)

val bookmark = bookmarkInterfaceRepository.findBookmarkInterfaceById(bookmarkId) ?: throw RuntimeException()
val bookmark = bookmarkInterfaceRepository.findBookmarkInterfaceById(bookmarkId) ?: throw BookmarkNotFoundException()

bookmark.updateBookmark(dto.title, dto.description)
bookmarkInterfaceRepository.save(bookmark)
Expand All @@ -98,7 +95,6 @@ class SharedBookmarkService(
bookmarkInterfaceRepository.saveAll(bookmarkList)
}

@Transactional
fun moveBookmarkList(token: String, dto: BookmarkDto.MoveBookmarkDto) {
val account = jwtProvider.getAccountFromToken(token)
checkAuthority(account, dto.nextFolderId)
Expand All @@ -107,13 +103,9 @@ class SharedBookmarkService(

for (bookmark in bookmarkList) {
val sharedBookmark = bookmark as SharedBookmark
if (sharedBookmark.isSameRootFolderId(folder.id)) throw RuntimeException("같은 공유보관함이 아닙니다!")
if (!sharedBookmark.isSameRootFolderId(folder.rootFolderId)) throw NotSameRootFolderException()

sharedBookmark.folderId?.let {
folderRepository.findFolderById(it)
}?.run {
this.updateBookmarkCount(-1)
}
deleteBookmarkInfoAtFolder(bookmark)

sharedBookmark.moveFolder(dto.nextFolderId)
sharedBookmark.changeFolderInfo(folder)
Expand All @@ -123,6 +115,14 @@ class SharedBookmarkService(
bookmarkInterfaceRepository.saveAll(bookmarkList)
}

fun deleteBookmarkInfoAtFolder(bookmark: SharedBookmark) {
bookmark.folderId?.let {
folderRepository.findFolderById(it)
}?.run {
this.updateBookmarkCount(-1)
}
}

fun toggleOnRemindBookmark(token: String, bookmarkId: String) {
val account = jwtProvider.getAccountFromToken(token)
val bookmark =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.yapp.web2.exception.custom

import com.yapp.web2.exception.BusinessException
import com.yapp.web2.util.ExceptionMessage

class NoPermissionException : BusinessException(ExceptionMessage.NO_PERMISSION)
8 changes: 8 additions & 0 deletions src/main/kotlin/com/yapp/web2/util/ExceptionMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ class ExceptionMessage {
const val REMIND_CYCLE_VALID_EXCEPTION = "정확한 주기 일자를 입력해주세요."

const val FOLDER_SIZE_EXCEED_EXCEPTION = "하위 폴더는 최대 8개까지 생성을 할 수 있습니다."

const val FOLDER_IS_NOT_ROOT = "보관함이 아닙니다."

const val AlREADY_INVITED = "이미 초대되었습니다."

const val NO_PERMISSION = "권한이 없습니다."

const val NOT_SAME_ROOT_FOLDER = "동일한 보관함이 아닙니다."
}
}

0 comments on commit 03baa04

Please sign in to comment.