diff --git a/src/main/java/project/backend/domain/culturalevent/controller/CulturalEventController.java b/src/main/java/project/backend/domain/culturalevent/controller/CulturalEventController.java index e9fc402..f425299 100644 --- a/src/main/java/project/backend/domain/culturalevent/controller/CulturalEventController.java +++ b/src/main/java/project/backend/domain/culturalevent/controller/CulturalEventController.java @@ -15,6 +15,7 @@ import project.backend.domain.culturalevent.service.CulturalEventService; import project.backend.domain.culturalevnetcategory.entity.CategoryTitle; import project.backend.domain.culturalevnetinfo.service.CulturalEventInfoService; +import project.backend.domain.keyword.dto.CulturalEventPopularKeywordListDto; import project.backend.domain.keyword.dto.CulturalEventSearchKeywordListDto; import project.backend.domain.keyword.entity.CulturalEventSearchKeyword; import project.backend.domain.keyword.mapper.CulturalEventSearchKeywordMapper; @@ -126,7 +127,7 @@ public ResponseEntity unLikeCulturalEvent(@Positive @PathVariable Long id) { } - @ApiOperation(value = "문화생활 최근 검색어") + @ApiOperation(value = "문화생활 최근 검색어 리스트 조회") @GetMapping("/recent-keywords") public ResponseEntity> getCulturalEventRecentKeywordList() { // Get Recent Keywords @@ -150,6 +151,13 @@ public ResponseEntity deleteCulturalEventRecentKeyword(@Positive @PathVariable L culturalEventSearchKeywordService.deleteCulturalEventSearchKeyword(id); return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); } + + @ApiOperation(value = "문화생활 인기 검색어 리스트 조회") + @GetMapping("/popular-keywords") + public ResponseEntity> getCulturalEventPopularKeywordList() { + List culturalEventSearchKeywordListDtoList = culturalEventSearchKeywordService.getCulturalEventPopularKeywordList(); + return ResponseEntity.status(HttpStatus.OK).body(culturalEventSearchKeywordListDtoList); + } } diff --git a/src/main/java/project/backend/domain/keyword/dto/CulturalEventPopularKeywordListDto.java b/src/main/java/project/backend/domain/keyword/dto/CulturalEventPopularKeywordListDto.java new file mode 100644 index 0000000..39d7426 --- /dev/null +++ b/src/main/java/project/backend/domain/keyword/dto/CulturalEventPopularKeywordListDto.java @@ -0,0 +1,13 @@ +package project.backend.domain.keyword.dto; + +import lombok.*; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CulturalEventPopularKeywordListDto { + private Integer ordering; + private String keyword; +} \ No newline at end of file diff --git a/src/main/java/project/backend/domain/keyword/repository/CulturalEventSearchKeywordRepository.java b/src/main/java/project/backend/domain/keyword/repository/CulturalEventSearchKeywordRepository.java index 422717e..75b0fd6 100644 --- a/src/main/java/project/backend/domain/keyword/repository/CulturalEventSearchKeywordRepository.java +++ b/src/main/java/project/backend/domain/keyword/repository/CulturalEventSearchKeywordRepository.java @@ -1,9 +1,13 @@ package project.backend.domain.keyword.repository; +import io.lettuce.core.dynamic.annotation.Param; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import project.backend.domain.keyword.entity.CulturalEventSearchKeyword; import project.backend.domain.member.entity.Member; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -12,4 +16,7 @@ public interface CulturalEventSearchKeywordRepository extends JpaRepository findByIsRecentTrueAndMemberOrderByUpdatedDateDesc(Member member); Optional findFirstByMemberAndKeywordAndIsRecentTrue(Member member, String keyword); + + @Query("SELECT k.keyword, COUNT(k) AS freq FROM CulturalEventSearchKeyword k WHERE k.updatedDate >= :oneHourAgo GROUP BY k.keyword ORDER BY freq DESC") + List findTopKeywordsWithinOneHour(@Param("oneHourAgo") LocalDateTime oneHourAgo, Pageable pageable); } diff --git a/src/main/java/project/backend/domain/keyword/service/CulturalEventSearchKeywordService.java b/src/main/java/project/backend/domain/keyword/service/CulturalEventSearchKeywordService.java index 0cefca9..f8ae036 100644 --- a/src/main/java/project/backend/domain/keyword/service/CulturalEventSearchKeywordService.java +++ b/src/main/java/project/backend/domain/keyword/service/CulturalEventSearchKeywordService.java @@ -1,8 +1,10 @@ package project.backend.domain.keyword.service; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import project.backend.domain.keyword.dto.CulturalEventPopularKeywordListDto; import project.backend.domain.keyword.entity.CulturalEventSearchKeyword; import project.backend.domain.keyword.repository.CulturalEventSearchKeywordRepository; import project.backend.domain.member.entity.Member; @@ -15,6 +17,7 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import java.util.stream.IntStream; @Service @RequiredArgsConstructor @@ -86,9 +89,25 @@ public void deleteCulturalEventSearchKeyword(Long id) { culturalEventSearchKeyword.isRecent = false; } + /** + * 시간별 인기 검색어 리스트 + * + * @return List + */ + public List getCulturalEventPopularKeywordList() { + // Variables + LocalDateTime oneHourAgo = LocalDateTime.now().minusHours(1); + PageRequest pageRequest = PageRequest.of(0, 7); // 첫 번째 페이지, 7개의 결과 + + // Get Keywords + List topKeywords = culturalEventSearchKeywordRepository.findTopKeywordsWithinOneHour(oneHourAgo, pageRequest); + + // Make Dto List + return IntStream.range(0, topKeywords.size()) + .mapToObj(i -> CulturalEventPopularKeywordListDto.builder().ordering(i + 1).keyword((String) topKeywords.get(i)[0]).build()) + .collect(Collectors.toList()); + } - // 인기 키워드 생성(1시간 지난 키워드(is_deleted 중에서) 삭제) - // TODO : 아직 개발하지 않았음. // 키워드 검증 private CulturalEventSearchKeyword verifiedCulturalEventSearchKeyword(Long id) {