-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoteQueryService.java
More file actions
175 lines (147 loc) · 6.36 KB
/
Copy pathVoteQueryService.java
File metadata and controls
175 lines (147 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.ject.vs.vote.port;
import com.ject.vs.vote.adapter.web.dto.MyParticipatedVoteResponse;
import com.ject.vs.vote.domain.*;
import com.ject.vs.vote.exception.VoteNotFoundException;
import com.ject.vs.vote.port.in.VoteParticipationQueryUseCase;
import com.ject.vs.vote.port.in.VoteQueryUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.Clock;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class VoteQueryService implements VoteQueryUseCase, VoteParticipationQueryUseCase {
private final VoteRepository voteRepository;
private final VoteOptionRepository voteOptionRepository;
private final VoteParticipationRepository voteParticipationRepository;
private final Clock clock;
@Override
public boolean isParticipated(Long voteId, Long userId) {
return voteParticipationRepository.existsByVoteIdAndUserId(voteId, userId);
}
@Override
public Optional<Long> getSelectedOptionId(Long voteId, Long userId) {
return voteParticipationRepository.findByVoteIdAndUserId(voteId, userId)
.map(VoteParticipation::getOptionId);
}
@Override
public VoteSummary getVoteSummary(Long voteId) {
Vote vote = voteRepository.findById(voteId).orElseThrow(VoteNotFoundException::new);
return new VoteSummary(vote.getId(), vote.getTitle(), vote.getStatus(clock), vote.getEndAt());
}
@Override
public VoteChatSummary getVoteChatSummary(Long voteId) {
Vote vote = voteRepository.findById(voteId).orElseThrow(VoteNotFoundException::new);
return new VoteChatSummary(
vote.getId(),
vote.getTitle(),
vote.getThumbnailUrl(),
vote.getStatus(clock),
vote.getEndAt(),
vote.getOptionA().getLabel(),
vote.getOptionB().getLabel()
);
}
@Override
public VoteRatio getRatio(Long voteId) {
List<VoteOption> options = voteOptionRepository.findByVoteIdOrderByPosition(voteId);
if (options.size() != 2) throw new IllegalStateException("Vote must have exactly 2 options");
long total = voteParticipationRepository.countByVoteId(voteId);
long aCount = voteParticipationRepository.countByVoteIdAndOptionId(voteId, options.get(0).getId());
int aRatio = total == 0 ? 0 : (int) Math.round(aCount * 100.0 / total);
return new VoteRatio(aRatio, 100 - aRatio, (int) total);
}
@Override
public VoteOption getSelectedOption(Long voteId, Long userId) {
Optional<Long> selectedOptionId = getSelectedOptionId(voteId, userId);
Vote vote = voteRepository.findById(voteId).orElseThrow(VoteNotFoundException::new);
return vote.getOption(selectedOptionId.get());
}
@Override
public Optional<VoteOptionCode> findSelectedOptionCode(Long voteId, Long userId) {
return getSelectedOptionId(voteId, userId).map(optionId -> getSelectedOption(voteId, userId).getCode());
}
@Override
public int getParticipantCount(Long voteId) {
return (int) voteParticipationRepository.countByVoteId(voteId);
}
@Override
public List<Long> findAllVoteIdsByStatus(List<Long> voteIds, VoteStatus status) {
if (voteIds.isEmpty()) return List.of();
return voteRepository.findAllByIdIn(voteIds).stream()
.filter(vote -> {
boolean ongoing = vote.isOngoing(clock);
return status == VoteStatus.ONGOING ? ongoing : !ongoing;
})
.map(Vote::getId)
.toList();
}
// VoteParticipationQueryUseCase 구현 (채팅 도메인 호환)
@Override
public boolean isParticipant(Long voteId, Long userId) {
return isParticipated(voteId, userId);
}
@Override
public List<Long> findAllVoteIdsByUserId(Long userId) {
return voteParticipationRepository.findAllVoteIdsByUserId(userId);
}
@Override
public long countParticipantsByVoteId(Long voteId) {
return voteParticipationRepository.countByVoteId(voteId);
}
@Override
public List<Long> findAllUserIdsByVoteId(Long voteId) {
return voteParticipationRepository.findAllUserIdsByVoteId(voteId);
}
@Override
public long countParticipationByUserId(Long userId) {
return voteParticipationRepository.countByUserId(userId);
}
@Override
public MyParticipatedVoteResponse findVotesByOrder(Long userId, VoteSortType type) {
List<Vote> list;
if(type == VoteSortType.END_AT) { // 종료 임박순
list = voteRepository.findVotesByOrderByDeadLine(userId);
}
else if(type == VoteSortType.POPULAR) { // 인기순
list = voteRepository.findVotesByOrderByPopularity(userId);
}
else { // default 최신순
list = voteRepository.findVotesByOrderByLatest(userId);
}
List<MyParticipatedVoteResponse.VoteElement> elementList = list.stream()
.map(v -> new MyParticipatedVoteResponse.VoteElement(
v.getId(),
v.getTitle(),
v.getContent(),
v.getThumbnailUrl(),
v.getEndAt(),
v.getEndAt()
)).toList();
return new MyParticipatedVoteResponse(elementList.size(), elementList);
}
@Override
public MyParticipatedVoteResponse findVotesEndByOrder(Long userId, VoteSortType type) {
List<Vote> list;
if(type == VoteSortType.END_AT) {
list = voteRepository.findVotesEndByDeadLine(userId);
}
else {
list = voteRepository.findVotesEndByLatest(userId);
}
List<MyParticipatedVoteResponse.VoteElement> elementList = list.stream()
.map(v -> new MyParticipatedVoteResponse.VoteElement(
v.getId(),
v.getTitle(),
v.getContent(),
v.getThumbnailUrl(),
v.getEndAt(),
v.getEndAt()
)).toList();
return new MyParticipatedVoteResponse(elementList.size(), elementList);
}
}