Skip to content

Commit

Permalink
[#132] refactor(bookmark page)
Browse files Browse the repository at this point in the history
- folderId를 통해 조회할 때, remind 조회 로직, 쿼리 변경
- userId로 조회할 때, 리마인드/전체 조회 로작, 쿼리 변경
- 오늘자 remind bookmark 조회 쿼리 변경
- 불필요한 data dto 클래스 제거
  • Loading branch information
Ji-Ha committed Jul 7, 2022
1 parent 7271868 commit 5544b9c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 134 deletions.
17 changes: 0 additions & 17 deletions src/main/kotlin/com/yapp/web2/domain/bookmark/BookmarkDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,4 @@ class BookmarkDto {
val title: String,
val description: String
)

data class BookmarkRequestDto(
val id: String,
val userId: Long?,
val link: String,
val title: String?,
val description: String?,
val image: String?,
val folderId: Long?,
val folderEmoji: String?,
val folderName: String?,
val clickCount: Int,
val deleteTime: LocalDateTime?,
val deleted: Boolean,
val saveTime: LocalDateTime,
val parentBookmarkId: String?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ class BookmarkPageController(
) {
@GetMapping("/{folderId}")
fun getBookmarkPage(
request: HttpServletRequest,
@PathVariable folderId: Long,
pageable: Pageable,
@RequestParam remind: Boolean
): ResponseEntity<Page<BookmarkDto.BookmarkRequestDto>> {
): ResponseEntity<Page<Bookmark>> {
val accessToken = ControllerUtil.extractAccessToken(request)
return ResponseEntity.status(HttpStatus.OK)
.body(bookmarkPageService.getAllPageByFolderId(folderId, pageable, remind))
.body(bookmarkPageService.getAllPageByFolderId(accessToken, folderId, pageable, remind))
}

@GetMapping("/main")
fun getAllBookmarkPage(
request: HttpServletRequest,
pageable: Pageable,
@RequestParam remind: Boolean
): ResponseEntity<Page<BookmarkDto.BookmarkRequestDto>> {
): ResponseEntity<Page<Bookmark>> {
val token = ControllerUtil.extractAccessToken(request)
return ResponseEntity.status(HttpStatus.OK)
.body(bookmarkPageService.getAllPageByUserId(token, pageable, remind))
Expand All @@ -42,12 +44,11 @@ class BookmarkPageController(
@GetMapping("/trash")
fun getTrashBookmarkPage(
request: HttpServletRequest,
pageable: Pageable,
@RequestParam remind: Boolean
pageable: Pageable
): ResponseEntity<Page<Bookmark>> {
val token = ControllerUtil.extractAccessToken(request)
return ResponseEntity.status(HttpStatus.OK)
.body(bookmarkPageService.getTrashPageByUserId(token, pageable, remind))
.body(bookmarkPageService.getTrashPageByUserId(token, pageable))
}

@GetMapping("/search/{keyWord}")
Expand All @@ -74,7 +75,7 @@ class BookmarkPageController(
fun getBookmarkPageByFolderToken(
@PathVariable folderToken: String,
pageable: Pageable
): ResponseEntity<Page<BookmarkDto.BookmarkRequestDto>> {
): ResponseEntity<Page<Bookmark>> {
return ResponseEntity.status(HttpStatus.OK)
.body(bookmarkPageService.getAllPageByEncryptFolderId(folderToken, pageable))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,11 @@ interface BookmarkRepository : MongoRepository<Bookmark, String> {

fun findAllByFolderId(folderId: Long): List<Bookmark>

fun findAllByFolderIdAndDeleteTimeIsNullAndRemindTimeIsNotNull(folderId: Long, pageable: Pageable): List<Bookmark>
fun findAllByFolderIdAndDeleteTimeIsNullAndRemindTimeIsNotNull(folderId: Long, pageable: Pageable): Page<Bookmark>

fun findAllByFolderIdAndDeleteTimeIsNull(folderId: Long, pageable: Pageable): List<Bookmark>
fun findAllByFolderIdAndDeletedIsFalse(folderId: Long, pageable: Pageable): Page<Bookmark>

fun findAllByFolderIdAndDeleteTimeIsNull(folderId: Long): List<Bookmark>

fun findAllByUserIdAndRemindTimeIsNotNullAndDeleteTimeIsNull(userId: Long, pageable: Pageable): List<Bookmark>

fun findAllByUserIdAndDeleteTimeIsNull(userId: Long, pageable: Pageable): List<Bookmark>

fun findAllByUserIdAndDeleteTimeIsNotNullAndRemindTimeIsNotNull(userId: Long, pageable: Pageable): Page<Bookmark>

fun findAllByUserIdAndDeleteTimeIsNotNull(userId: Long, pageable: Pageable): Page<Bookmark>
fun findAllByUserIdAndDeletedIsTrue(userId: Long, pageable: Pageable): Page<Bookmark>

@Query("{\$and: [{'userId': ?2} , {\$or: [{'title': {\$regex: \".*?0.*\"}}, {\"link\": {\$regex: \".*?1.*\"}}]}]}")
fun findByTitleContainingIgnoreCaseOrLinkContainingIgnoreCaseAndUserId(title: String, link: String, userId: Long, pageable: Pageable): Page<Bookmark>
Expand All @@ -35,8 +27,6 @@ interface BookmarkRepository : MongoRepository<Bookmark, String> {

fun findAllByRemindTimeAndDeleteTimeIsNullAndRemindStatusIsFalse(remindTime: String): List<Bookmark>

fun findAllByRemindTimeAfterAndUserIdAndDeleteTimeIsNull(now: String, userId: Long): List<Bookmark>

fun findAllByUserId(userId: Long): List<Bookmark>

fun findAllByUserIdAndRemindCheckIsFalseAndRemindStatusIsTrueAndRemindTimeIsNotNull(userId: Long): List<Bookmark>
Expand All @@ -45,4 +35,16 @@ interface BookmarkRepository : MongoRepository<Bookmark, String> {

@Query(value = "{ 'remindList': { \$elemMatch: { 'fcmToken' : ?0 } } }")
fun findAllBookmarkByFcmToken(fcmToken: String): List<Bookmark>

@Query(value = "{\$and: [{folderId: {\$in: ?1}}, {deleted: false}, {remindList: {\$elemMatch: {userId : ?0}}}]}")
fun findRemindBookmarkInFolder(userId: Long, folderIdList: List<Long>): List<Bookmark>

@Query(value = "{\$or: [{folderId: {\$in: ?1}}, {userId: ?0}]}")
fun findAllBookmark(userId: Long, folderIdList: List<Long>): List<Bookmark>

@Query(value = "{\$and: [{remindList: {\$elemMatch: {userId: ?0}}}, {remindList: {\$elemMatch: {remindTime: ?1}}}]}")
fun findTodayRemindBookmark(userId: Long, today: String): List<Bookmark>

@Query(value = "{ 'remindList': { \$elemMatch: { 'userId' : ?0 } } }")
fun findRemindBookmark(userId: Long): List<Bookmark>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package com.yapp.web2.domain.bookmark.service

import com.yapp.web2.domain.bookmark.BookmarkDto
import com.yapp.web2.domain.bookmark.entity.Bookmark
import com.yapp.web2.domain.bookmark.entity.BookmarkInterface
import com.yapp.web2.domain.bookmark.repository.BookmarkInterfaceRepository
import com.yapp.web2.domain.bookmark.repository.BookmarkRepository
import com.yapp.web2.domain.folder.entity.Folder
import com.yapp.web2.security.jwt.JwtProvider
import com.yapp.web2.util.AES256Util
import org.springframework.data.domain.Page
Expand All @@ -15,129 +14,82 @@ import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate

@Service
@Transactional(readOnly = true)
class BookmarkPageService(
private val bookmarkRepository: BookmarkRepository,
private val bookmarkInterfaceRepository: BookmarkInterfaceRepository,
private val jwtProvider: JwtProvider,
private val aes256Util: AES256Util
) {
@Transactional(readOnly = true)

fun getAllPageByFolderId(
token: String,
folderId: Long,
pageable: Pageable,
remind: Boolean
): Page<BookmarkDto.BookmarkRequestDto> {
val list = when (remind) {
true -> bookmarkRepository.findAllByFolderIdAndDeleteTimeIsNullAndRemindTimeIsNotNull(folderId, pageable)
false -> bookmarkRepository.findAllByFolderIdAndDeleteTimeIsNull(folderId, pageable)
}
): Page<Bookmark> {
val userId = jwtProvider.getIdFromToken(token)

val bookmarkList = bookmarkToBookmarkRequestDto(list)
val bookmarkInterfaceList =
bookmarkInterfaceToBookmarkRequestDto(bookmarkInterfaceRepository.findAllByFolderId(folderId))
return when (remind) {
true -> {
val bookmarkList =
bookmarkRepository.findRemindBookmarkInFolder(userId, folderIdList = mutableListOf(folderId))
PageImpl(bookmarkList, pageable, bookmarkList.size.toLong())
}
false -> bookmarkRepository.findAllByFolderIdAndDeletedIsFalse(folderId, pageable)
}
}

val result = bookmarkList + bookmarkInterfaceList
return PageImpl(result, pageable, result.size.toLong())
fun getAllPageByEncryptFolderId(token: String, pageable: Pageable): Page<Bookmark> {
val folderId = aes256Util.decrypt(token).toLong()
return bookmarkRepository.findAllByFolderIdAndDeletedIsFalse(folderId, pageable)
}

fun getTrashPageByUserId(token: String, pageable: Pageable, remind: Boolean): Page<Bookmark> {
fun getTrashPageByUserId(token: String, pageable: Pageable): Page<Bookmark> {
val idFromToken = jwtProvider.getIdFromToken(token)
return when (remind) {
true -> bookmarkRepository.findAllByUserIdAndDeleteTimeIsNotNullAndRemindTimeIsNotNull(
idFromToken,
pageable
)
false -> bookmarkRepository.findAllByUserIdAndDeleteTimeIsNotNull(idFromToken, pageable)
}

return bookmarkRepository.findAllByUserIdAndDeletedIsTrue(idFromToken, pageable)
}

fun getAllPageByUserId(token: String, pageable: Pageable, remind: Boolean): Page<BookmarkDto.BookmarkRequestDto> {
val idFromToken = jwtProvider.getIdFromToken(token)
fun getAllPageByUserId(token: String, pageable: Pageable, remind: Boolean): Page<Bookmark> {
val account = jwtProvider.getAccountFromToken(token)

val list = when (remind) {
true -> bookmarkRepository.findAllByUserIdAndRemindTimeIsNotNullAndDeleteTimeIsNull(idFromToken, pageable)
false -> bookmarkRepository.findAllByUserIdAndDeleteTimeIsNull(idFromToken, pageable)
return when (remind) {
true -> {
val bookmarkList =
bookmarkRepository.findRemindBookmark(account.id!!)
PageImpl(bookmarkList, pageable, bookmarkList.size.toLong())
}
false -> {
val folderIdList = mutableListOf<Long>()

for (af in account.accountFolderList)
folderIdList.addAll(getAllLowerFolderId(af.folder))

val bookmarkList = bookmarkRepository.findAllBookmark(account.id!!, folderIdList)
PageImpl(bookmarkList, pageable, bookmarkList.size.toLong())
}
}
}

val bookmarkList = bookmarkToBookmarkRequestDto(list)
val bookmarkInterfaceList =
bookmarkInterfaceToBookmarkRequestDto(bookmarkInterfaceRepository.findAllByUserId(idFromToken))
fun getAllLowerFolderId(parentFolder: Folder): List<Long> {
val folderIdList = mutableListOf<Long>()

val result = bookmarkList + bookmarkInterfaceList
return PageImpl(result, pageable, result.size.toLong())
folderIdList.add(parentFolder.id!!)
parentFolder.children?.let {
for (folder in it)
folderIdList.addAll(getAllLowerFolderId(folder))
}
return folderIdList
}

fun getTodayRemindBookmark(token: String): BookmarkDto.RemindList {
val idFromToken = jwtProvider.getIdFromToken(token)
val yesterday = LocalDate.now().minusDays(1).toString()

return BookmarkDto.RemindList(
bookmarkRepository.findAllByRemindTimeAfterAndUserIdAndDeleteTimeIsNull(
yesterday,
idFromToken
)
bookmarkRepository.findTodayRemindBookmark(idFromToken, yesterday)
)
}

fun getAllPageByEncryptFolderId(token: String, pageable: Pageable): Page<BookmarkDto.BookmarkRequestDto> {
val folderIdByString = aes256Util.decrypt(token)
val bookmarkList =
bookmarkToBookmarkRequestDto(bookmarkRepository.findAllByFolderIdAndDeleteTimeIsNull(folderIdByString.toLong()))
val bookmarkInterfaceList =
bookmarkInterfaceToBookmarkRequestDto(bookmarkInterfaceRepository.findAllByFolderId(folderIdByString.toLong()))

val list = bookmarkList + bookmarkInterfaceList

return PageImpl(list, pageable, list.size.toLong())
}

private fun bookmarkToBookmarkRequestDto(bookmarkList: List<Bookmark>): List<BookmarkDto.BookmarkRequestDto> {
val result = mutableListOf<BookmarkDto.BookmarkRequestDto>()
for (bookmark in bookmarkList) {
result.add(
BookmarkDto.BookmarkRequestDto(
bookmark.id,
bookmark.userId,
bookmark.link,
bookmark.title,
bookmark.description,
bookmark.image,
bookmark.folderId,
bookmark.folderEmoji,
bookmark.folderName,
bookmark.clickCount,
bookmark.deleteTime,
bookmark.deleted,
bookmark.saveTime,
null
)
)
}
return result
}

private fun bookmarkInterfaceToBookmarkRequestDto(bookmarkList: List<BookmarkInterface>): List<BookmarkDto.BookmarkRequestDto> {
val result = mutableListOf<BookmarkDto.BookmarkRequestDto>()
for (bookmark in bookmarkList) {
result.add(
BookmarkDto.BookmarkRequestDto(
bookmark.id,
bookmark.userId,
bookmark.link,
bookmark.title,
bookmark.description,
bookmark.image,
bookmark.folderId,
bookmark.folderEmoji,
bookmark.folderName,
bookmark.clickCount,
bookmark.deleteTime,
bookmark.deleted,
bookmark.saveTime,
null
)
)
}
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal open class BookmarkRepositoryTest {
@Test
fun `폴더 아이디에 해당하는 북마크들을 최신순으로 가져온다`() {
// when
val bookmarkPages = bookmarkRepository.findAllByFolderIdAndDeleteTimeIsNull(1, PageRequest.of(1, 5, Sort.by("saveTime").descending()))
val bookmarkPages = bookmarkRepository.findAllByFolderIdAndDeletedIsFalse(1, PageRequest.of(1, 5, Sort.by("saveTime").descending()))
// then
for (page in bookmarkPages)
assertEquals(1, page.folderId)
Expand Down

0 comments on commit 5544b9c

Please sign in to comment.