Skip to content

Commit

Permalink
게시글 전체 조회 시 이미지 경로 포함하도록 수정 (#364)
Browse files Browse the repository at this point in the history
* chore: (#346) 코드 컨벤션 수정

* refactor: (#346) 게시글 응답 imageUrl 포함시키도록 수정

* fix: (#346) 빈 이미지 처리 수정
  • Loading branch information
woo-chang authored and tjdtls690 committed Sep 12, 2023
1 parent 466d89d commit c9fa359
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public record PostOptionResponse(
Long optionId,
String content,
String imageUrl,
Integer voteCount,
Double votePercent
) {
Expand All @@ -16,11 +17,16 @@ public static PostOptionResponse of(final Post post, final PostOption postOption
return new PostOptionResponse(
postOption.getId(),
postOption.getContent(),
convertImageUrl(postOption.getImageUrl()),
post.isClosed() ? postOption.getVoteCount() : HIDDEN_COUNT,
post.isClosed() ? ((double) postOption.getVoteCount() / post.getTotalVoteCount()) * 100 : HIDDEN_COUNT
);
}

private static String convertImageUrl(final String imageUrl) {
return imageUrl == null ? "" : imageUrl;
}

public static PostOptionResponse of(
final PostOption postOption,
final boolean isVisibleVoteResult,
Expand All @@ -29,6 +35,7 @@ public static PostOptionResponse of(
return new PostOptionResponse(
postOption.getId(),
postOption.getContent(),
convertImageUrl(postOption.getImageUrl()),
postOption.getVoteCount(isVisibleVoteResult),
postOption.getVotePercent(totalVoteCount)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.votogether.domain.post.entity.Post;
import com.votogether.domain.post.entity.PostBody;
import com.votogether.domain.post.entity.PostCategory;
import com.votogether.domain.post.entity.PostContentImage;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -16,6 +17,7 @@ public record PostResponse(
WriterResponse writer,
String title,
String content,
String imageUrl,
List<CategoryResponse> categories,

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
Expand All @@ -30,12 +32,19 @@ public record PostResponse(
public static PostResponse of(final Post post, final Member loginMember) {
final Member writer = post.getWriter();
final PostBody postBody = post.getPostBody();
final List<PostContentImage> contentImages = postBody.getPostContentImages().getContentImages();
final StringBuilder contentImageUrl = new StringBuilder();

if (!contentImages.isEmpty()) {
contentImageUrl.append(contentImages.get(0).getImageUrl());
}

return new PostResponse(
post.getId(),
WriterResponse.of(writer.getId(), writer.getNickname()),
postBody.getTitle(),
postBody.getContent(),
convertImageUrl(contentImageUrl.toString()),
getCategories(post),
post.getCreatedAt(),
post.getDeadline(),
Expand All @@ -47,6 +56,10 @@ public static PostResponse of(final Post post, final Member loginMember) {
);
}

private static String convertImageUrl(final String imageUrl) {
return imageUrl == null ? "" : imageUrl;
}

private static List<CategoryResponse> getCategories(final Post post) {
return post.getPostCategories()
.getPostCategories()
Expand Down Expand Up @@ -74,11 +87,20 @@ private static List<PostOptionResponse> getOptions(
}

public static PostResponse forGuest(final Post post) {
final PostBody postBody = post.getPostBody();
final List<PostContentImage> contentImages = postBody.getPostContentImages().getContentImages();
final StringBuilder contentImageUrl = new StringBuilder();

if (!contentImages.isEmpty()) {
contentImageUrl.append(contentImages.get(0).getImageUrl());
}

return new PostResponse(
post.getId(),
WriterResponse.from(post.getWriter()),
post.getPostBody().getTitle(),
post.getPostBody().getContent(),
convertImageUrl(contentImageUrl.toString()),
getCategories(post),
post.getCreatedAt(),
post.getDeadline(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static PostDetailResponse of(final Post post, final Member loginMember) {
WriterResponse.of(writer.getId(), writer.getNickname()),
postBody.getTitle(),
postBody.getContent(),
convertImageUrl(contentImageUrl.toString()),
contentImageUrl.toString(),
getCategories(postCategories.getPostCategories()),
post.getCreatedAt(),
post.getDeadline(),
Expand All @@ -54,10 +54,6 @@ public static PostDetailResponse of(final Post post, final Member loginMember) {
);
}

private static String convertImageUrl(final String imageUrl) {
return imageUrl.contains("없는사진") ? "" : imageUrl;
}

private static List<CategoryResponse> getCategories(final List<PostCategory> postCategories) {
return postCategories.stream()
.map(PostCategory::getCategory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ public static PostOptionDetailResponse of(
return new PostOptionDetailResponse(
postOption.getId(),
postOption.getContent(),
convertImageUrl(postOption.getImageUrl()),
postOption.getImageUrl(),
postOption.getVoteCount(isVisibleVoteResult),
postOption.getVotePercent(totalVoteCount)
);
}

private static String convertImageUrl(final String imageUrl) {
return imageUrl.contains("없는사진") ? "" : imageUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public List<PostResponse> searchPostsWithKeywordForGuest(
final PostClosingType postClosingType,
final PostSortType postSortType,
final Long categoryId
) {
) {
final Pageable pageable = PageRequest.of(page, BASIC_PAGING_SIZE);
final List<Post> posts =
postRepository.findAllWithKeyword(keyword, postClosingType, postSortType, categoryId, pageable);
Expand Down

0 comments on commit c9fa359

Please sign in to comment.