diff --git a/src/main/java/com/example/reddiserver/controller/SearchController.java b/src/main/java/com/example/reddiserver/controller/SearchController.java index c0f1063..6dbcf09 100644 --- a/src/main/java/com/example/reddiserver/controller/SearchController.java +++ b/src/main/java/com/example/reddiserver/controller/SearchController.java @@ -1,10 +1,9 @@ package com.example.reddiserver.controller; import com.example.reddiserver.common.ApiResponse; -import com.example.reddiserver.dto.post.response.PostResponseDto; +import com.example.reddiserver.dto.search.response.HotPostInfo; +import com.example.reddiserver.dto.search.response.HotPostResponseDto; import com.example.reddiserver.dto.search.response.SearchResponseDto; -import com.example.reddiserver.service.NotionService; -import com.example.reddiserver.service.PostService; import com.example.reddiserver.service.SearchService; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; @@ -28,6 +27,13 @@ public class SearchController { @GetMapping("/") public ApiResponse getSearchList(@RequestParam String keyword, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { SearchResponseDto searchResponseDtoList = searchService.getSearchList(keyword, PageRequest.of(page, size)); - return ApiResponse.successResponse(searchResponseDtoList, "검색 완료"); + return ApiResponse.successResponse(searchResponseDtoList, "검색"); + } + + @Operation(summary = "인기 마케팅 레퍼런스") + @GetMapping("/hot-post") + public ApiResponse getHotPostList() { + HotPostResponseDto hotPostResponseDto = searchService.getHotPostList(); + return ApiResponse.successResponse(hotPostResponseDto, "인기 마케팅 레퍼런스 조회"); } } diff --git a/src/main/java/com/example/reddiserver/repository/PostRepository.java b/src/main/java/com/example/reddiserver/repository/PostRepository.java index b752775..baf9e55 100644 --- a/src/main/java/com/example/reddiserver/repository/PostRepository.java +++ b/src/main/java/com/example/reddiserver/repository/PostRepository.java @@ -24,4 +24,7 @@ public interface PostRepository extends JpaRepository { @Query("SELECT p FROM Post p WHERE p.title like :keyword OR p.content like :keyword") Page findByTitleContainingOrContentContaining(String keyword, Pageable pageable); + + @Query("SELECT p FROM Post p ORDER BY p.view_count DESC") + List findTopNByOrderByViewCountDesc(Pageable pageable); } diff --git a/src/main/java/com/example/reddiserver/service/SearchService.java b/src/main/java/com/example/reddiserver/service/SearchService.java index 90e12fb..75eb357 100644 --- a/src/main/java/com/example/reddiserver/service/SearchService.java +++ b/src/main/java/com/example/reddiserver/service/SearchService.java @@ -1,11 +1,10 @@ package com.example.reddiserver.service; -import com.example.reddiserver.dto.search.response.SearchBrands; -import com.example.reddiserver.dto.search.response.SearchPosts; -import com.example.reddiserver.dto.search.response.SearchResponseDto; +import com.example.reddiserver.dto.search.response.*; import com.example.reddiserver.repository.BrandRepository; import com.example.reddiserver.repository.PostRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -33,4 +32,14 @@ public SearchResponseDto getSearchList(String keyword, Pageable pageable) { .brands(brands) .build(); } + + public HotPostResponseDto getHotPostList() { + List topNPosts = postRepository.findTopNByOrderByViewCountDesc(PageRequest.of(0, 2)).stream() + .map(HotPostInfo::from) + .collect(Collectors.toList()); + + return HotPostResponseDto.builder() + .posts(topNPosts) + .build(); + } }