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

Refactor: 저자의 다이어리 조회시 공동저자 설정된 다이어리도 조회하도록 기능 수정 #117

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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 @@ -30,6 +30,7 @@ public interface PostRepository extends JpaRepository<Post, Long> {

Page<Post> findByTeamAndMemberOrderByCreatedAtDescPostIdDesc(Team team, Member member, Pageable pageable);

Page<Post> findByAuthorsList_MemberOrderByCreatedAtDescPostIdDesc(Member member, Pageable pageable);
boolean existsByTeam(Team team);

boolean existsByProject(Project project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -68,17 +69,40 @@ public Page<Post> getPostsByCategories(Optional<String> optSearch, int page, int
return postRepository.findAllByOrderByCreatedAtDesc(request);
}

// @Override
// public Page<Post> getPostsByMember(Long memberId, int page, int size) {
// PageRequest request = PageRequest.of(page, size);
// Member member = memberRepository.findById(memberId).get();
//
// if (!postRepository.existsByMember(member)){
// throw new PostHandler(ErrorStatus.POST_NOT_EXIST_BY_MEMBER);
// }
// return postRepository.findByMemberOrderByCreatedAtDescPostIdDesc(member, request);
// }


@Override
public Page<Post> getPostsByMember(Long memberId, int page, int size) {
PageRequest request = PageRequest.of(page, size);
Member member = memberRepository.findById(memberId).get();
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));
// 멤버가 작성한 다이어리 조회
Page<Post> postsByMember = postRepository.findByMemberOrderByCreatedAtDescPostIdDesc(member, request);
// 멤버가 공동 저자인 다이어리 조회
Page<Post> postsByCoauthor = postRepository.findByAuthorsList_MemberOrderByCreatedAtDescPostIdDesc(member, request);

if (!postRepository.existsByMember(member)){
if (postsByMember.isEmpty() && postsByCoauthor.isEmpty()) {
throw new PostHandler(ErrorStatus.POST_NOT_EXIST_BY_MEMBER);
}
return postRepository.findByMemberOrderByCreatedAtDescPostIdDesc(member, request);
// 두 개의 결과를 하나의 리스트로 병합
List<Post> combinedPosts = new ArrayList<>();
combinedPosts.addAll(postsByMember.getContent());
combinedPosts.addAll(postsByCoauthor.getContent());
// 병합된 리스트를 페이지로 반환
return new PageImpl<>(combinedPosts, request, combinedPosts.size());
}


@Override
public Page<Post> getPostsByTeam(Long teamId, int page, int size) {
PageRequest request = PageRequest.of(page, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public ApiResponse<PostResponseDTO.UpdatePostResultDTO> setPostTeam(@PathVariabl


// 저자의 다이어리 리스트 페이징 조회
// TODO: 멤버별 작성한 글 조회시 공동 저자로 등록된 멤버도 조회가능하도록 기능 수정 필요
@GetMapping("/member/{memberId}/paging")
@Operation(summary = "저자의 다이어리 리스트 페이징 조회 API", description = "저자의 다이어리 리스트를 페이징으로 조회하기 위해 'Path Variable'로 해당 팀의 'memberId'를 받습니다. **첫 페이지는 0부터 입니다.**", security = @SecurityRequirement(name = "accessToken"))
public ApiResponse<PostResponseDTO.MemberPostPreviewListDTO> findPostByMember(@PathVariable Long memberId, @RequestParam @Min(0) Integer page, @RequestParam @Min(1) @Max(5) Integer size) {
Expand Down
Loading