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

[#771] Pagination 조회 쿼리 PK 기반 수정 #772

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
@@ -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;
Expand Down Expand Up @@ -39,7 +38,9 @@ public List<PostResponseDto> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class HomeFeedRequestDto {

private String requestUserName;
private boolean isGuest;
Pageable pageable;
private Pageable pageable;
private Long lastPostId;

private HomeFeedRequestDto() {
}
Expand All @@ -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;
Expand All @@ -42,4 +55,8 @@ public boolean isGuest() {
public Pageable getPageable() {
return pageable;
}

public Long getLastPostId() {
return lastPostId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public interface PostRepository extends JpaRepository<Post, Long> {
@Query("select p from Post p left join fetch p.user order by p.createdAt desc")
List<Post> findAllPosts(Pageable pageable);

@Query("select p from Post p left join fetch p.user where p.id < :postId order by p.id desc")
List<Post> findAllPostsToBe(@Param("postId") Long postId, Pageable pageable);

@Query("select p from Post p where p.user = :user order by p.createdAt desc")
List<Post> findAllPostsByUser(@Param("user") User user, Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,9 +144,7 @@ public ResponseEntity<Void> delete(

return ResponseEntity.noContent().build();
}




@ForLoginAndGuestUser
@GetMapping("/posts/{postId}/likes")
public ResponseEntity<List<LikeUsersResponse>> searchLikeUsers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ public class PostFeedController {
public ResponseEntity<List<PostResponse>> readHomeFeed(
@Authenticated AppUser appUser,
@PageableDefault Pageable pageable,
@RequestParam(required = false) Long lastPostId,
@RequestParam(required = false, defaultValue = "all") String type
) {
FeedType selectedFeedType = feedTypes.stream()
.filter(feedType -> feedType.isSatisfiedBy(type))
.findAny()
.orElseThrow(HomeFeedTypeException::new);

HomeFeedRequestDto homeFeedRequestDto = new HomeFeedRequestDto(appUser, pageable);
HomeFeedRequestDto homeFeedRequestDto = new HomeFeedRequestDto(appUser, pageable, lastPostId);
List<PostResponseDto> postResponseDtos = selectedFeedType.find(homeFeedRequestDto);

List<PostResponse> postResponses = PostAssembler.postResponses((postResponseDtos));
Expand Down