mirror of
https://github.com/headporter81/specialsource-homepage-backend.git
synced 2026-12-06 07:41:09 +09:00
feat : 스크랩 게시물 목록에 id 커서(before) 조회 추가 (무한 스크롤 중복·누락 방지)
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 53s
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 53s
- GET /api/scraped-posts?before={id}: 해당 id보다 오래된 안 읽은 글 조회
- 안 읽은 글 정렬을 scrapedAt → id 내림차순으로 변경 (id 순서 = 스크랩 순서)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7813053122
commit
657d92114b
3 files changed
+10
-6
No files matched your search
@@ -24,6 +24,7 @@ public class ScrapedPostController {
|
||||
@GetMapping
|
||||
public ResponseEntity<?> getPosts(
|
||||
@RequestParam(required = false) String board,
|
||||
@RequestParam(required = false) Long before, // 무한 스크롤 커서: 이 id보다 오래된 글 (page는 0으로)
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "20") int size
|
||||
) {
|
||||
@@ -32,7 +33,7 @@ public class ScrapedPostController {
|
||||
}
|
||||
|
||||
Pageable pageable = PageRequest.of(page, Math.min(size, 100));
|
||||
Page<ScrapedPostResponse> posts = scrapedPostQueryService.getPosts(board, pageable);
|
||||
Page<ScrapedPostResponse> posts = scrapedPostQueryService.getPosts(board, before, pageable);
|
||||
return ResponseEntity.ok(posts);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,10 @@ import java.time.LocalDateTime;
|
||||
public interface ScrapedPostRepository extends JpaRepository<ScrapedPost, Long> {
|
||||
boolean existsByBoardKeyAndLink(String boardKey, String link);
|
||||
|
||||
Page<ScrapedPost> findByBoardKeyAndViewedAtIsNullOrderByScrapedAtDesc(String boardKey, Pageable pageable);
|
||||
// 안 읽은 글: id(= 스크랩 순서) 내림차순 커서 조회. 무한 스크롤 중 새 글 추가·읽음 처리로 순서가 밀려도 중복·누락 없음
|
||||
Page<ScrapedPost> findByBoardKeyAndViewedAtIsNullAndIdLessThanOrderByIdDesc(String boardKey, Long id, Pageable pageable);
|
||||
|
||||
Page<ScrapedPost> findByViewedAtIsNullOrderByScrapedAtDesc(Pageable pageable);
|
||||
Page<ScrapedPost> findByViewedAtIsNullAndIdLessThanOrderByIdDesc(Long id, Pageable pageable);
|
||||
|
||||
Page<ScrapedPost> findByBoardKeyAndViewedAtIsNotNullOrderByViewedAtDesc(String boardKey, Pageable pageable);
|
||||
|
||||
|
||||
@@ -17,11 +17,13 @@ public class ScrapedPostQueryService {
|
||||
|
||||
private final ScrapedPostRepository scrapedPostRepository;
|
||||
|
||||
public Page<ScrapedPostResponse> getPosts(String boardKey, Pageable pageable) {
|
||||
public Page<ScrapedPostResponse> getPosts(String boardKey, Long before, Pageable pageable) {
|
||||
// 이미 조회한(viewedAt != null) 글은 목록에서 제외한다.
|
||||
// before: 이 id보다 오래된 글만 (무한 스크롤 커서). 없으면 최신 글부터
|
||||
long cursor = before == null ? Long.MAX_VALUE : before;
|
||||
Page<ScrapedPost> page = (boardKey == null || boardKey.isBlank())
|
||||
? scrapedPostRepository.findByViewedAtIsNullOrderByScrapedAtDesc(pageable)
|
||||
: scrapedPostRepository.findByBoardKeyAndViewedAtIsNullOrderByScrapedAtDesc(boardKey, pageable);
|
||||
? scrapedPostRepository.findByViewedAtIsNullAndIdLessThanOrderByIdDesc(cursor, pageable)
|
||||
: scrapedPostRepository.findByBoardKeyAndViewedAtIsNullAndIdLessThanOrderByIdDesc(boardKey, cursor, pageable);
|
||||
|
||||
return page.map(ScrapedPostResponse::from);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user