@@ -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}
0 commit comments