From 200a3c57b0b1a22d6c70b103560f85ac2cc3b7b3 Mon Sep 17 00:00:00 2001 From: Jinhong Date: Sun, 21 Nov 2021 14:53:13 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=8B=A0=EA=B7=9C=20Pagination=20A?= =?UTF-8?q?PI=20=EC=B6=94=EA=B0=80=20(#771)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/application/PostFeedService.java | 5 +++-- .../dto/request/HomeFeedRequestDto.java | 19 ++++++++++++++++++- .../domain/repository/PostRepository.java | 3 +++ .../post/presentation/PostController.java | 5 +---- .../post/presentation/PostFeedController.java | 3 ++- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/application/PostFeedService.java b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/application/PostFeedService.java index aa491c89b..155df815f 100644 --- a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/application/PostFeedService.java +++ b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/application/PostFeedService.java @@ -1,7 +1,6 @@ package com.woowacourse.pickgit.post.application; -import com.woowacourse.pickgit.exception.authentication.UnauthorizedException; import com.woowacourse.pickgit.exception.post.PostNotFoundException; import com.woowacourse.pickgit.exception.user.UserNotFoundException; import com.woowacourse.pickgit.post.application.dto.PostDtoAssembler; @@ -39,7 +38,9 @@ public List allHomeFeed(HomeFeedRequestDto homeFeedRequestDto) Pageable pageable = homeFeedRequestDto.getPageable(); if (homeFeedRequestDto.isGuest()) { - return PostDtoAssembler.postResponseDtos(null, postRepository.findAllPosts(pageable)); + return PostDtoAssembler.postResponseDtos(null, + postRepository.findAllPostsToBe(homeFeedRequestDto.getLastPostId(), pageable) + ); } User requestUser = findUserByName(homeFeedRequestDto.getRequestUserName()); diff --git a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/application/dto/request/HomeFeedRequestDto.java b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/application/dto/request/HomeFeedRequestDto.java index cd11cbd25..f485cc5a9 100644 --- a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/application/dto/request/HomeFeedRequestDto.java +++ b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/application/dto/request/HomeFeedRequestDto.java @@ -9,7 +9,8 @@ public class HomeFeedRequestDto { private String requestUserName; private boolean isGuest; - Pageable pageable; + private Pageable pageable; + private Long lastPostId; private HomeFeedRequestDto() { } @@ -25,6 +26,18 @@ public HomeFeedRequestDto(AppUser appUser, Pageable pageable) { this.pageable = pageable; } + public HomeFeedRequestDto(AppUser appUser, Pageable pageable, Long lastPostId) { + if(appUser.isGuest()) { + requestUserName = null; + } else { + requestUserName = appUser.getUsername(); + } + + this.isGuest = appUser.isGuest(); + this.pageable = pageable; + this.lastPostId = lastPostId; + } + public HomeFeedRequestDto(String requestUserName, boolean isGuest, Pageable pageable) { this.requestUserName = requestUserName; this.isGuest = isGuest; @@ -42,4 +55,8 @@ public boolean isGuest() { public Pageable getPageable() { return pageable; } + + public Long getLastPostId() { + return lastPostId; + } } diff --git a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/domain/repository/PostRepository.java b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/domain/repository/PostRepository.java index 6fe47c6b3..8879e963f 100644 --- a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/domain/repository/PostRepository.java +++ b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/domain/repository/PostRepository.java @@ -14,6 +14,9 @@ public interface PostRepository extends JpaRepository { @Query("select p from Post p left join fetch p.user order by p.createdAt desc") List findAllPosts(Pageable pageable); + @Query("select p from Post p left join fetch p.user where p.id < :postId order by p.id desc") + List findAllPostsToBe(@Param("postId") Long postId, Pageable pageable); + @Query("select p from Post p where p.user = :user order by p.createdAt desc") List findAllPostsByUser(@Param("user") User user, Pageable pageable); diff --git a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/presentation/PostController.java b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/presentation/PostController.java index 7030093ce..aeb822bba 100644 --- a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/presentation/PostController.java +++ b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/presentation/PostController.java @@ -17,7 +17,6 @@ import com.woowacourse.pickgit.post.presentation.dto.request.PostUpdateRequest; import com.woowacourse.pickgit.post.presentation.dto.response.LikeResponse; import com.woowacourse.pickgit.post.presentation.dto.response.LikeUsersResponse; -import com.woowacourse.pickgit.post.presentation.dto.response.PostResponse; import com.woowacourse.pickgit.post.presentation.dto.response.PostUpdateResponse; import com.woowacourse.pickgit.post.presentation.dto.response.RepositoryResponse; import java.net.URI; @@ -145,9 +144,7 @@ public ResponseEntity delete( return ResponseEntity.noContent().build(); } - - - + @ForLoginAndGuestUser @GetMapping("/posts/{postId}/likes") public ResponseEntity> searchLikeUsers( diff --git a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/presentation/PostFeedController.java b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/presentation/PostFeedController.java index ebd99c03f..cce0522f9 100644 --- a/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/presentation/PostFeedController.java +++ b/backend/pick-git/src/main/java/com/woowacourse/pickgit/post/presentation/PostFeedController.java @@ -40,6 +40,7 @@ public class PostFeedController { public ResponseEntity> readHomeFeed( @Authenticated AppUser appUser, @PageableDefault Pageable pageable, + @RequestParam(required = false) Long lastPostId, @RequestParam(required = false, defaultValue = "all") String type ) { FeedType selectedFeedType = feedTypes.stream() @@ -47,7 +48,7 @@ public ResponseEntity> readHomeFeed( .findAny() .orElseThrow(HomeFeedTypeException::new); - HomeFeedRequestDto homeFeedRequestDto = new HomeFeedRequestDto(appUser, pageable); + HomeFeedRequestDto homeFeedRequestDto = new HomeFeedRequestDto(appUser, pageable, lastPostId); List postResponseDtos = selectedFeedType.find(homeFeedRequestDto); List postResponses = PostAssembler.postResponses((postResponseDtos));