diff --git a/nplusone-api/src/main/kotlin/com/haeseong/nplusone/ui/search/SearchHistoryController.kt b/nplusone-api/src/main/kotlin/com/haeseong/nplusone/ui/search/SearchHistoryController.kt index dc2d4c4..c355f3d 100644 --- a/nplusone-api/src/main/kotlin/com/haeseong/nplusone/ui/search/SearchHistoryController.kt +++ b/nplusone-api/src/main/kotlin/com/haeseong/nplusone/ui/search/SearchHistoryController.kt @@ -40,4 +40,14 @@ class SearchHistoryController( ) return ApiResponse.success() } -} \ No newline at end of file + + @DeleteMapping + fun deleteAll( + @ApiIgnore authentication: Authentication, + ): ApiResponse<*> { + searchHistoryService.deleteAll( + memberId = authentication.resolveMemberId(), + ) + return ApiResponse.success() + } +} diff --git a/nplusone-domain/src/main/kotlin/com/haeseong/nplusone/domain/search/history/SearchHistoryRepository.kt b/nplusone-domain/src/main/kotlin/com/haeseong/nplusone/domain/search/history/SearchHistoryRepository.kt index b2b0d9b..bb82880 100644 --- a/nplusone-domain/src/main/kotlin/com/haeseong/nplusone/domain/search/history/SearchHistoryRepository.kt +++ b/nplusone-domain/src/main/kotlin/com/haeseong/nplusone/domain/search/history/SearchHistoryRepository.kt @@ -11,6 +11,7 @@ import java.time.LocalDateTime interface SearchHistoryRepository : JpaRepository { fun findByMember_memberId(memberId: Long, pageable: Pageable): List fun findByMember_memberIdAndSearchHistoryId(memberId: Long, searchHistoryId: Long): SearchHistory? + fun findByMember_memberId(memberId: Long): List @Query( value = "SELECT search_word as searchWord, count(search_history_id) AS searchCount " + "FROM search_history " + diff --git a/nplusone-domain/src/main/kotlin/com/haeseong/nplusone/domain/search/history/SearchHistoryService.kt b/nplusone-domain/src/main/kotlin/com/haeseong/nplusone/domain/search/history/SearchHistoryService.kt index 1b8b522..47ce249 100644 --- a/nplusone-domain/src/main/kotlin/com/haeseong/nplusone/domain/search/history/SearchHistoryService.kt +++ b/nplusone-domain/src/main/kotlin/com/haeseong/nplusone/domain/search/history/SearchHistoryService.kt @@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional interface SearchHistoryService { fun record(searchEvent: SearchEvent): SearchHistoryVo fun delete(memberId: Long, searchHistoryId: Long) + fun deleteAll(memberId: Long) fun getSearchHistories(memberId: Long): List } @@ -48,10 +49,16 @@ class SearchHistoryServiceImpl( ?.run { searchHistoryRepository.delete(this) } } + @Transactional + override fun deleteAll(memberId: Long) { + searchHistoryRepository.findByMember_memberId(memberId) + .forEach { searchHistoryRepository.delete(it) } + } + override fun getSearchHistories(memberId: Long): List { return searchHistoryRepository.findByMember_memberId( memberId = memberId, pageable = PageRequest.of(0, 10, Sort.Direction.DESC, "searchedAt") ).map { SearchHistoryVo.from(it) } } -} \ No newline at end of file +}