Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 게시글 무한스크롤 마지막 값 설정 #37

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 +91,13 @@ 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