Skip to content

Commit 2c227e4

Browse files
authored
[feat] 게시글 정렬 기능과 카테고리 기능을 같이 사용할 수 있도록 구현#208
[feat] 게시글 정렬 기능과 카테고리 기능을 같이 사용할 수 있도록 구현#208
2 parents 01893ab + 67fee64 commit 2c227e4

File tree

4 files changed

+88
-30
lines changed

4 files changed

+88
-30
lines changed

src/main/java/com/back/domain/post/post/dto/request/PostSortScrollRequestDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.back.domain.post.post.enums.PostSortStatus;
44

55
public record PostSortScrollRequestDto(
6+
Long categoryId,
67
Long lastId,
78
Integer lastLikeCount,
89
Integer lastCommentCount,

src/main/java/com/back/domain/post/post/repository/PostRepository.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,29 @@
88
@Repository
99
public interface PostRepository extends JpaRepository<Post, Long> {
1010

11-
// 최신순
11+
// 최신순 (카테고리 없음)
1212
List<Post> findTop10ByOrderByIdDesc();
1313
List<Post> findTop10ByIdLessThanOrderByIdDesc(Long lastId);
1414

15-
// 추천순
15+
// 추천순 (카테고리 없음)
1616
List<Post> findTop10ByOrderByLikeCountDescIdDesc();
1717
List<Post> findTop10ByLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(
1818
Integer likeCount, Integer likeCount2, Long id);
1919

20-
// 댓글순
20+
// 댓글순 (카테고리 없음)
2121
List<Post> findTop10ByOrderByCommentCountDescIdDesc();
2222
List<Post> findTop10ByCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(
2323
Integer commentCount, Integer commentCount2, Long id);
24+
25+
// 최신순 (카테고리)
26+
List<Post> findTop10ByCategoryIdOrderByIdDesc(Long categoryId);
27+
List<Post> findTop10ByCategoryIdAndIdLessThanOrderByIdDesc(Long categoryId, Long id);
28+
29+
// 추천순 (카테고리)
30+
List<Post> findTop10ByCategoryIdOrderByLikeCountDescIdDesc(Long categoryId);
31+
List<Post> findTop10ByCategoryIdAndLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(Long categoryId, Integer likeCount, Integer likeCountEquals, Long id);
32+
33+
// 댓글순 (카테고리)
34+
List<Post> findTop10ByCategoryIdOrderByCommentCountDescIdDesc(Long categoryId);
35+
List<Post> findTop10ByCategoryIdAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(Long categoryId, Integer commentCount, Integer commentCountEquals, Long id);
2436
}

src/main/java/com/back/domain/post/post/service/PostService.java

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,13 @@ public PostResponseDto createPost(PostCreateRequestDto reqBody, List<MultipartFi
101101
public List<PostResponseDto> getPosts(PostSortScrollRequestDto reqBody) {
102102
List<Post> posts;
103103

104-
switch (reqBody.postSortStatus()) {
105-
case POPULAR -> {
106-
if (reqBody.lastId() == null || reqBody.lastLikeCount() == null) {
107-
posts = postRepository.findTop10ByOrderByLikeCountDescIdDesc();
108-
} else {
109-
posts = postRepository.findTop10ByLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(reqBody.lastLikeCount(), reqBody.lastLikeCount(), reqBody.lastId());
110-
}
111-
}
112-
case COMMENTS -> {
113-
if (reqBody.lastId() == null || reqBody.lastCommentCount() == null) {
114-
posts = postRepository.findTop10ByOrderByCommentCountDescIdDesc();
115-
} else {
116-
posts = postRepository.findTop10ByCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(reqBody.lastCommentCount(), reqBody.lastCommentCount(), reqBody.lastId());
117-
}
118-
}
119-
case LATEST -> {
120-
if (reqBody.lastId() == null) {
121-
// 첫 페이지 요청
122-
posts = postRepository.findTop10ByOrderByIdDesc();
123-
} else {
124-
// 이후 페이지 요청
125-
posts = postRepository.findTop10ByIdLessThanOrderByIdDesc(reqBody.lastId());
126-
}
127-
}
128-
default -> throw new IllegalArgumentException("지원하지 않는 정렬 기준: " + reqBody.postSortStatus());
104+
// 카테고리 ID 유무에 따른 분기 처리
105+
if (reqBody.categoryId() != null) {
106+
// 카테고리별 조회 로직
107+
posts = findPostsByCategory(reqBody);
108+
} else {
109+
// 카테고리 없음 (전체) 조회
110+
posts = findAllPosts(reqBody);
129111
}
130112

131113
return posts.stream()
@@ -301,4 +283,67 @@ private void addTag(List<String> tagNames, Post post) {
301283
post.addTag(tag);
302284
}
303285
}
286+
287+
// 카테고리 없음 (전체) 조회 메서드
288+
private List<Post> findAllPosts(PostSortScrollRequestDto reqBody) {
289+
return switch (reqBody.postSortStatus()) {
290+
case POPULAR -> {
291+
if (reqBody.lastId() == null || reqBody.lastLikeCount() == null) {
292+
yield postRepository.findTop10ByOrderByLikeCountDescIdDesc();
293+
} else {
294+
yield postRepository.findTop10ByLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(reqBody.lastLikeCount(), reqBody.lastLikeCount(), reqBody.lastId());
295+
}
296+
}
297+
case COMMENTS -> {
298+
if (reqBody.lastId() == null || reqBody.lastCommentCount() == null) {
299+
yield postRepository.findTop10ByOrderByCommentCountDescIdDesc();
300+
} else {
301+
yield postRepository.findTop10ByCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(reqBody.lastCommentCount(), reqBody.lastCommentCount(), reqBody.lastId());
302+
}
303+
}
304+
case LATEST -> {
305+
if (reqBody.lastId() == null) {
306+
yield postRepository.findTop10ByOrderByIdDesc();
307+
} else {
308+
yield postRepository.findTop10ByIdLessThanOrderByIdDesc(reqBody.lastId());
309+
}
310+
}
311+
default -> throw new IllegalArgumentException("지원하지 않는 정렬 기준: " + reqBody.postSortStatus());
312+
};
313+
}
314+
315+
// 카테고리별 조회 메서드
316+
private List<Post> findPostsByCategory(PostSortScrollRequestDto reqBody) {
317+
return switch (reqBody.postSortStatus()) {
318+
case POPULAR -> {
319+
if (reqBody.lastId() == null || reqBody.lastLikeCount() == null) {
320+
yield postRepository.findTop10ByCategoryIdOrderByLikeCountDescIdDesc(
321+
reqBody.categoryId());
322+
} else {
323+
yield postRepository.findTop10ByCategoryIdAndLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(
324+
reqBody.categoryId(), reqBody.lastLikeCount(), reqBody.lastLikeCount(),
325+
reqBody.lastId());
326+
}
327+
}
328+
case COMMENTS -> {
329+
if (reqBody.lastId() == null || reqBody.lastCommentCount() == null) {
330+
yield postRepository.findTop10ByCategoryIdOrderByCommentCountDescIdDesc(
331+
reqBody.categoryId());
332+
} else {
333+
yield postRepository.findTop10ByCategoryIdAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(
334+
reqBody.categoryId(), reqBody.lastCommentCount(), reqBody.lastCommentCount(),
335+
reqBody.lastId());
336+
}
337+
}
338+
case LATEST -> {
339+
if (reqBody.lastId() == null) {
340+
yield postRepository.findTop10ByCategoryIdOrderByIdDesc(reqBody.categoryId());
341+
} else {
342+
yield postRepository.findTop10ByCategoryIdAndIdLessThanOrderByIdDesc(reqBody.categoryId(),
343+
reqBody.lastId());
344+
}
345+
}
346+
default -> throw new IllegalArgumentException("지원하지 않는 정렬 기준: " + reqBody.postSortStatus());
347+
};
348+
}
304349
}

src/test/java/com/back/domain/post/post/controller/PostControllerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ void getPosts() throws Exception {
122122
secondPage.add(createSampleResponseDto(i));
123123
}
124124

125-
given(postService.getPosts(new PostSortScrollRequestDto(null, 0, 0, PostSortStatus.LATEST))).willReturn(firstPage); // 첫 호출(lastId 없음)
126-
given(postService.getPosts(new PostSortScrollRequestDto(21L, 0, 0, PostSortStatus.LATEST))).willReturn(secondPage);
125+
given(postService.getPosts(new PostSortScrollRequestDto(1L, null, 0, 0, PostSortStatus.LATEST))).willReturn(firstPage); // 첫 호출(lastId 없음)
126+
given(postService.getPosts(new PostSortScrollRequestDto(1L, 21L, 0, 0, PostSortStatus.LATEST))).willReturn(secondPage);
127127

128128
// when & then
129129
mockMvc.perform(get("/posts"))

0 commit comments

Comments
 (0)