@@ -50,6 +50,7 @@ public class PollService {
50
50
@ Autowired
51
51
private GroupMemberRepository groupMemberRepository ;
52
52
53
+ <<<<<<< HEAD
53
54
private PagedResponse <PollResponse > mapPollPagetoPageResponse (UserPrincipal currentUser , Page <Poll > polls ) {
54
55
if (polls .getNumberOfElements () == 0 ) {
55
56
return new PagedResponse <>(Collections .emptyList (), polls .getNumber (),
@@ -71,6 +72,8 @@ private PagedResponse<PollResponse> mapPollPagetoPageResponse(UserPrincipal curr
71
72
return new PagedResponse <>(pollResponses , polls .getNumber (),
72
73
polls .getSize (), polls .getTotalElements (), polls .getTotalPages (), polls .isLast ());
73
74
}
75
+ =======
76
+ >>>>>>> 4841 b43f8923c68f2eb28eab9dc9c02310165544
74
77
75
78
public PagedResponse <PollResponse > getAllPolls (UserPrincipal currentUser , int page , int size ) {
76
79
validatePageNumberAndSize (page , size );
@@ -100,6 +103,45 @@ public PagedResponse<PollResponse> getAllPollsInGroup(Long groupId, UserPrincipa
100
103
return mapPollPagetoPageResponse (userPrincipal , polls );
101
104
}
102
105
106
+ public PagedResponse <PollResponse > getAllPollsInGroup (Long groupId , UserPrincipal userPrincipal , int page , int size ) {
107
+ //그룹 유효성검사
108
+ Group group = groupRepository .findById (groupId )
109
+ .orElseThrow (() -> new ResourceNotFoundException ("Group" , "id" , groupId ));
110
+
111
+ //그룹 멤버 인증
112
+ if (!groupMemberRepository .existsByUserIdAndGroupId (userPrincipal .getId (), groupId )) {
113
+ throw new BadRequestException ("그룹에 가입된 사용자만 투표를 조회할 수 있습니다." );
114
+ }
115
+
116
+ //page 표시 정보 설정하기
117
+ Pageable pageable = PageRequest .of (page , size , Sort .by ("createdAt" ).descending ());
118
+ //결과 데이터 가져오기
119
+ Page <Poll > polls = pollRepository .findByGroupId (groupId , pageable );
120
+
121
+ if (polls .getNumberOfElements () == 0 ) {
122
+ return new PagedResponse <>(Collections .emptyList (), polls .getNumber (),
123
+ polls .getSize (), polls .getTotalElements (), polls .getTotalPages (), polls .isLast ());
124
+ }
125
+
126
+ // Map Polls to PollResponses containing vote counts and poll creator details
127
+ List <Long > pollIds = polls .map (Poll ::getId ).getContent ();
128
+ Map <Long , Long > choiceVoteCountMap = getChoiceVoteCountMap (pollIds );
129
+ Map <Long , Long > pollUserVoteMap = getPollUserVoteMap (userPrincipal , pollIds );
130
+ Map <Long , User > creatorMap = getPollCreatorMap (polls .getContent ());
131
+
132
+
133
+ List <PollResponse > pollResponses = polls .map (poll -> {
134
+ return ModelMapper .mapPollToPollResponse (poll ,
135
+ choiceVoteCountMap ,
136
+ creatorMap .get (poll .getCreatedBy ()),
137
+ pollUserVoteMap == null ? null : pollUserVoteMap .getOrDefault (poll .getId (), null ));
138
+ }).getContent ();
139
+
140
+ return new PagedResponse <>(pollResponses , polls .getNumber (),
141
+ polls .getSize (), polls .getTotalElements (), polls .getTotalPages (), polls .isLast ());
142
+
143
+ }
144
+
103
145
public PagedResponse <PollResponse > getPollsCreatedBy (String username , UserPrincipal currentUser , int page , int size ) {
104
146
validatePageNumberAndSize (page , size );
105
147
@@ -193,6 +235,40 @@ private Poll createPollInternal(PollRequest request, Long createdBy, Group group
193
235
return pollRepository .save (poll );
194
236
}
195
237
238
+ <<<<<<< HEAD
239
+ =======
240
+ //그룹 투표 생성
241
+ public Poll createPollInGroup (Long groupId , PollRequest request , UserPrincipal userPrincipal ) {
242
+ //그룹 엔티티 조회
243
+ Group group = groupRepository .findById (groupId )
244
+ .orElseThrow (() -> new ResourceNotFoundException ("Group" , "id" , groupId ));
245
+ //투표 객체 생성
246
+ Poll poll = new Poll ();
247
+ poll .setQuestion (request .getQuestion ());
248
+ poll .setGroup (group );
249
+ poll .setCreatedBy (userPrincipal .getId ());
250
+
251
+ //선택지 추가
252
+ request .getChoices ().forEach (choiceRequest -> {
253
+ poll .addChoice (new Choice (choiceRequest .getText ()));
254
+ });
255
+
256
+ //종료시간 계산
257
+ Instant now = Instant .now ();
258
+ Instant expirationDateTime = now .plus (Duration .ofDays (request .getPollLength ().getDays ()))
259
+ .plus (Duration .ofHours (request .getPollLength ().getHours ()));
260
+ poll .setExpirationDateTime (expirationDateTime );
261
+
262
+
263
+ //저장
264
+ return pollRepository .save (poll );
265
+ }
266
+
267
+
268
+ public PollResponse getPollById (Long pollId , UserPrincipal currentUser ) {
269
+ Poll poll = pollRepository .findById (pollId ).orElseThrow (
270
+ () -> new ResourceNotFoundException ("Poll" , "id" , pollId ));
271
+ >>>>>>> 4841 b43f8923c68f2eb28eab9dc9c02310165544
196
272
197
273
public Poll createPoll (PollRequest pollRequest ) {
198
274
return createPollInternal (pollRequest , null , null );
0 commit comments