Skip to content

Commit 44a8769

Browse files
authored
[feat #262] 카테고리 삭제 시 관련 엔티티 삭제 기능 (#263)
* feat : 컨텐츠 id 목록으로 북마크 삭제 쿼리 구현 * feat : 카테고리 id로 컨텐츠 삭제 쿼리 구현 * feat : 카테고리 삭제 시 북마크, 컨텐츠 삭제
1 parent 88826a9 commit 44a8769

File tree

7 files changed

+44
-0
lines changed

7 files changed

+44
-0
lines changed

adapters/out-persistence/src/main/kotlin/com/pokit/out/persistence/bookmark/impl/BookMarkAdapter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class BookMarkAdapter(
4141
override fun isBookmarked(contentId: Long, userId: Long): Boolean =
4242
bookMarkRepository.existsByContentIdAndUserIdAndDeleted(contentId, userId, false)
4343

44+
override fun deleteByContentIds(contentIds: List<Long>, userId: Long) {
45+
bookMarkRepository.deleteByContentIdsAndUserId(contentIds, userId)
46+
}
47+
4448
override fun loadByUserId(userId: Long, pageable: Pageable): Slice<Bookmark> =
4549
bookMarkRepository.findByUserIdAndDeleted(userId, false, pageable)
4650
.map { it.toDomain() }

adapters/out-persistence/src/main/kotlin/com/pokit/out/persistence/bookmark/persist/BookMarkRepository.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package com.pokit.out.persistence.bookmark.persist
33
import org.springframework.data.domain.Pageable
44
import org.springframework.data.domain.Slice
55
import org.springframework.data.jpa.repository.JpaRepository
6+
import org.springframework.data.jpa.repository.Modifying
7+
import org.springframework.data.jpa.repository.Query
8+
import org.springframework.data.repository.query.Param
69

710
interface BookMarkRepository : JpaRepository<BookmarkEntity, Long>, BookmarkQuerydslRepository {
811
fun findByContentIdAndUserIdAndDeleted(
@@ -16,4 +19,16 @@ interface BookMarkRepository : JpaRepository<BookmarkEntity, Long>, BookmarkQuer
1619
fun existsByContentIdAndUserIdAndDeleted(contentId: Long, userId: Long, deleted: Boolean): Boolean
1720

1821
fun countByUserIdAndDeleted(userId: Long, deleted: Boolean): Int
22+
23+
@Modifying(clearAutomatically = true)
24+
@Query(
25+
"""
26+
update BookmarkEntity b set b.deleted = true
27+
where b.contentId in :contentIds and b.userId = :userId and b.deleted = false
28+
"""
29+
)
30+
fun deleteByContentIdsAndUserId(
31+
@Param("contentIds") contentIds: List<Long>,
32+
@Param("userId") userId: Long
33+
)
1934
}

adapters/out-persistence/src/main/kotlin/com/pokit/out/persistence/content/impl/ContentAdapter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ class ContentAdapter(
259259
return contentRepository.findByIdOrNull(id)?.toDomain()
260260
}
261261

262+
override fun deleteByCategoryId(categoryId: Long) {
263+
contentRepository.deleteByCategoryId(categoryId)
264+
}
265+
262266
override fun loadByContentIds(contentIds: List<Long>): List<Content> =
263267
contentRepository.findByIdIn(contentIds)
264268
.map { it.toDomain() }

adapters/out-persistence/src/main/kotlin/com/pokit/out/persistence/content/persist/ContentRepository.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,13 @@ interface ContentRepository : JpaRepository<ContentEntity, Long>, ContentJdbcRep
8787
@Param("contentIds") contentIds: List<Long>,
8888
@Param("categoryId") categoryId: Long
8989
)
90+
91+
@Modifying(clearAutomatically = true)
92+
@Query(
93+
"""
94+
update ContentEntity c set c.deleted = true
95+
where c.categoryId = :categoryId
96+
"""
97+
)
98+
fun deleteByCategoryId(categoryId: Long)
9099
}

application/src/main/kotlin/com/pokit/bookmark/port/out/BookmarkPort.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ interface BookmarkPort {
1414
fun loadByUserId(userId: Long, pageable: Pageable): Slice<Bookmark>
1515

1616
fun isBookmarked(contentId: Long, userId: Long): Boolean
17+
18+
fun deleteByContentIds(contentIds: List<Long>, userId: Long)
1719
}

application/src/main/kotlin/com/pokit/category/port/service/CategoryService.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pokit.category.port.service
22

3+
import com.pokit.bookmark.port.out.BookmarkPort
34
import com.pokit.category.dto.CategoriesResponse
45
import com.pokit.category.dto.CategoryCommand
56
import com.pokit.category.dto.DuplicateCategoryCommandV2
@@ -19,6 +20,7 @@ import com.pokit.common.exception.NotFoundCustomException
1920
import com.pokit.content.port.out.ContentPort
2021
import com.pokit.user.model.User
2122
import com.pokit.user.port.out.UserPort
23+
import org.springframework.data.domain.PageRequest
2224
import org.springframework.data.domain.Pageable
2325
import org.springframework.data.domain.Slice
2426
import org.springframework.data.domain.SliceImpl
@@ -33,6 +35,7 @@ class CategoryService(
3335
private val contentPort: ContentPort,
3436
private val sharedCategoryPort: SharedCategoryPort,
3537
private val userPort: UserPort,
38+
private val bookmarkPort: BookmarkPort,
3639
) : CategoryUseCase {
3740
companion object {
3841
private const val MAX_CATEGORY_COUNT = 30
@@ -92,6 +95,11 @@ class CategoryService(
9295
val category = categoryPort.loadByIdAndUserId(categoryId, userId)
9396
?: throw NotFoundCustomException(CategoryErrorCode.NOT_FOUND_CATEGORY)
9497

98+
val contents = contentPort.loadByUserIdAndCategoryName(category.userId, category.categoryName, PageRequest.of(0, MAX_CATEGORY_COUNT))
99+
val contentIds = contents.map { it.contentId }.toMutableList()
100+
101+
bookmarkPort.deleteByContentIds(contentIds, userId)
102+
contentPort.deleteByCategoryId(categoryId)
95103
categoryPort.delete(category)
96104
}
97105

application/src/main/kotlin/com/pokit/content/port/out/ContentPort.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ interface ContentPort {
5858
fun loadAllByKeyword(userId: Long, searchKeywords: List<InterestType>, pageable: Pageable): Slice<ContentsResult>
5959

6060
fun loadById(id: Long): Content?
61+
62+
fun deleteByCategoryId(categoryId: Long)
6163
}

0 commit comments

Comments
 (0)