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

게시글 응답에 선택지별 득표 수, 게시글 전체 투표 수 반환 구현 #224

Merged
merged 3 commits into from
Mar 22, 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
14 changes: 13 additions & 1 deletion src/main/java/balancetalk/module/post/dto/BalanceOptionDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;

import java.util.List;
import java.util.Optional;

@Data
@Builder
@NoArgsConstructor
Expand All @@ -27,6 +30,9 @@ public class BalanceOptionDto {
@Schema(description = "DB에 저장되는 이미지 이름", example = "4df23447-2355-45h2-8783-7f6gd2ceb848_고양이.jpg")
private String storedFileName;

@Schema(description = "선택지 득표 수", example = "15")
private int votesCount;

public BalanceOption toEntity(@Nullable File image) {
BalanceOption.BalanceOptionBuilder builder = BalanceOption.builder()
.title(title)
Expand All @@ -38,10 +44,16 @@ public BalanceOption toEntity(@Nullable File image) {
}

public static BalanceOptionDto fromEntity(BalanceOption balanceOption) {
int votesCount = Optional.ofNullable(balanceOption.getVotes())
.map(List::size)
.orElse(0);

BalanceOptionDtoBuilder builder = BalanceOptionDto.builder()
.balanceOptionId(balanceOption.getId())
.title(balanceOption.getTitle())
.description(balanceOption.getDescription());
.description(balanceOption.getDescription())
.votesCount(votesCount);

if (balanceOption.getFile() != null) {
builder.storedFileName(balanceOption.getFile().getStoredName());
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/balancetalk/module/post/dto/PostResponse.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package balancetalk.module.post.dto;

import balancetalk.module.post.domain.BalanceOption;
import balancetalk.module.post.domain.Post;
import balancetalk.module.post.domain.PostCategory;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Data
Expand Down Expand Up @@ -49,6 +51,9 @@ public class PostResponse {
@Schema(description = "태그 리스트", example = "[\"태그1\", \"태그2\", \"태그3\"]")
private List<PostTagDto> postTags;

@Schema(description = "전체 투표 수", example = "15")
private int totalVotesCount;

@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
@Schema(description = "게시글 작성일", example = "2023-12-25T15:30:00")
private LocalDateTime createdAt;
Expand All @@ -70,6 +75,7 @@ public static PostResponse fromEntity(Post post, boolean myLike, boolean myBookm
.category(post.getCategory())
.balanceOptions(getBalanceOptions(post))
.postTags(getPostTags(post))
.totalVotesCount(getTotalVotes(post))
.createdAt(post.getCreatedAt())
.createdBy(post.getMember().getNickname())
.build();
Expand All @@ -86,4 +92,12 @@ private static List<BalanceOptionDto> getBalanceOptions(Post post) {
.map(BalanceOptionDto::fromEntity)
.collect(Collectors.toList());
}

private static int getTotalVotes(Post post) {
return Optional.ofNullable(post.getOptions())
.map(options -> options.stream()
.mapToInt(option -> Optional.ofNullable(option.getVotes()).map(List::size).orElse(0))
.sum())
.orElse(0);
}
}