Skip to content

Commit

Permalink
[#168] feature(공유 북마크 권한 설정)
Browse files Browse the repository at this point in the history
- 공유 북마크에 대한 추가, 삭제, 이동, remind on/off, 업데이트 부분에서 사용자의 권한을 확인하는 API 추가
- shared 북마크에 접근하는 uri를 sharedBookmark에서 shared로 수정함
- Folder Entity에서 동일한 보관함인지 확인하는 로직 추가
  • Loading branch information
Ji-Ha committed Jul 24, 2022
1 parent 3c965ea commit 1f19068
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import javax.servlet.http.HttpServletRequest
import javax.validation.Valid

@RestController
@RequestMapping("/api/v1/sharedBookmark")
@RequestMapping("/api/v1/shared")
class SharedBookmarkController(
private val sharedBookmarkService: SharedBookmarkService
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.yapp.web2.domain.bookmark.service

import com.yapp.web2.domain.account.entity.Account
import com.yapp.web2.domain.bookmark.BookmarkDto
import com.yapp.web2.domain.bookmark.entity.BookmarkInterface
import com.yapp.web2.domain.bookmark.entity.SharedBookmark
import com.yapp.web2.domain.bookmark.repository.BookmarkInterfaceRepository
import com.yapp.web2.domain.bookmark.entity.Bookmark
import com.yapp.web2.domain.bookmark.entity.Remind
import com.yapp.web2.domain.bookmark.repository.BookmarkRepository
import com.yapp.web2.domain.folder.entity.Authority
import com.yapp.web2.domain.folder.entity.Folder
import com.yapp.web2.domain.folder.repository.FolderRepository
Expand All @@ -16,7 +16,7 @@ import org.springframework.transaction.annotation.Transactional
@Service
@Transactional
class SharedBookmarkService(
private val bookmarkInterfaceRepository: BookmarkInterfaceRepository,
private val bookmarkRepository: BookmarkRepository,
private val folderRepository: FolderRepository,
private val jwtProvider: JwtProvider
) {
Expand All @@ -35,18 +35,18 @@ class SharedBookmarkService(
throw NoPermissionException()
}

fun addBookmark(token: String, folderId: Long, bookmarkDto: BookmarkDto.AddBookmarkDto): BookmarkInterface {
fun addBookmark(token: String, folderId: Long, bookmarkDto: BookmarkDto.AddBookmarkDto): Bookmark {
val account = jwtProvider.getAccountFromToken(token)
checkAuthority(account, folderId)

val folder = checkFolderAbsence(folderId)
val bookmark = BookmarkDto.addBookmarkDtoToSharedBookmark(bookmarkDto, account, folder)
val bookmark = BookmarkDto.addBookmarkDtoToBookmark(bookmarkDto, account)

bookmark.changeFolderInfo(folder.id, folder.emoji, folder.name)
bookmark.changeFolderInfo(folder)
checkSameBookmark(bookmark, folderId)
folder.updateBookmarkCount(1)

return bookmarkInterfaceRepository.save(bookmark)
return bookmarkRepository.save(bookmark)
}

fun addBookmarkList(token: String, folderId: Long, dto: BookmarkDto.AddBookmarkListDto) {
Expand All @@ -59,67 +59,65 @@ class SharedBookmarkService(
return folderRepository.findFolderById(folderId) ?: throw ObjectNotFoundException()
}

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

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

fun deleteBookmark(token: String, dto: BookmarkDto.SharedBookmarkDeleteDto) {
val account = jwtProvider.getAccountFromToken(token)
checkAuthority(account, dto.folderId)

val bookmarkList = bookmarkInterfaceRepository.findAllById(dto.dotoriIdList)
val bookmarkList = bookmarkRepository.findAllById(dto.dotoriIdList)
val folder = checkFolderAbsence(dto.folderId)

bookmarkInterfaceRepository.deleteAllByParentBookmarkId(dto.dotoriIdList)

for (bookmark in bookmarkList) {
bookmark.deleteBookmark()
folder.updateBookmarkCount(-1)
}

bookmarkInterfaceRepository.saveAll(bookmarkList)
bookmarkRepository.saveAll(bookmarkList)
}

fun updateBookmark(token: String, bookmarkId: String, dto: BookmarkDto.UpdateSharedBookmarkDto) {
val account = jwtProvider.getAccountFromToken(token)
checkAuthority(account, dto.folderId)

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

bookmark.updateBookmark(dto.title, dto.description)
bookmarkInterfaceRepository.save(bookmark)

val bookmarkList = bookmarkInterfaceRepository.findAllByParentBookmarkId(bookmarkId)
for (b in bookmarkList) b.updateBookmark(dto.title, dto.description)
val toChangeBookmark = bookmarkRepository.findBookmarkById(bookmarkId) ?: throw BookmarkNotFoundException()

bookmarkInterfaceRepository.saveAll(bookmarkList)
toChangeBookmark.updateBookmark(dto.title, dto.description)
bookmarkRepository.save(toChangeBookmark)
}

fun moveBookmarkList(token: String, dto: BookmarkDto.MoveBookmarkDto) {
val account = jwtProvider.getAccountFromToken(token)
checkAuthority(account, dto.nextFolderId)
val bookmarkList = bookmarkInterfaceRepository.findAllById(dto.bookmarkIdList)
checkSameRootFolder(dto.folderId, dto.nextFolderId)
val bookmarkList = bookmarkRepository.findAllById(dto.bookmarkIdList)
val folder = folderRepository.findFolderById(dto.nextFolderId) ?: throw FolderNotFoundException()

for (bookmark in bookmarkList) {
val sharedBookmark = bookmark as SharedBookmark
if (!sharedBookmark.isSameRootFolderId(folder.rootFolderId)) throw NotSameRootFolderException()

deleteBookmarkInfoAtFolder(bookmark)

sharedBookmark.moveFolder(dto.nextFolderId)
sharedBookmark.changeFolderInfo(folder)
bookmark.moveFolder(dto.nextFolderId)
bookmark.changeFolderInfo(folder)
folder.updateBookmarkCount(1)
}

bookmarkInterfaceRepository.saveAll(bookmarkList)
bookmarkRepository.saveAll(bookmarkList)
}

fun deleteBookmarkInfoAtFolder(bookmark: SharedBookmark) {
fun checkSameRootFolder(beforeFolderId: Long, nextFolderId: Long) {
val beforeFolder = folderRepository.findFolderById(beforeFolderId) ?: throw FolderNotFoundException()
val nextFolder = folderRepository.findFolderById(nextFolderId) ?: throw FolderNotFoundException()

if(!beforeFolder.isFolderSameRootFolder(nextFolder)) throw NotSameRootFolderException()
}

fun deleteBookmarkInfoAtFolder(bookmark: Bookmark) {
bookmark.folderId?.let {
folderRepository.findFolderById(it)
}?.run {
Expand All @@ -130,23 +128,25 @@ class SharedBookmarkService(
fun toggleOnRemindBookmark(token: String, bookmarkId: String) {
val account = jwtProvider.getAccountFromToken(token)
val bookmark =
bookmarkInterfaceRepository.findBookmarkInterfaceById(bookmarkId) ?: throw BookmarkNotFoundException()
val copyBookmark = BookmarkInterface.copyBookmark(account, bookmark)
bookmarkRepository.findBookmarkById(bookmarkId) ?: throw BookmarkNotFoundException()
val folderId = bookmark.folderId ?: throw RuntimeException("폴더에 속해있지 않습니다!")
val remind = Remind(account)

bookmark.remindOn(account.id!!)
checkAuthority(account, folderId)

bookmarkInterfaceRepository.save(copyBookmark)
bookmarkInterfaceRepository.save(bookmark)
bookmark.remindOn(remind)
bookmarkRepository.save(bookmark)
}

fun toggleOffRemindBookmark(token: String, bookmarkId: String) {
val account = jwtProvider.getAccountFromToken(token)
val bookmark =
bookmarkInterfaceRepository.findBookmarkInterfaceById(bookmarkId) ?: throw BookmarkNotFoundException()
bookmarkRepository.findBookmarkById(bookmarkId) ?: throw BookmarkNotFoundException()
val folderId = bookmark.folderId ?: throw RuntimeException("폴더에 속해있지 않습니다!")
checkAuthority(account, folderId)

bookmarkInterfaceRepository.deleteByParentBookmarkIdAndUserId(bookmarkId, account.id!!)
bookmark.remindOff(account.id!!)

bookmarkInterfaceRepository.save(bookmark)
bookmarkRepository.save(bookmark)
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/yapp/web2/domain/folder/entity/Folder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,9 @@ class Folder(
if(parentFolder == null) return true
return false
}

fun isFolderSameRootFolder(folder: Folder): Boolean {
if(folder.rootFolderId == this.rootFolderId) return true
return false
}
}

0 comments on commit 1f19068

Please sign in to comment.