diff --git a/src/main/kotlin/com/yapp/web2/domain/bookmark/entity/Bookmark.kt b/src/main/kotlin/com/yapp/web2/domain/bookmark/entity/Bookmark.kt index 1f2c2270..e0918ad6 100644 --- a/src/main/kotlin/com/yapp/web2/domain/bookmark/entity/Bookmark.kt +++ b/src/main/kotlin/com/yapp/web2/domain/bookmark/entity/Bookmark.kt @@ -85,6 +85,8 @@ class Bookmark( var remindList = mutableListOf() + var shared: Boolean = false + fun restore(): Bookmark { this.deleted = false this.deleteTime = null @@ -96,6 +98,10 @@ class Bookmark( this.description = description } + fun changeSharedTrue() { + this.shared = true + } + fun deletedByFolder() { this.folderId = null this.folderEmoji = "" diff --git a/src/main/kotlin/com/yapp/web2/domain/bookmark/repository/BookmarkRepository.kt b/src/main/kotlin/com/yapp/web2/domain/bookmark/repository/BookmarkRepository.kt index 6654319b..59f76ad5 100644 --- a/src/main/kotlin/com/yapp/web2/domain/bookmark/repository/BookmarkRepository.kt +++ b/src/main/kotlin/com/yapp/web2/domain/bookmark/repository/BookmarkRepository.kt @@ -14,6 +14,8 @@ interface BookmarkRepository : MongoRepository { fun findAllByFolderId(folderId: Long): List + fun findAllByFolderId(folderIdList: List): List + fun findAllByFolderIdAndDeleteTimeIsNullAndRemindTimeIsNotNull(folderId: Long, pageable: Pageable): Page fun findAllByFolderIdAndDeletedIsFalse(folderId: Long, pageable: Pageable): Page diff --git a/src/main/kotlin/com/yapp/web2/domain/folder/controller/FolderController.kt b/src/main/kotlin/com/yapp/web2/domain/folder/controller/FolderController.kt index d5180a53..eec76d4c 100644 --- a/src/main/kotlin/com/yapp/web2/domain/folder/controller/FolderController.kt +++ b/src/main/kotlin/com/yapp/web2/domain/folder/controller/FolderController.kt @@ -113,12 +113,18 @@ class FolderController( } // TODO: 2022/06/22 유저 정보 확인 - @ApiOperation(value = "암호화된 폴더 ID 조회 API") + @ApiOperation(value = "폴더 토큰 발급 API") @GetMapping("encrypt/{folderId}") fun getEncryptFolderId(@PathVariable folderId: Long): ResponseEntity { return ResponseEntity.status(HttpStatus.OK).body(folderService.encryptFolderId(folderId)) } + @ApiOperation(value = "초대를 위한 폴더 토큰 발급 API") + @GetMapping("invite/{folderId}") + fun getFolderInvitationToken(@PathVariable folderId: Long): ResponseEntity { + return ResponseEntity.status(HttpStatus.OK).body(folderService.createFolderInvitationToken(folderId)) + } + @ApiOperation(value = "보관함에 속한 유저 리스트 조회 API") @GetMapping("belong/{folderId}") fun getAccountList(@PathVariable @ApiParam(value = "폴더 ID", example = "2", required = true) folderId: Long): diff --git a/src/main/kotlin/com/yapp/web2/domain/folder/entity/Folder.kt b/src/main/kotlin/com/yapp/web2/domain/folder/entity/Folder.kt index 14d127f9..51576003 100644 --- a/src/main/kotlin/com/yapp/web2/domain/folder/entity/Folder.kt +++ b/src/main/kotlin/com/yapp/web2/domain/folder/entity/Folder.kt @@ -175,9 +175,12 @@ class Folder( this.bookmarkCount += count } - fun setRootFolder(folder: Folder) { - folder.rootFolderId?.let { - this.rootFolderId = it - } ?: let { this.rootFolderId = folder.id } + fun folderToShared(rootFolderId: Long) { + this.rootFolderId = rootFolderId + } + + fun isRootFolder(): Boolean { + if(parentFolder == null) return true + return false } } \ No newline at end of file diff --git a/src/main/kotlin/com/yapp/web2/domain/folder/service/FolderService.kt b/src/main/kotlin/com/yapp/web2/domain/folder/service/FolderService.kt index 8073c423..042e721a 100644 --- a/src/main/kotlin/com/yapp/web2/domain/folder/service/FolderService.kt +++ b/src/main/kotlin/com/yapp/web2/domain/folder/service/FolderService.kt @@ -324,6 +324,42 @@ class FolderService( return childList } + fun createFolderInvitationToken(rootFolderId: Long): FolderTokenDto { + //그냥 재귀를 돌면서 Folder 리스트들을 다 받고, 그 후에 폴더들에 rootFolder 추가, 북마크 shared 변경 하면 되지 않을까? 굳이 재귀? + val folder = folderRepository.findFolderById(rootFolderId) ?: throw FolderNotFoundException() + if(!folder.isRootFolder()) throw RuntimeException("보관함이 아닙니다! 공유를 할 수 없습니다.") + + // 하위 폴더들 모두 rootFolderId 추가해주기 + val sharedFolderIdList = folderToShared(folder, rootFolderId) + + // 하위 북마크들 모두 shared가 true인 상태로 변경해주기 + changeBookmarkToShared(sharedFolderIdList) + + // 토큰 발급 + return FolderTokenDto(jwtProvider.createFolderToken(rootFolderId)) + } + + private fun changeBookmarkToShared(sharedFolderIdList: List) { + val bookmarkList = bookmarkRepository.findAllByFolderId(sharedFolderIdList) + + for (bookmark in bookmarkList) + bookmark.changeSharedTrue() + } + + private fun folderToShared(folder: Folder, rootFolderId: Long): List { + val folderIdList = mutableListOf() + folder.folderToShared(rootFolderId) + folderIdList.add(folder.id) + + folder.children?.let { + for (child in it) { + folderIdList.addAll(folderToShared(child, rootFolderId)) + } + } + + return folderIdList + } + fun encryptFolderId(folderId: Long): FolderTokenDto { val folder = folderRepository.findFolderById(folderId) ?: throw FolderNotFoundException() return FolderTokenDto(jwtProvider.createFolderToken(folderId = folder.id!!))