Skip to content

Commit

Permalink
feat : update hot-post method controller, service, repository
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungGyo-00 committed Jan 28, 2024
1 parent 17aedef commit b7ecfb0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,6 +27,13 @@ public class SearchController {
@GetMapping("/")
public ApiResponse<SearchResponseDto> 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<HotPostResponseDto> getHotPostList() {
HotPostResponseDto hotPostResponseDto = searchService.getHotPostList();
return ApiResponse.successResponse(hotPostResponseDto, "인기 마케팅 레퍼런스 조회");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ public interface PostRepository extends JpaRepository<Post, Long> {

@Query("SELECT p FROM Post p WHERE p.title like :keyword OR p.content like :keyword")
Page<Post> findByTitleContainingOrContentContaining(String keyword, Pageable pageable);

@Query("SELECT p FROM Post p ORDER BY p.view_count DESC")
List<Post> findTopNByOrderByViewCountDesc(Pageable pageable);
}
15 changes: 12 additions & 3 deletions src/main/java/com/example/reddiserver/service/SearchService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,4 +32,14 @@ public SearchResponseDto getSearchList(String keyword, Pageable pageable) {
.brands(brands)
.build();
}

public HotPostResponseDto getHotPostList() {
List<HotPostInfo> topNPosts = postRepository.findTopNByOrderByViewCountDesc(PageRequest.of(0, 2)).stream()
.map(HotPostInfo::from)
.collect(Collectors.toList());

return HotPostResponseDto.builder()
.posts(topNPosts)
.build();
}
}

0 comments on commit b7ecfb0

Please sign in to comment.