Skip to content

Commit

Permalink
추가: 문화생활 인기 검색어 리스트 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosiee committed Jun 2, 2024
1 parent 85e68ff commit b971a40
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -126,7 +127,7 @@ public ResponseEntity unLikeCulturalEvent(@Positive @PathVariable Long id) {
}


@ApiOperation(value = "문화생활 최근 검색어")
@ApiOperation(value = "문화생활 최근 검색어 리스트 조회")
@GetMapping("/recent-keywords")
public ResponseEntity<List<CulturalEventSearchKeywordListDto>> getCulturalEventRecentKeywordList() {
// Get Recent Keywords
Expand All @@ -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<List<CulturalEventPopularKeywordListDto>> getCulturalEventPopularKeywordList() {
List<CulturalEventPopularKeywordListDto> culturalEventSearchKeywordListDtoList = culturalEventSearchKeywordService.getCulturalEventPopularKeywordList();
return ResponseEntity.status(HttpStatus.OK).body(culturalEventSearchKeywordListDtoList);
}
}


Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,4 +16,7 @@ public interface CulturalEventSearchKeywordRepository extends JpaRepository<Cult
List<CulturalEventSearchKeyword> findByIsRecentTrueAndMemberOrderByUpdatedDateDesc(Member member);

Optional<CulturalEventSearchKeyword> 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<Object[]> findTopKeywordsWithinOneHour(@Param("oneHourAgo") LocalDateTime oneHourAgo, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +17,7 @@
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -86,9 +89,25 @@ public void deleteCulturalEventSearchKeyword(Long id) {
culturalEventSearchKeyword.isRecent = false;
}

/**
* 시간별 인기 검색어 리스트
*
* @return List
*/
public List<CulturalEventPopularKeywordListDto> getCulturalEventPopularKeywordList() {
// Variables
LocalDateTime oneHourAgo = LocalDateTime.now().minusHours(1);
PageRequest pageRequest = PageRequest.of(0, 7); // 첫 번째 페이지, 7개의 결과

// Get Keywords
List<Object[]> 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) {
Expand Down

0 comments on commit b971a40

Please sign in to comment.