diff --git a/adapters/out-persistence/src/main/kotlin/com/pokit/out/persistence/content/impl/ContentAdapter.kt b/adapters/out-persistence/src/main/kotlin/com/pokit/out/persistence/content/impl/ContentAdapter.kt index ce8a755b..9c2496db 100644 --- a/adapters/out-persistence/src/main/kotlin/com/pokit/out/persistence/content/impl/ContentAdapter.kt +++ b/adapters/out-persistence/src/main/kotlin/com/pokit/out/persistence/content/impl/ContentAdapter.kt @@ -1,6 +1,5 @@ package com.pokit.out.persistence.content.impl -import com.pokit.category.model.CategoryStatus import com.pokit.content.dto.response.ContentsResult import com.pokit.content.dto.request.ContentSearchCondition import com.pokit.content.model.Content @@ -125,6 +124,35 @@ class ContentAdapter( return SliceImpl(contentResults, pageable, hasNext) } + override fun loadBookmarkedContentsByUserId(userId: Long, pageable: Pageable): Slice { + val contents = queryFactory.select(contentEntity, categoryEntity.name, userLogEntity.count()) + .from(contentEntity) + .leftJoin(userLogEntity).on(userLogEntity.contentId.eq(contentEntity.id)) + .join(categoryEntity).on(categoryEntity.id.eq(contentEntity.categoryId)) + .join(bookmarkEntity).on(bookmarkEntity.contentId.eq(contentEntity.id)) + .where( + categoryEntity.userId.eq(userId), + contentEntity.deleted.isFalse, + ) + .offset(pageable.offset) + .groupBy(contentEntity) + .limit((pageable.pageSize + 1).toLong()) + .orderBy(getSortOrder(contentEntity.createdAt, "createdAt", pageable)) + .fetch() + + val hasNext = getHasNext(contents, pageable) + + val contentResults = contents.map { + ContentsResult.of( + it[contentEntity]!!.toDomain(), + it[categoryEntity.name]!!, + it[userLogEntity.count()]!! + ) + } + + return SliceImpl(contentResults, pageable, hasNext) + } + override fun loadByContentIds(contentIds: List): List = contentRepository.findByIdIn(contentIds) .map { it.toDomain() } diff --git a/application/src/main/kotlin/com/pokit/content/port/out/ContentPort.kt b/application/src/main/kotlin/com/pokit/content/port/out/ContentPort.kt index 09ff7a89..b864db7d 100644 --- a/application/src/main/kotlin/com/pokit/content/port/out/ContentPort.kt +++ b/application/src/main/kotlin/com/pokit/content/port/out/ContentPort.kt @@ -32,4 +32,6 @@ interface ContentPort { fun deleteByUserId(userId: Long) fun loadByContentIds(contentIds: List): List + + fun loadBookmarkedContentsByUserId(userId: Long, pageable: Pageable): Slice } diff --git a/application/src/main/kotlin/com/pokit/content/port/service/ContentService.kt b/application/src/main/kotlin/com/pokit/content/port/service/ContentService.kt index f93fa6a5..807c79f8 100644 --- a/application/src/main/kotlin/com/pokit/content/port/service/ContentService.kt +++ b/application/src/main/kotlin/com/pokit/content/port/service/ContentService.kt @@ -113,20 +113,10 @@ class ContentService( } override fun getBookmarkContents(userId: Long, pageable: Pageable): Slice { - val bookMarks = bookMarkPort.loadByUserId(userId, pageable) - val contentIds = bookMarks.content.map { it.contentId } - val contentsById = contentPort.loadByContentIds(contentIds).associateBy { it.id } - - val remindContents = contentIds.map { contentId -> - val content = contentsById[contentId] - content?.let { - val isRead = userLogPort.isContentRead(it.id, userId) - val category = categoryPort.loadCategoryOrThrow(it.categoryId, userId).toRemindCategory() - it.toRemindContentResult(isRead, category) - } - } - - return SliceImpl(remindContents, pageable, bookMarks.hasNext()) + val bookMarks = contentPort.loadBookmarkedContentsByUserId(userId, pageable) + .map { it.toRemindContentResult() } + + return SliceImpl(bookMarks.content, pageable, bookMarks.hasNext()) } override fun getUnreadContents(userId: Long, pageable: Pageable): Slice {