Skip to content

Commit

Permalink
Merge pull request #214 from bucket-back/OMCT-409-limit-vote-particip…
Browse files Browse the repository at this point in the history
…ation

[OMCT-409] 최대 투표자 수를 제한하는 요구사항 추가
  • Loading branch information
Yiseull authored Dec 29, 2023
2 parents 86a6bfe + 4e48721 commit badd8a1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ public record VoteCreateRequest(

@Schema(description = "아이템2 ID", example = "2")
@NotNull(message = "아이템2 ID는 필수 값입니다.")
Long item2Id
Long item2Id,

@Schema(description = "최대 투표자 수", example = "100")
Integer maximumParticipants
) {
public VoteCreateServiceRequest toCreateVoteServiceRequest() {
return VoteCreateServiceRequest.builder()
.hobby(Hobby.fromHobbyValue(hobby))
.content(content)
.item1Id(item1Id)
.item2Id(item2Id)
.maximumParticipants(maximumParticipants)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public record VoteCreateServiceRequest(
Hobby hobby,
String content,
Long item1Id,
Long item2Id
Long item2Id,
Integer maximumParticipants
) {
public VoteCreateImplRequest toImplRequest() {
return new VoteCreateImplRequest(hobby, content, item1Id, item2Id);
return new VoteCreateImplRequest(hobby, content, item1Id, item2Id, maximumParticipants);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public class Vote extends BaseEntity {
@Column(name = "end_time", nullable = false)
private LocalDateTime endTime;

@Column(name = "maximum_participants")
private Integer maximumParticipants;

@OneToMany(mappedBy = "vote", cascade = CascadeType.ALL)
private List<Voter> voters = new ArrayList<>();

Expand All @@ -67,7 +70,8 @@ private Vote(
final Long item1Id,
final Long item2Id,
final Hobby hobby,
final String content
final String content,
final Integer maximumParticipants
) {
this.memberId = Objects.requireNonNull(memberId);
this.item1Id = Objects.requireNonNull(item1Id);
Expand All @@ -76,6 +80,7 @@ private Vote(
this.content = new Content(content);
this.startTime = LocalDateTime.now();
this.endTime = startTime.plusDays(1);
this.maximumParticipants = maximumParticipants;
}

public String getContent() {
Expand All @@ -100,4 +105,12 @@ public boolean isVoting() {
final LocalDateTime now = LocalDateTime.now();
return this.endTime.isAfter(now);
}

public void close(final LocalDateTime now) {
this.endTime = now;
}

public boolean reachMaximumParticipants() {
return this.maximumParticipants != null && this.maximumParticipants == this.voters.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Vote append(
.item2Id(request.item2Id())
.hobby(request.hobby())
.content(request.content())
.maximumParticipants(request.maximumParticipants())
.build();

return voteRepository.save(vote);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.programmers.bucketback.domains.vote.implementation;

import java.time.LocalDateTime;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -25,6 +27,10 @@ public void participate(
final Voter voter = voterReader.read(vote, memberId, itemId);

voter.participate(itemId);

if (vote.reachMaximumParticipants()) {
vote.close(LocalDateTime.now());
}
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public record VoteCreateImplRequest(
Hobby hobby,
String content,
Long item1Id,
Long item2Id
Long item2Id,
Integer maximumParticipants
) {
}

0 comments on commit badd8a1

Please sign in to comment.