Skip to content

Commit

Permalink
refactor: #36 Page로 마지막 키 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
dyk-im committed Nov 13, 2024
1 parent 4244f59 commit a23b8fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.UUID;

import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -88,11 +89,11 @@ public ResponseEntity<GlobalResponseDto<ArticleResponseDto>> getArticle(
//메인 페이지 게시글 조회
@GetMapping
@Operation(summary = "게시글 목록 조회(스크롤)", description = "페이징을 적용하여 게시글 목록을 조회합니다.")
public ResponseEntity<GlobalResponseDto<List<ArticleResponseDto>>> getArticles(
public ResponseEntity<GlobalResponseDto<Page<ArticleResponseDto>>> getArticles(
HttpServletRequest request,
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime cursor,
@RequestParam(defaultValue = "10") int size) {
List<ArticleResponseDto> articles = articleService.getArticles(request, cursor, size);
Page<ArticleResponseDto> articles = articleService.getArticles(request, cursor, size);
return ResponseEntity.status(HttpStatus.OK).body(GlobalResponseDto.success(articles));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.UUID;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -17,11 +18,8 @@ public interface ArticleRepository extends JpaRepository<Article, UUID> {
@Query("SELECT new com.leets.xcellentbe.domain.article.dto.ArticlesWithMediaDto(p, pm.filePath) FROM Article p LEFT JOIN ArticleMedia pm ON p.articleId = pm.article.articleId WHERE p.writer = :user")
List<ArticlesWithMediaDto[]> findPostsByWriter(User user);

@Query("SELECT a FROM Article a WHERE a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED ORDER BY a.createdAt DESC")
List<Article> findRecentArticles(Pageable pageable);

@Query("SELECT a FROM Article a WHERE a.createdAt < :cursor AND a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED ORDER BY a.createdAt DESC")
List<Article> findRecentArticles(@Param("cursor") LocalDateTime cursor, Pageable pageable);
@Query("SELECT a FROM Article a WHERE (:cursor IS NULL OR a.createdAt < :cursor) AND a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED ORDER BY a.createdAt DESC")
Page<Article> findRecentArticles(@Param("cursor") LocalDateTime cursor, Pageable pageable);

@Query("SELECT COUNT(a) FROM Article a WHERE a.rePost = :article AND a.deletedStatus = com.leets.xcellentbe.domain.shared.DeletedStatus.NOT_DELETED")
long countReposts(@Param("article") Article article);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -175,24 +176,17 @@ public ArticleResponseDto getArticle(HttpServletRequest request, UUID articleId)
}

//게시글 전체 조회
public List<ArticleResponseDto> getArticles(HttpServletRequest request, LocalDateTime cursor, int size) {

public Page<ArticleResponseDto> getArticles(HttpServletRequest request, LocalDateTime cursor, int size) {
User user = getUser(request);

Pageable pageable = PageRequest.of(0, size);

List<Article> articles = (cursor == null) ?
articleRepository.findRecentArticles(pageable) : // 처음 로드 시
articleRepository.findRecentArticles(cursor, pageable);

return articles
.stream()
.map(article -> {
boolean isOwner = article.getWriter().getUserId().equals(user.getUserId());
ArticleStatsDto stats = findArticleStats(article);
return ArticleResponseDto.fromWithoutComments(article, isOwner, stats);
})
.collect(Collectors.toList());
Page<Article> articles = articleRepository.findRecentArticles(cursor, pageable);

return articles.map(article -> {
boolean isOwner = article.getWriter().getUserId().equals(user.getUserId());
ArticleStatsDto stats = findArticleStats(article);
return ArticleResponseDto.fromWithoutComments(article, isOwner, stats);
});
}

//리포스트 작성 (인용 x, 단순)
Expand Down

0 comments on commit a23b8fc

Please sign in to comment.