@@ -50,7 +50,7 @@ public class PollService {
50
50
@ Autowired
51
51
private GroupMemberRepository groupMemberRepository ;
52
52
53
- <<<<<<< HEAD
53
+
54
54
private PagedResponse <PollResponse > mapPollPagetoPageResponse (UserPrincipal currentUser , Page <Poll > polls ) {
55
55
if (polls .getNumberOfElements () == 0 ) {
56
56
return new PagedResponse <>(Collections .emptyList (), polls .getNumber (),
@@ -72,8 +72,7 @@ private PagedResponse<PollResponse> mapPollPagetoPageResponse(UserPrincipal curr
72
72
return new PagedResponse <>(pollResponses , polls .getNumber (),
73
73
polls .getSize (), polls .getTotalElements (), polls .getTotalPages (), polls .isLast ());
74
74
}
75
- =======
76
- >>>>>>> 4841 b43f8923c68f2eb28eab9dc9c02310165544
75
+
77
76
78
77
public PagedResponse <PollResponse > getAllPolls (UserPrincipal currentUser , int page , int size ) {
79
78
validatePageNumberAndSize (page , size );
@@ -103,44 +102,6 @@ public PagedResponse<PollResponse> getAllPollsInGroup(Long groupId, UserPrincipa
103
102
return mapPollPagetoPageResponse (userPrincipal , polls );
104
103
}
105
104
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
105
145
106
public PagedResponse <PollResponse > getPollsCreatedBy (String username , UserPrincipal currentUser , int page , int size ) {
146
107
validatePageNumberAndSize (page , size );
@@ -235,41 +196,6 @@ private Poll createPollInternal(PollRequest request, Long createdBy, Group group
235
196
return pollRepository .save (poll );
236
197
}
237
198
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
272
-
273
199
public Poll createPoll (PollRequest pollRequest ) {
274
200
return createPollInternal (pollRequest , null , null );
275
201
}
0 commit comments