@@ -78,7 +78,7 @@ public PollDto createPoll(PollCreateDto request, Long memberId) {
7878 .build ();
7979 pollOptionsRepository .save (option );
8080 }
81- return convertToDto (savedPoll );
81+ return convertToDto (savedPoll , memberId , false );
8282 } catch (ResponseStatusException e ) {
8383 throw e ;
8484 } catch (Exception e ) {
@@ -90,39 +90,7 @@ public PollDto createPoll(PollCreateDto request, Long memberId) {
9090 public PollDto getPoll (Long pollId , Long memberId ) {
9191 Poll poll = pollRepository .findById (pollId )
9292 .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "투표를 찾을 수 없습니다." ));
93- List <PollOptions > options = pollOptionsRepository .findByPoll_PollId (poll .getPollId ());
94- List <PollOptionDto > optionDtos = new ArrayList <>();
95- Long totalVoteCount = pollVoteRepository .countByPollId (poll .getPollId ());
96- for (int i = 0 ; i < options .size (); i ++) {
97- PollOptions option = options .get (i );
98- Long voteCount = pollVoteRepository .countByPollOptionId (option .getPollItemsId ());
99- boolean voted = false ;
100- log .info ("voted 판단 진입: memberId={}, pollItemsId={}" , memberId , option .getPollItemsId ());
101- if (memberId != null ) {
102- voted = pollVoteRepository .findByMember_MemberIdAndPollOptions_PollItemsId (memberId , option .getPollItemsId ()).isPresent ();
103- log .info ("투표 옵션 체크: memberId={}, pollItemsId={}, voted={}" , memberId , option .getPollItemsId (), voted );
104- }
105- optionDtos .add (PollOptionDto .builder ()
106- .pollItemsId (option .getPollItemsId ())
107- .content (option .getOption ())
108- .voteCount (voteCount )
109- .statics (null )
110- .pollOptionIndex (i + 1 )
111- .voted (voted )
112- .build ());
113- }
114- LocalDateTime expectedCloseAt = poll .getReservedCloseAt () != null ? poll .getReservedCloseAt () : poll .getCreatedAt ().plusDays (7 );
115- return PollDto .builder ()
116- .pollId (poll .getPollId ())
117- .postId (poll .getPost () != null ? poll .getPost ().getPostId () : null )
118- .voteTitle (poll .getVoteTitle ())
119- .status (PollDto .PollStatus .valueOf (poll .getStatus ().name ()))
120- .createdAt (poll .getCreatedAt ())
121- .closedAt (poll .getClosedAt ())
122- .expectedCloseAt (expectedCloseAt )
123- .pollOptions (optionDtos )
124- .totalVoteCount (totalVoteCount )
125- .build ();
93+ return convertToDto (poll , memberId , false );
12694 }
12795
12896 @ Override
@@ -347,9 +315,12 @@ public Long getVoteCountByPostId(Long postId) {
347315
348316
349317 @ Override
350- public PollDto updatePoll (Long pollId , PollUpdateDto pollUpdateDto ) {
318+ public PollDto updatePoll (Long pollId , PollUpdateDto pollUpdateDto , Long memberId ) {
351319 Poll poll = pollRepository .findById (pollId )
352320 .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "수정할 투표를 찾을 수 없습니다." ));
321+ if (!poll .getPost ().getMember ().getMemberId ().equals (memberId )) {
322+ throw new ResponseStatusException (HttpStatus .FORBIDDEN , "본인만 투표를 수정할 수 있습니다." );
323+ }
353324 if (getVoteCountByPollId (pollId ) > 0 ) {
354325 throw new ResponseStatusException (HttpStatus .FORBIDDEN , "투표가 진행된 투표는 수정할 수 없습니다." );
355326 }
@@ -403,7 +374,7 @@ public PollDto updatePoll(Long pollId, PollUpdateDto pollUpdateDto) {
403374 System .out .println ("poll에 저장된 reservedCloseAt 값: " + poll .getReservedCloseAt ());
404375 }
405376 Poll updated = pollRepository .save (poll );
406- return convertToDto (updated );
377+ return convertToDto (updated , null , false );
407378 }
408379
409380 @ Override
@@ -467,22 +438,24 @@ public void patchUpdatePoll(Long pollId, PollUpdateDto pollUpdateDto) {
467438 public PollDto getPollWithStatistics (Long pollId , Long memberId ) {
468439 Poll poll = pollRepository .findById (pollId )
469440 .orElseThrow (() -> new ResponseStatusException (HttpStatus .NOT_FOUND , "투표를 찾을 수 없습니다." ));
441+ return convertToDto (poll , memberId , true );
442+ }
443+
444+ private PollDto convertToDto (Poll poll , Long memberId , boolean withStatistics ) {
470445 List <PollOptions > options = pollOptionsRepository .findByPoll_PollId (poll .getPollId ());
471- List <Long > optionIds = options . stream (). map ( PollOptions :: getPollItemsId ). toList ();
446+ List <PollOptionDto > optionDtos = new ArrayList <> ();
472447 Long totalVoteCount = pollVoteRepository .countByPollId (poll .getPollId ());
473- List <PollOptionDto > optionDtos ;
474- boolean voted = false ;
475- if (memberId != null ) {
476- voted = pollVoteRepository .findByMember_MemberIdAndPoll_PollId (memberId , pollId ).isPresent ();
477- }
478- if (poll .getStatus () == Poll .PollStatus .CLOSED && !optionIds .isEmpty ()) {
479- List <Object []> staticsRaw = pollVoteRepository .countStaticsByPollOptionIds (optionIds );
480- optionDtos = new ArrayList <>();
481- for (int i = 0 ; i < options .size (); i ++) {
482- PollOptions option = options .get (i );
483- Long voteCount = pollVoteRepository .countByPollOptionId (option .getPollItemsId ());
484- List <PollStaticsDto > statics = staticsRaw .stream ()
485- .filter (arr -> ((Long )arr [0 ]).equals (option .getPollItemsId ()))
448+ for (int i = 0 ; i < options .size (); i ++) {
449+ PollOptions option = options .get (i );
450+ Long voteCount = pollVoteRepository .countByPollOptionId (option .getPollItemsId ());
451+ boolean voted = false ;
452+ if (memberId != null ) {
453+ voted = pollVoteRepository .findByMember_MemberIdAndPollOptions_PollItemsId (memberId , option .getPollItemsId ()).isPresent ();
454+ }
455+ List <PollStaticsDto > statics = null ;
456+ if (withStatistics && poll .getStatus () == Poll .PollStatus .CLOSED ) {
457+ List <Object []> staticsRaw = pollVoteRepository .countStaticsByPollOptionIds (List .of (option .getPollItemsId ()));
458+ statics = staticsRaw .stream ()
486459 .map (arr -> {
487460 String gender = arr [1 ] != null ? arr [1 ].toString () : null ;
488461 Integer age = arr [2 ] != null ? ((Number )arr [2 ]).intValue () : null ;
@@ -493,50 +466,14 @@ public PollDto getPollWithStatistics(Long pollId, Long memberId) {
493466 .voteCount ((Long )arr [3 ])
494467 .build ();
495468 }).toList ();
496- voted = false ;
497- if (memberId != null ) {
498- voted = pollVoteRepository .findByMember_MemberIdAndPollOptions_PollItemsId (memberId , option .getPollItemsId ()).isPresent ();
499- }
500- optionDtos .add (PollOptionDto .builder ()
501- .pollItemsId (option .getPollItemsId ())
502- .content (option .getOption ())
503- .voteCount (voteCount )
504- .statics (statics )
505- .voted (voted )
506- .build ());
507469 }
508- } else {
509- optionDtos = options .stream ().map (option -> PollOptionDto .builder ()
510- .pollItemsId (option .getPollItemsId ())
511- .content (option .getOption ())
512- .voteCount (pollVoteRepository .countByPollOptionId (option .getPollItemsId ()))
513- .build ()).toList ();
514- }
515- return PollDto .builder ()
516- .pollId (poll .getPollId ())
517- .voteTitle (poll .getVoteTitle ())
518- .status (PollDto .PollStatus .valueOf (poll .getStatus ().name ()))
519- .expectedCloseAt (poll .getReservedCloseAt ())
520- .createdAt (poll .getCreatedAt ())
521- .closedAt (poll .getClosedAt ())
522- .pollOptions (optionDtos )
523- .totalVoteCount (totalVoteCount )
524- .build ();
525- }
526-
527- private PollDto convertToDto (Poll poll ) {
528- List <PollOptions > options = pollOptionsRepository .findByPoll_PollId (poll .getPollId ());
529- List <PollOptionDto > optionDtos = new ArrayList <>();
530- Long totalVoteCount = pollVoteRepository .countByPollId (poll .getPollId ());
531- for (int i = 0 ; i < options .size (); i ++) {
532- PollOptions option = options .get (i );
533- Long voteCount = pollVoteRepository .countByPollOptionId (option .getPollItemsId ());
534470 optionDtos .add (PollOptionDto .builder ()
535471 .pollItemsId (option .getPollItemsId ())
536472 .content (option .getOption ())
537473 .voteCount (voteCount )
538- .statics (null )
474+ .statics (statics )
539475 .pollOptionIndex (i + 1 )
476+ .voted (voted )
540477 .build ());
541478 }
542479 LocalDateTime expectedCloseAt = poll .getReservedCloseAt () != null ? poll .getReservedCloseAt () : poll .getCreatedAt ().plusDays (7 );
0 commit comments