33import com .example .polls .exception .BadRequestException ;
44import com .example .polls .exception .ResourceNotFoundException ;
55import com .example .polls .model .*;
6- import com .example .polls .payload .Response .PagedResponse ;
76import com .example .polls .payload .Request .PollRequest ;
8- import com .example .polls .payload .Response .PollResponse ;
97import 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 .* ;
1311import com .example .polls .security .UserPrincipal ;
1412import com .example .polls .util .AppConstants ;
1513import com .example .polls .util .ModelMapper ;
3129import java .util .function .Function ;
3230import java .util .stream .Collectors ;
3331
32+
3433@ Service
3534public class PollService {
3635
@@ -44,36 +43,66 @@ public class PollService {
4443 private UserRepository userRepository ;
4544
4645 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 ;
4752
48- public PagedResponse <PollResponse > getAllPolls (UserPrincipal currentUser , int page , int size ) {
49- validatePageNumberAndSize (page , size );
50-
51- // Retrieve Polls
52- Pageable pageable = PageRequest .of (page , size , Sort .Direction .DESC , "createdAt" );
53- Page <Poll > polls = pollRepository .findAll (pageable );
5453
54+ private PagedResponse <PollResponse > mapPollPagetoPageResponse (UserPrincipal currentUser , Page <Poll > polls ) {
5555 if (polls .getNumberOfElements () == 0 ) {
5656 return new PagedResponse <>(Collections .emptyList (), polls .getNumber (),
5757 polls .getSize (), polls .getTotalElements (), polls .getTotalPages (), polls .isLast ());
5858 }
5959
60- // Map Polls to PollResponses containing vote counts and poll creator details
6160 List <Long > pollIds = polls .map (Poll ::getId ).getContent ();
6261 Map <Long , Long > choiceVoteCountMap = getChoiceVoteCountMap (pollIds );
6362 Map <Long , Long > pollUserVoteMap = getPollUserVoteMap (currentUser , pollIds );
6463 Map <Long , User > creatorMap = getPollCreatorMap (polls .getContent ());
6564
66- List <PollResponse > pollResponses = polls .map (poll -> {
67- return ModelMapper . mapPollToPollResponse ( poll ,
68- choiceVoteCountMap ,
69- creatorMap .get (poll .getCreatedBy ()),
70- pollUserVoteMap == null ? null : pollUserVoteMap .getOrDefault (poll .getId (), null ));
71- } ).getContent ();
65+ List <PollResponse > pollResponses = polls .map (poll -> ModelMapper . mapPollToPollResponse (
66+ poll ,
67+ choiceVoteCountMap ,
68+ creatorMap .get (poll .getCreatedBy ()),
69+ pollUserVoteMap == null ? null : pollUserVoteMap .getOrDefault (poll .getId (), null )
70+ ) ).getContent ();
7271
7372 return new PagedResponse <>(pollResponses , polls .getNumber (),
7473 polls .getSize (), polls .getTotalElements (), polls .getTotalPages (), polls .isLast ());
7574 }
7675
76+
77+ public PagedResponse <PollResponse > getAllPolls (UserPrincipal currentUser , int page , int size ) {
78+ validatePageNumberAndSize (page , size );
79+
80+ // Retrieve Polls
81+ Pageable pageable = PageRequest .of (page , size , Sort .Direction .DESC , "createdAt" );
82+ Page <Poll > polls = pollRepository .findAll (pageable );
83+
84+ return mapPollPagetoPageResponse (currentUser , polls );
85+ }
86+
87+ public PagedResponse <PollResponse > getAllPollsInGroup (Long groupId , UserPrincipal userPrincipal , int page , int size ) {
88+ //그룹 유효성검사
89+ Group group = groupRepository .findById (groupId )
90+ .orElseThrow (() -> new ResourceNotFoundException ("Group" , "id" , groupId ));
91+
92+ //그룹 멤버 인증
93+ if (!groupMemberRepository .existsByUserIdAndGroupId (userPrincipal .getId (), groupId )) {
94+ throw new BadRequestException ("그룹에 가입된 사용자만 투표를 조회할 수 있습니다." );
95+ }
96+
97+ //page 표시 정보 설정하기
98+ Pageable pageable = PageRequest .of (page , size , Sort .by ("createdAt" ).descending ());
99+ //결과 데이터 가져오기
100+ Page <Poll > polls = pollRepository .findByGroupId (groupId , pageable );
101+
102+ return mapPollPagetoPageResponse (userPrincipal , polls );
103+ }
104+
105+
77106 public PagedResponse <PollResponse > getPollsCreatedBy (String username , UserPrincipal currentUser , int page , int size ) {
78107 validatePageNumberAndSize (page , size );
79108
@@ -143,29 +172,49 @@ public PagedResponse<PollResponse> getPollsVotedBy(String username, UserPrincipa
143172 }
144173
145174
146- public Poll createPoll (PollRequest pollRequest ) {
175+ private Poll createPollInternal (PollRequest request , Long createdBy , Group group ) {
147176 Poll poll = new Poll ();
148- poll .setQuestion (pollRequest .getQuestion ());
177+ poll .setQuestion (request .getQuestion ());
178+
179+ if (createdBy != null ) {
180+ poll .setCreatedBy (createdBy );
181+ }
182+
183+ if (group != null ) {
184+ poll .setGroup (group );
185+ }
149186
150- pollRequest .getChoices ().forEach (choiceRequest -> {
187+ request .getChoices ().forEach (choiceRequest -> {
151188 poll .addChoice (new Choice (choiceRequest .getText ()));
152189 });
153190
154191 Instant now = Instant .now ();
155- Instant expirationDateTime = now .plus (Duration .ofDays (pollRequest .getPollLength ().getDays ()))
156- .plus (Duration .ofHours (pollRequest .getPollLength ().getHours ()));
157-
192+ Instant expirationDateTime = now .plus (Duration .ofDays (request .getPollLength ().getDays ()))
193+ .plus (Duration .ofHours (request .getPollLength ().getHours ()));
158194 poll .setExpirationDateTime (expirationDateTime );
159195
160196 return pollRepository .save (poll );
161197 }
162198
163- public PollResponse getPollById ( Long pollId , UserPrincipal currentUser ) {
164- Poll poll = pollRepository . findById ( pollId ). orElseThrow (
165- () -> new ResourceNotFoundException ( "Poll" , "id" , pollId ));
199+ public Poll createPoll ( PollRequest pollRequest ) {
200+ return createPollInternal ( pollRequest , null , null );
201+ }
166202
167- // Retrieve Vote Counts of every choice belonging to the current poll
168- List <ChoiceVoteCount > votes = voteRepository .countByPollIdGroupByChoiceId (pollId );
203+ //그룹 투표 생성
204+ public Poll createPollInGroup (Long groupId , PollRequest request , UserPrincipal userPrincipal ) {
205+ //그룹 엔티티 조회
206+ Group group = groupRepository .findById (groupId )
207+ .orElseThrow (() -> new ResourceNotFoundException ("Group" , "id" , groupId ));
208+ boolean isMember = groupMemberRepository .existsByUserIdAndGroupId (userPrincipal .getId (), groupId );
209+ if (!isMember ) {
210+ throw new BadRequestException ("해당 그룹에 가입된 자만 투표할 수 있습니다." );
211+ }
212+ //저장
213+ return createPollInternal (request , userPrincipal .getId (), group );
214+ }
215+
216+ public PollResponse buildPollResponse (Poll poll , UserPrincipal currentUser ) {
217+ List <ChoiceVoteCount > votes = voteRepository .countByPollIdGroupByChoiceId (poll .getId ());
169218
170219 Map <Long , Long > choiceVotesMap = votes .stream ()
171220 .collect (Collectors .toMap (ChoiceVoteCount ::getChoiceId , ChoiceVoteCount ::getVoteCount ));
@@ -177,13 +226,38 @@ public PollResponse getPollById(Long pollId, UserPrincipal currentUser) {
177226 // Retrieve vote done by logged in user
178227 Vote userVote = null ;
179228 if (currentUser != null ) {
180- userVote = voteRepository .findByUserIdAndPollId (currentUser .getId (), pollId );
229+ userVote = voteRepository .findByUserIdAndPollId (currentUser .getId (), poll . getId () );
181230 }
182231
183232 return ModelMapper .mapPollToPollResponse (poll , choiceVotesMap ,
184233 creator , userVote != null ? userVote .getChoice ().getId (): null );
185234 }
186235
236+ public PollResponse getPollById (Long pollId , UserPrincipal currentUser ) {
237+ Poll poll = pollRepository .findById (pollId ).orElseThrow (
238+ () -> new ResourceNotFoundException ("Poll" , "id" , pollId ));
239+
240+ return buildPollResponse (poll , currentUser );
241+ }
242+
243+ public PollResponse getPollByIdInGroup (Long pollId , Long groupId , UserPrincipal currentUser ) {
244+ Group group = groupRepository .findById (groupId )
245+ .orElseThrow (() -> new ResourceNotFoundException ("Group" , "id" , groupId ));
246+ boolean isMember = groupMemberRepository .existsByUserIdAndGroupId (currentUser .getId (), groupId );
247+ if (!isMember ) {
248+ throw new BadRequestException ("해당 그룹에 가입된 사용자만 투표를 조회할 수 있습니다." );
249+ }
250+
251+ Poll poll = pollRepository .findById (pollId ).orElseThrow (
252+ () -> new ResourceNotFoundException ("Poll" , "id" , pollId )
253+ );
254+ if (poll .getGroup () != null && !poll .getGroup ().getId ().equals (groupId )) {
255+ throw new BadRequestException ("해당 투표는 요청한 그룹에 속하지 않습니다." );
256+ }
257+
258+ return buildPollResponse (poll , currentUser );
259+ }
260+
187261 public PollResponse castVoteAndGetUpdatedPoll (Long pollId , VoteRequest voteRequest , UserPrincipal currentUser ) {
188262 Poll poll = pollRepository .findById (pollId )
189263 .orElseThrow (() -> new ResourceNotFoundException ("Poll" , "id" , pollId ));
0 commit comments