From b971a4014404a670932c2ed14b9c912eeda2ecf4 Mon Sep 17 00:00:00 2001 From: gabang2 Date: Sun, 2 Jun 2024 17:44:26 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B6=94=EA=B0=80:=20=EB=AC=B8=ED=99=94?= =?UTF-8?q?=EC=83=9D=ED=99=9C=20=EC=9D=B8=EA=B8=B0=20=EA=B2=80=EC=83=89?= =?UTF-8?q?=EC=96=B4=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CulturalEventController.java | 10 +++++++- .../CulturalEventPopularKeywordListDto.java | 13 +++++++++++ .../CulturalEventSearchKeywordRepository.java | 7 ++++++ .../CulturalEventSearchKeywordService.java | 23 +++++++++++++++++-- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/main/java/project/backend/domain/keyword/dto/CulturalEventPopularKeywordListDto.java 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) {