From 3bb815e143fbf67e914c1bc020d36954f443b7a0 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sun, 3 Mar 2024 09:30:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[chore]=20#29=20todo=20=EC=A3=BC=EC=84=9D?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/service/PostService.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/harang/server/service/PostService.java b/src/main/java/org/harang/server/service/PostService.java index 4f898c4..0080c05 100644 --- a/src/main/java/org/harang/server/service/PostService.java +++ b/src/main/java/org/harang/server/service/PostService.java @@ -77,6 +77,7 @@ public List getAllPosts() { .collect(Collectors.toList()); } + // TODO: 이전 호출 결과도 중복되어 반환되는 문제 원인 파악 public List getSearchResults(String title) { return postRepository.findByTitleIsContaining(title) .stream() From 9ff9091a8edf606877f5dec2090624f4a95fdf6c Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sun, 3 Mar 2024 10:06:53 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[feat]=20#29=20=EC=B6=94=EC=B2=9C=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/PostController.java | 7 +++++++ .../harang/server/service/PostService.java | 21 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/harang/server/controller/PostController.java b/src/main/java/org/harang/server/controller/PostController.java index 271046b..e5e876f 100644 --- a/src/main/java/org/harang/server/controller/PostController.java +++ b/src/main/java/org/harang/server/controller/PostController.java @@ -8,7 +8,9 @@ import org.harang.server.dto.common.ApiResponse; import org.harang.server.dto.request.PostRequest; import org.harang.server.dto.response.PostResponse; +import org.harang.server.dto.type.ErrorMessage; import org.harang.server.dto.type.SuccessMessage; +import org.harang.server.exception.CustomException; import org.harang.server.service.PostService; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; @@ -47,4 +49,9 @@ public ApiResponse> getAllPosts() { public ApiResponse getSearchResults(@RequestParam(name = "title") String title) { return ApiResponse.success(postService.getSearchResults(title)); } + + @GetMapping("/recommend") + public ApiResponse getRecommendedResults() { + return ApiResponse.success(postService.getRecommendedPosts()); + } } diff --git a/src/main/java/org/harang/server/service/PostService.java b/src/main/java/org/harang/server/service/PostService.java index 0080c05..e266d2c 100644 --- a/src/main/java/org/harang/server/service/PostService.java +++ b/src/main/java/org/harang/server/service/PostService.java @@ -1,5 +1,7 @@ package org.harang.server.service; +import java.util.ArrayList; +import java.util.Collections; import lombok.RequiredArgsConstructor; import org.harang.server.domain.*; import org.harang.server.domain.enums.Status; @@ -77,7 +79,7 @@ public List getAllPosts() { .collect(Collectors.toList()); } - // TODO: 이전 호출 결과도 중복되어 반환되는 문제 원인 파악 + // TODO: 이전 호출 결과도 중복되어 반환되는 문제 원인 파 public List getSearchResults(String title) { return postRepository.findByTitleIsContaining(title) .stream() @@ -92,4 +94,21 @@ public void deletePost(Long memberId, Long postId) { locationRepository.deleteByPostId(postId); postRepository.delete(post); } + + public List getRecommendedPosts() { + List posts = postRepository.findAll(); + + // 전체 게시글이 3개 미만이면 빈 리스트 반환 (추천 게시글 렌더 X) + if (posts.size() < 3) { + return new ArrayList<>(); + } + + Collections.shuffle(posts); + List recommendedPosts = posts.subList(0, 3); + + return recommendedPosts + .stream() + .map(p -> PostResponse.of(p)) + .toList(); + } }