From c2a03bdade1d515f3137cd8979722b807d6f2743 Mon Sep 17 00:00:00 2001 From: Jimin Lim <50178026+jimin3263@users.noreply.github.com> Date: Mon, 12 Aug 2024 13:46:00 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=82=AD=EC=A0=9C=EB=90=9C=20=EC=BB=A8?= =?UTF-8?q?=ED=85=90=EC=B8=A0=20=EC=A1=B0=ED=9A=8C=EB=90=98=EB=8A=94=20?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=ED=94=BD=EC=8A=A4=20(#85)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../content/impl/ContentAdapter.kt | 30 ++++++++++++++++++- .../com/pokit/content/port/out/ContentPort.kt | 2 ++ .../content/port/service/ContentService.kt | 18 +++-------- 3 files changed, 35 insertions(+), 15 deletions(-) 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 {