Skip to content

Commit cedfce9

Browse files
committed
Feat: 투표 상태 변경 로직 추가
1 parent e3dffaf commit cedfce9

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/main/java/devkor/com/teamcback/domain/vote/repository/CustomVoteRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ public interface CustomVoteRepository {
1313
List<Vote> getVoteByStatusWithPage(VoteStatus status, Long voteTopicId, int size);
1414

1515
List<GetVoteOptionRes> getVoteOptionByVoteId(Long voteId);
16+
17+
List<GetVoteOptionRes> getVoteOptionByVoteIdOrderByCount(Long voteId);
1618
}

src/main/java/devkor/com/teamcback/domain/vote/repository/CustomVoteRepositoryImpl.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,32 @@ public List<GetVoteOptionRes> getVoteOptionByVoteId(Long voteId) {
100100
.fetch();
101101
}
102102

103+
@Override
104+
public List<GetVoteOptionRes> getVoteOptionByVoteIdOrderByCount(Long voteId) {
105+
return jpaQueryFactory
106+
.select(
107+
new QGetVoteOptionRes(
108+
voteOption.id,
109+
voteOption.optionText,
110+
voteRecord.id.count().intValue()
111+
)
112+
)
113+
.from(vote)
114+
.join(voteOption)
115+
.on(vote.voteTopicId.eq(voteOption.voteTopicId))
116+
.leftJoin(voteRecord)
117+
.on(voteRecord.voteId.eq(vote.id)
118+
.and(voteRecord.voteOptionId.eq(voteOption.id)))
119+
.where(
120+
vote.id.eq(voteId)
121+
)
122+
.groupBy(voteOption.id, voteOption.optionText)
123+
.orderBy(
124+
voteRecord.id.count().intValue().asc()
125+
)
126+
.fetch();
127+
}
128+
103129
private BooleanExpression gtVoteCursor(Long lastVoteId) {
104130
if (lastVoteId== null) return null;
105131
return vote.id.gt(lastVoteId);

src/main/java/devkor/com/teamcback/domain/vote/service/VoteService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.util.*;
2222

23+
import static devkor.com.teamcback.domain.vote.entity.VoteStatus.CLOSED;
2324
import static devkor.com.teamcback.global.response.ResultCode.*;
2425

2526
@Service
@@ -58,7 +59,7 @@ public GetVoteRes getVoteByPlace(Long voteTopicId, Long placeId) {
5859
/**
5960
* 투표 저장
6061
*/
61-
@UpdateScore(addScore = 2)
62+
@UpdateScore(addScore = 3)
6263
@Transactional
6364
public SaveVoteRecordRes saveVoteRecord(Long userId, SaveVoteRecordReq req) {
6465
if(userId == null) throw new GlobalException(FORBIDDEN);
@@ -87,6 +88,12 @@ else if(!Objects.equals(voteRecord.getVoteOptionId(), voteOption.getId())){ //
8788
voteRecord.setVoteOptionId(null); // 기존 투표 옵션을 null로 설정 (기록 남기기)
8889
}
8990

91+
// 투표 상태 변경
92+
List<GetVoteOptionRes> voteOptionList = voteOptionRepository.getVoteOptionByVoteIdOrderByCount(vote.getId()); // 투표 항목, 현황
93+
int min = voteOptionList.get(0).getVoteCount();
94+
int max = voteOptionList.get(voteOptionList.size() - 1).getVoteCount();
95+
if(max - min >= 30) vote.changeStatus(CLOSED);
96+
9097
return new SaveVoteRecordRes();
9198
}
9299

0 commit comments

Comments
 (0)