Skip to content

Commit

Permalink
refactor: (#346) 게시글 응답 imageUrl 포함시키도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
woo-chang committed Aug 12, 2023
1 parent c5a9a12 commit cb608ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 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.contains("없는사진") ? "" : 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.contains("없는사진") ? "" : 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

0 comments on commit cb608ae

Please sign in to comment.