Skip to content

Commit

Permalink
feat: 최근검색어 모두 삭제 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
junhaesung authored Sep 30, 2023
1 parent d16613b commit 167cc0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ class SearchHistoryController(
)
return ApiResponse.success()
}
}

@DeleteMapping
fun deleteAll(
@ApiIgnore authentication: Authentication,
): ApiResponse<*> {
searchHistoryService.deleteAll(
memberId = authentication.resolveMemberId(),
)
return ApiResponse.success()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import java.time.LocalDateTime
interface SearchHistoryRepository : JpaRepository<SearchHistory, Long> {
fun findByMember_memberId(memberId: Long, pageable: Pageable): List<SearchHistory>
fun findByMember_memberIdAndSearchHistoryId(memberId: Long, searchHistoryId: Long): SearchHistory?
fun findByMember_memberId(memberId: Long): List<SearchHistory>
@Query(
value = "SELECT search_word as searchWord, count(search_history_id) AS searchCount " +
"FROM search_history " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SearchHistoryVo>
}

Expand Down Expand Up @@ -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<SearchHistoryVo> {
return searchHistoryRepository.findByMember_memberId(
memberId = memberId,
pageable = PageRequest.of(0, 10, Sort.Direction.DESC, "searchedAt")
).map { SearchHistoryVo.from(it) }
}
}
}

0 comments on commit 167cc0c

Please sign in to comment.