-
Notifications
You must be signed in to change notification settings - Fork 1
[FEAT] 투표자 이름 정보 추가 #217
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
[FEAT] 투표자 이름 정보 추가 #217
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,37 @@ | ||
| package com.debatetimer.domain.poll; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class VoteInfo { | ||
|
|
||
| private static final Comparator<Vote> VOTE_COMPARATOR = Comparator.comparing(Vote::getCreatedAt); | ||
| private static final long INITIAL_VOTE_COUNT = 0L; | ||
|
|
||
| private final long pollId; | ||
| private final long totalCount; | ||
| private final long prosCount; | ||
| private final long consCount; | ||
| private final List<ParticipantName> voterNames; | ||
|
|
||
| public VoteInfo(long pollId, long prosCount, long consCount) { | ||
| public VoteInfo(long pollId, List<Vote> votes) { | ||
| Map<VoteTeam, Long> voteCounts = createVoteCounts(votes); | ||
| this.pollId = pollId; | ||
| this.totalCount = prosCount + consCount; | ||
| this.prosCount = prosCount; | ||
| this.consCount = consCount; | ||
| this.totalCount = votes.size(); | ||
| this.prosCount = voteCounts.getOrDefault(VoteTeam.PROS, INITIAL_VOTE_COUNT); | ||
| this.consCount = voteCounts.getOrDefault(VoteTeam.CONS, INITIAL_VOTE_COUNT); | ||
| this.voterNames = votes.stream() | ||
| .sorted(VOTE_COMPARATOR) | ||
| .map(Vote::getName) | ||
| .toList(); | ||
| } | ||
|
|
||
| private Map<VoteTeam, Long> createVoteCounts(List<Vote> votes) { | ||
| return votes.stream() | ||
| .collect(Collectors.groupingBy(Vote::getTeam, Collectors.counting())); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ALTER TABLE vote | ||
| ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; | ||
|
|
||
| ALTER TABLE vote | ||
| ADD COLUMN modified_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import com.debatetimer.dto.poll.response.PollInfoResponse; | ||
| import com.debatetimer.entity.customize.CustomizeTableEntity; | ||
| import com.debatetimer.entity.poll.PollEntity; | ||
| import com.debatetimer.entity.poll.VoteEntity; | ||
| import io.restassured.http.ContentType; | ||
| import io.restassured.http.Headers; | ||
| import org.junit.jupiter.api.Nested; | ||
|
|
@@ -44,9 +45,9 @@ class GetPollInfo { | |
| Member member = memberGenerator.generate("email@email.com"); | ||
| CustomizeTableEntity table = customizeTableEntityGenerator.generate(member); | ||
| PollEntity pollEntity = pollEntityGenerator.generate(table, PollStatus.PROGRESS); | ||
| voteEntityGenerator.generate(pollEntity, VoteTeam.PROS, "콜리"); | ||
| voteEntityGenerator.generate(pollEntity, VoteTeam.PROS, "비토"); | ||
| voteEntityGenerator.generate(pollEntity, VoteTeam.CONS, "커찬"); | ||
| VoteEntity voter1 = voteEntityGenerator.generate(pollEntity, VoteTeam.PROS, "콜리"); | ||
| VoteEntity voter2 = voteEntityGenerator.generate(pollEntity, VoteTeam.PROS, "비토"); | ||
| VoteEntity voter3 = voteEntityGenerator.generate(pollEntity, VoteTeam.CONS, "커찬"); | ||
| Headers headers = headerGenerator.generateAccessTokenHeader(member); | ||
|
|
||
| PollInfoResponse response = given() | ||
|
|
@@ -64,7 +65,9 @@ class GetPollInfo { | |
| () -> assertThat(response.status()).isEqualTo(pollEntity.getStatus()), | ||
| () -> assertThat(response.totalCount()).isEqualTo(3L), | ||
| () -> assertThat(response.prosCount()).isEqualTo(2L), | ||
| () -> assertThat(response.consCount()).isEqualTo(1L) | ||
| () -> assertThat(response.consCount()).isEqualTo(1L), | ||
| () -> assertThat(response.voterNames()).containsExactly( | ||
| voter1.getName(), voter2.getName(), voter3.getName()) | ||
| ); | ||
|
Comment on lines
+68
to
71
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [의견 🔈 ] 음... 순서 검증이 필요한 부분이에요. 지금 voteEntites에서는 정렬이 안되어 있지만 VoteInfo에서 정렬을 해주고 있고 Controller 응답에서 투표 생성 순으로 정렬되어 응답이 온다는 점을 검증해야 하기 때문에 containsExactly가 InAnyOrder보다 맞는 검증이라 생각했습니다. |
||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.debatetimer.domain.poll; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class VoteInfoTest { | ||
|
|
||
| @Nested | ||
| class getVoterNames { | ||
|
|
||
| @Test | ||
| void 생성_순으로_투표자_이름을_정렬한다() { | ||
| LocalDateTime now = LocalDateTime.now(); | ||
| LocalDateTime oneMinutesAgo = now.minusMinutes(1); | ||
| LocalDateTime twoMinutesAgo = now.minusMinutes(2); | ||
| long pollId = 1L; | ||
| Vote nowVote = new Vote(1L, pollId, VoteTeam.PROS, "콜리1", "code1", now); | ||
| Vote oneMinutesAgoVote = new Vote(2L, pollId, VoteTeam.PROS, "콜리2", "code2", oneMinutesAgo); | ||
| Vote twoMinutesAgoVote = new Vote(3L, pollId, VoteTeam.PROS, "콜리3", "code3", twoMinutesAgo); | ||
|
|
||
| VoteInfo voteInfo = new VoteInfo(pollId, List.of(nowVote, oneMinutesAgoVote, twoMinutesAgoVote)); | ||
| List<String> voterNames = voteInfo.getVoterNames() | ||
| .stream() | ||
| .map(ParticipantName::getValue) | ||
| .toList(); | ||
|
|
||
| assertThat(voterNames) | ||
| .containsExactly( | ||
| twoMinutesAgoVote.getName().getValue(), | ||
| oneMinutesAgoVote.getName().getValue(), | ||
| nowVote.getName().getValue() | ||
| ); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import com.debatetimer.dto.poll.response.PollInfoResponse; | ||
| import com.debatetimer.entity.customize.CustomizeTableEntity; | ||
| import com.debatetimer.entity.poll.PollEntity; | ||
| import com.debatetimer.entity.poll.VoteEntity; | ||
| import com.debatetimer.service.BaseServiceTest; | ||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.Nested; | ||
|
|
@@ -44,9 +45,9 @@ class GetPollInfo { | |
| Member member = memberGenerator.generate("email@email.com"); | ||
| CustomizeTableEntity table = customizeTableEntityGenerator.generate(member); | ||
| PollEntity pollEntity = pollEntityGenerator.generate(table, PollStatus.PROGRESS); | ||
| voteEntityGenerator.generate(pollEntity, VoteTeam.PROS, "콜리"); | ||
| voteEntityGenerator.generate(pollEntity, VoteTeam.PROS, "비토"); | ||
| voteEntityGenerator.generate(pollEntity, VoteTeam.CONS, "커찬"); | ||
| VoteEntity voter1 = voteEntityGenerator.generate(pollEntity, VoteTeam.PROS, "콜리"); | ||
| VoteEntity voter2 = voteEntityGenerator.generate(pollEntity, VoteTeam.PROS, "비토"); | ||
| VoteEntity voter3 = voteEntityGenerator.generate(pollEntity, VoteTeam.CONS, "커찬"); | ||
|
|
||
| PollInfoResponse pollInfo = pollService.getPollInfo(table.getId(), member); | ||
|
|
||
|
|
@@ -57,7 +58,9 @@ class GetPollInfo { | |
| () -> assertThat(pollInfo.status()).isEqualTo(pollEntity.getStatus()), | ||
| () -> assertThat(pollInfo.totalCount()).isEqualTo(3L), | ||
| () -> assertThat(pollInfo.prosCount()).isEqualTo(2L), | ||
| () -> assertThat(pollInfo.consCount()).isEqualTo(1L) | ||
| () -> assertThat(pollInfo.consCount()).isEqualTo(1L), | ||
| () -> assertThat(pollInfo.voterNames()).containsExactly( | ||
| voter1.getName(), voter2.getName(), voter3.getName()) | ||
| ); | ||
|
Comment on lines
58
to
64
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| } | ||
| } | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vote의 getName() 메서드 시그니처를 String 으로 return 하게 만들면 안됨?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
반대로 필드를 List으로 도메인 중심으로 짜게 하고, DTO에서 String을 반환하게 만들었어요!