3
3
import com .example .polls .exception .BadRequestException ;
4
4
import com .example .polls .exception .ResourceNotFoundException ;
5
5
import com .example .polls .model .*;
6
- import com .example .polls .payload .Response .PagedResponse ;
7
6
import com .example .polls .payload .Request .PollRequest ;
8
- import com .example .polls .payload .Response .PollResponse ;
9
7
import com .example .polls .payload .Request .VoteRequest ;
10
- import com .example .polls .repository . PollRepository ;
11
- import com .example .polls .repository . UserRepository ;
12
- import com .example .polls .repository .VoteRepository ;
8
+ import com .example .polls .payload . Response . PagedResponse ;
9
+ import com .example .polls .payload . Response . PollResponse ;
10
+ import com .example .polls .repository .* ;
13
11
import com .example .polls .security .UserPrincipal ;
14
12
import com .example .polls .util .AppConstants ;
15
13
import com .example .polls .util .ModelMapper ;
31
29
import java .util .function .Function ;
32
30
import java .util .stream .Collectors ;
33
31
32
+
34
33
@ Service
35
34
public class PollService {
36
35
@@ -44,6 +43,13 @@ public class PollService {
44
43
private UserRepository userRepository ;
45
44
46
45
private static final Logger logger = LoggerFactory .getLogger (PollService .class );
46
+ @ Autowired
47
+ private GroupRepository groupRepository ;
48
+ @ Autowired
49
+ private GroupService groupService ;
50
+ @ Autowired
51
+ private GroupMemberRepository groupMemberRepository ;
52
+
47
53
48
54
public PagedResponse <PollResponse > getAllPolls (UserPrincipal currentUser , int page , int size ) {
49
55
validatePageNumberAndSize (page , size );
@@ -74,6 +80,45 @@ public PagedResponse<PollResponse> getAllPolls(UserPrincipal currentUser, int pa
74
80
polls .getSize (), polls .getTotalElements (), polls .getTotalPages (), polls .isLast ());
75
81
}
76
82
83
+ public PagedResponse <PollResponse > getAllPollsInGroup (Long groupId , UserPrincipal userPrincipal , int page , int size ) {
84
+ //그룹 유효성검사
85
+ Group group = groupRepository .findById (groupId )
86
+ .orElseThrow (() -> new ResourceNotFoundException ("Group" , "id" , groupId ));
87
+
88
+ //그룹 멤버 인증
89
+ if (!groupMemberRepository .existsByUserIdAndGroupId (userPrincipal .getId (), groupId )) {
90
+ throw new BadRequestException ("그룹에 가입된 사용자만 투표를 조회할 수 있습니다." );
91
+ }
92
+
93
+ //page 표시 정보 설정하기
94
+ Pageable pageable = PageRequest .of (page , size , Sort .by ("createdAt" ).descending ());
95
+ //결과 데이터 가져오기
96
+ Page <Poll > polls = pollRepository .findByGroupId (groupId , pageable );
97
+
98
+ if (polls .getNumberOfElements () == 0 ) {
99
+ return new PagedResponse <>(Collections .emptyList (), polls .getNumber (),
100
+ polls .getSize (), polls .getTotalElements (), polls .getTotalPages (), polls .isLast ());
101
+ }
102
+
103
+ // Map Polls to PollResponses containing vote counts and poll creator details
104
+ List <Long > pollIds = polls .map (Poll ::getId ).getContent ();
105
+ Map <Long , Long > choiceVoteCountMap = getChoiceVoteCountMap (pollIds );
106
+ Map <Long , Long > pollUserVoteMap = getPollUserVoteMap (userPrincipal , pollIds );
107
+ Map <Long , User > creatorMap = getPollCreatorMap (polls .getContent ());
108
+
109
+
110
+ List <PollResponse > pollResponses = polls .map (poll -> {
111
+ return ModelMapper .mapPollToPollResponse (poll ,
112
+ choiceVoteCountMap ,
113
+ creatorMap .get (poll .getCreatedBy ()),
114
+ pollUserVoteMap == null ? null : pollUserVoteMap .getOrDefault (poll .getId (), null ));
115
+ }).getContent ();
116
+
117
+ return new PagedResponse <>(pollResponses , polls .getNumber (),
118
+ polls .getSize (), polls .getTotalElements (), polls .getTotalPages (), polls .isLast ());
119
+
120
+ }
121
+
77
122
public PagedResponse <PollResponse > getPollsCreatedBy (String username , UserPrincipal currentUser , int page , int size ) {
78
123
validatePageNumberAndSize (page , size );
79
124
@@ -160,6 +205,34 @@ public Poll createPoll(PollRequest pollRequest) {
160
205
return pollRepository .save (poll );
161
206
}
162
207
208
+ //그룹 투표 생성
209
+ public Poll createPollInGroup (Long groupId , PollRequest request , UserPrincipal userPrincipal ) {
210
+ //그룹 엔티티 조회
211
+ Group group = groupRepository .findById (groupId )
212
+ .orElseThrow (() -> new ResourceNotFoundException ("Group" , "id" , groupId ));
213
+ //투표 객체 생성
214
+ Poll poll = new Poll ();
215
+ poll .setQuestion (request .getQuestion ());
216
+ poll .setGroup (group );
217
+ poll .setCreatedBy (userPrincipal .getId ());
218
+
219
+ //선택지 추가
220
+ request .getChoices ().forEach (choiceRequest -> {
221
+ poll .addChoice (new Choice (choiceRequest .getText ()));
222
+ });
223
+
224
+ //종료시간 계산
225
+ Instant now = Instant .now ();
226
+ Instant expirationDateTime = now .plus (Duration .ofDays (request .getPollLength ().getDays ()))
227
+ .plus (Duration .ofHours (request .getPollLength ().getHours ()));
228
+ poll .setExpirationDateTime (expirationDateTime );
229
+
230
+
231
+ //저장
232
+ return pollRepository .save (poll );
233
+ }
234
+
235
+
163
236
public PollResponse getPollById (Long pollId , UserPrincipal currentUser ) {
164
237
Poll poll = pollRepository .findById (pollId ).orElseThrow (
165
238
() -> new ResourceNotFoundException ("Poll" , "id" , pollId ));
0 commit comments