Skip to content

Commit

Permalink
feat: 비회원 게시글 조회 가능 및 selectedOptionId null 반환 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
gywns0417 committed Mar 19, 2024
1 parent 7e7fcca commit a192ef1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ private List<File> getImages(PostRequest postRequestDto) {
public Page<PostResponse> findAll(String token, Pageable pageable) {
// TODO: 검색, 정렬, 마감 기능 추가
Page<Post> posts = postRepository.findAll(pageable);
Member member = getCurrentMember(memberRepository);

if (token == null) {
return posts.map(post -> PostResponse.fromEntity(post, member, false, false, false));
return posts.map(post -> PostResponse.fromEntity(post, null, false, false, false));
}
Member member = getCurrentMember(memberRepository);

return posts.map(post -> PostResponse.fromEntity(post,
member,
Expand All @@ -86,13 +86,14 @@ public Page<PostResponse> findAll(String token, Pageable pageable) {
@Transactional(readOnly = true)
public PostResponse findById(Long postId, String token) {
Post post = getCurrentPost(postId);
Member member = getCurrentMember(memberRepository);

if (token == null) {
post.increaseViews();
return PostResponse.fromEntity(post, member, false, false, false);
return PostResponse.fromEntity(post, null, false, false, false);
}

Member member = getCurrentMember(memberRepository);

if (member.getRole() == Role.USER) {
post.increaseViews();
}
Expand Down Expand Up @@ -143,14 +144,15 @@ private Post getCurrentPost(Long postId) {
public List<PostResponse> findBestPosts(String token) {
PageRequest limit = PageRequest.of(0, BEST_POSTS_SIZE);
List<Post> posts = postRepository.findBestPosts(limit);
Member member = getCurrentMember(memberRepository);

if (token == null) {
return posts.stream()
.map(post -> PostResponse.fromEntity(post, member, false, false, false))
.map(post -> PostResponse.fromEntity(post, null, false, false, false))
.collect(Collectors.toList());
}

Member member = getCurrentMember(memberRepository);

return posts.stream()
.map(post -> PostResponse.fromEntity(post,
member,
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/balancetalk/module/post/dto/PostResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ private static List<BalanceOptionDto> getBalanceOptions(Post post) {
}

private static Long getSelectedOptionId(Post post, Member member) {
if (member == null) {
return null;
}

return post.getOptions().stream()
.filter(option -> Optional.ofNullable(option.getVotes())
.orElseGet(Collections::emptyList)
Expand Down

0 comments on commit a192ef1

Please sign in to comment.