@@ -87,10 +87,15 @@ public CursorResult<FollowingPostDto> explore(final Long myId, final Long cursor
8787 .ifPresent (like -> dto .setLiked (like .getLikeId ()));
8888 }
8989
90- final Long lastIdOfIndex = followingPostDtos .isEmpty () ?
91- null : followingPostDtos .get (followingPostDtos .size () - 1 ).getPostId ();
90+ final long lastIdOfIndex = followingPostDtos .isEmpty () ? 0 : followingPostDtos .get (followingPostDtos .size () - 1 ).getPostId ();
9291
93- return new CursorResult <>(followingPostDtos , hasNext (lastIdOfIndex ));
92+ final Boolean hasNext = postRepository .findNextFollowingPosts (
93+ myId ,
94+ followingPostDtos .get (Math .toIntExact (lastIdOfIndex )).getSelectedDate (),
95+ followingPostDtos .get (Math .toIntExact (lastIdOfIndex )).getCreatedAt (),
96+ page ).isEmpty ();
97+
98+ return new CursorResult <>(followingPostDtos , hasNext );
9499 }
95100
96101 public List <PostCountDto > getCountsPerDayWithYear (final int year , final Long userId ) {
@@ -134,13 +139,19 @@ public CursorResult<AllPostResponseDto> getAllPost(final Long myId, final Long u
134139 postRepository .findAllByUserOrderBySelectedDateDesc (user , page ) :
135140 postRepository .findByUserAndSelectedDateLessThanAndCreatedAtLessThanOrderBySelectedDateDesc (user , findPost .getSelectedDate (), findPost .getCreatedAt (), page );
136141
137- final Long lastIdOfIndex = postList .isEmpty () ? 0 : postList .get (postList .size () - 1 ).getPostId ();
142+ final long lastIdOfIndex = postList .isEmpty () ? 0 : postList .get (postList .size () - 1 ).getPostId ();
138143
139144 final List <AllPostResponseDto > posts = postList .stream ()
140145 .map (post -> postConverter .toAllPostResponseDto (post , likeRepository .findByUserIdAndPost (myId , post )))
141146 .toList ();
142147
143- return new CursorResult <>(posts , hasNext (lastIdOfIndex ));
148+ final Boolean hasNext = postRepository .findByUserAndSelectedDateLessThanAndCreatedAtLessThanOrderBySelectedDateDesc (
149+ user ,
150+ postList .get (Math .toIntExact (lastIdOfIndex )).getSelectedDate (),
151+ postList .get (Math .toIntExact (lastIdOfIndex )).getCreatedAt (),
152+ page ).isEmpty ();
153+
154+ return new CursorResult <>(posts , hasNext );
144155 }
145156
146157 @ Override
@@ -156,13 +167,20 @@ public CursorResult<AllPostResponseDto> getAllPostByCategory(final Long myId, fi
156167 postRepository .findAllByUserAndCategoryOrderBySelectedDateDesc (user , category , page ) :
157168 postRepository .findByUserAndCategoryAndSelectedDateLessThanAndCreatedAtLessThanOrderBySelectedDateDesc (user , category , findPost .getSelectedDate (), findPost .getCreatedAt (), page );
158169
159- final Long lastIdOfIndex = postList .isEmpty () ? 0 : postList .get (postList .size () - 1 ).getPostId ();
170+ final long lastIdOfIndex = postList .isEmpty () ? 0 : postList .get (postList .size () - 1 ).getPostId ();
160171
161172 final List <AllPostResponseDto > posts = postList .stream ()
162173 .map (post -> postConverter .toAllPostResponseDto (post , likeRepository .findByUserIdAndPost (myId , post )))
163174 .toList ();
164175
165- return new CursorResult <>(posts , hasNext (lastIdOfIndex ));
176+ final Boolean hasNext = postRepository .findByUserAndCategoryAndSelectedDateLessThanAndCreatedAtLessThanOrderBySelectedDateDesc (
177+ user ,
178+ category ,
179+ postList .get (Math .toIntExact (lastIdOfIndex )).getSelectedDate (),
180+ postList .get (Math .toIntExact (lastIdOfIndex )).getCreatedAt (),
181+ page ).isEmpty ();
182+
183+ return new CursorResult <>(posts , hasNext );
166184 }
167185
168186 public int getRecentScore (final Long categoryId ) {
@@ -182,10 +200,4 @@ private Post getPostById(final Long postId) {
182200 return postRepository .findById (postId )
183201 .orElseThrow (() -> EntityExceptionHandler .PostNotFound (postId ));
184202 }
185-
186- private Boolean hasNext (final Long id ) {
187- final Post post = postRepository .findById (id ).orElse (null );
188- return post != null && postRepository .existsBySelectedDateLessThanAndCreatedAtLessThan (post .getSelectedDate (), post .getCreatedAt ());
189- }
190-
191203}
0 commit comments