Skip to content

Commit cef6842

Browse files
committed
feat: recent post 커서페이징 적용
1 parent 54f2fa9 commit cef6842

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

src/main/java/org/ahpuh/surf/post/controller/PostController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ public ResponseEntity<Integer> getAllPostByCategory(
150150
}
151151

152152
@GetMapping("/posts/recent")
153-
public ResponseEntity<List<RecentPostDto>> recentAllPosts(
154-
@AuthenticationPrincipal final JwtAuthentication authentication
153+
public ResponseEntity<CursorResult<RecentPostDto>> recentAllPosts(
154+
@AuthenticationPrincipal final JwtAuthentication authentication,
155+
@RequestParam final Long cursorId
155156
) {
156-
final List<RecentPostDto> recentAllPosts = postService.recentAllPosts(authentication.userId);
157+
final CursorResult<RecentPostDto> recentAllPosts = postService.recentAllPosts(authentication.userId, cursorId, PageRequest.of(0, 10));
157158
return ResponseEntity.ok().body(recentAllPosts);
158159
}
159160

src/main/java/org/ahpuh/surf/post/repository/PostRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public interface PostRepository extends JpaRepository<Post, Long>, PostRepositor
2626

2727
List<Post> findByCategory(Category category);
2828

29-
List<Post> findTop100ByIsDeletedIsFalseOrderByCreatedAtDesc(Pageable page);
29+
List<Post> findTop10ByCreatedAtIsLessThanEqualOrderByCreatedAtDesc(LocalDateTime createdAt, Pageable page);
30+
31+
List<Post> findTop10ByCreatedAtIsLessThanOrderByCreatedAtDesc(LocalDateTime createdAt, Pageable page);
3032

3133
}

src/main/java/org/ahpuh/surf/post/service/PostService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public interface PostService {
3535

3636
int getRecentScore(Long categoryId);
3737

38-
List<RecentPostDto> recentAllPosts(Long myId);
38+
CursorResult<RecentPostDto> recentAllPosts(Long myId, Long cursorId, Pageable page);
3939

4040
}

src/main/java/org/ahpuh/surf/post/service/PostServiceImpl.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
import org.ahpuh.surf.post.repository.PostRepository;
1414
import org.ahpuh.surf.user.entity.User;
1515
import org.ahpuh.surf.user.repository.UserRepository;
16-
import org.springframework.data.domain.PageRequest;
1716
import org.springframework.data.domain.Pageable;
1817
import org.springframework.stereotype.Service;
1918
import org.springframework.transaction.annotation.Transactional;
2019

2120
import java.time.LocalDate;
21+
import java.time.LocalDateTime;
2222
import java.util.List;
23-
import java.util.stream.Collectors;
2423

2524
@RequiredArgsConstructor
2625
@Transactional(readOnly = true)
@@ -215,13 +214,30 @@ private Post getPostById(final Long postId) {
215214
.orElseThrow(() -> EntityExceptionHandler.PostNotFound(postId));
216215
}
217216

218-
public List<RecentPostDto> recentAllPosts(final Long myId) {
217+
public CursorResult<RecentPostDto> recentAllPosts(final Long myId, final Long cursorId, final Pageable page) {
219218
final User me = userRepository.findById(myId)
220219
.orElseThrow(() -> EntityExceptionHandler.UserNotFound(myId));
221-
return postRepository.findTop100ByIsDeletedIsFalseOrderByCreatedAtDesc(PageRequest.of(0, 100))
222-
.stream()
220+
final Post findPost = postRepository.findById(cursorId).orElse(null);
221+
222+
final List<Post> postList = findPost == null
223+
? postRepository.findTop10ByCreatedAtIsLessThanEqualOrderByCreatedAtDesc(LocalDateTime.now(), page)
224+
: postRepository.findTop10ByCreatedAtIsLessThanOrderByCreatedAtDesc(findPost.getCreatedAt(), page);
225+
226+
if (postList.isEmpty()) {
227+
return new CursorResult<>(List.of(), false);
228+
}
229+
230+
final List<RecentPostDto> posts = postList.stream()
223231
.map(postEntity -> postConverter.toRecentAllPosts(postEntity, me))
224-
.collect(Collectors.toList());
232+
.toList();
233+
234+
final Post lastPost = postList.get(postList.size() - 1);
235+
final boolean hasNext = !postRepository.findTop10ByCreatedAtIsLessThanOrderByCreatedAtDesc(
236+
lastPost.getCreatedAt(),
237+
page)
238+
.isEmpty();
239+
240+
return new CursorResult<>(posts, hasNext);
225241
}
226242

227243
}

src/test/java/org/ahpuh/surf/user/controller/UserControllerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ void testUpdateUser() throws Exception {
164164

165165
// Then
166166
final User user11 = userRepository.findAll().get(0);
167-
System.out.println("말이돼냐 -> " + user11.getProfilePhotoUrl());
168167
assertAll("afterUpdate",
169168
() -> assertThat(user11.getUserName(), is("수정된 name")),
170169
() -> assertThat(user11.getUrl(), is("내 블로그 주소")),

0 commit comments

Comments
 (0)