Skip to content

Commit

Permalink
Feat: 댓글/설문 이벤트 당첨자 리스트 간편 조회 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yxhwxn committed Aug 15, 2024
1 parent f53a93b commit b388518
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ public static class WinnerResponseDTO {
private String endDate;
private List<CommentDetailDTO> winners;
}

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class CommentEventWinners {
private String author;
private String commentText;
private String commentDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,13 @@ public static CrawlResponseDTO.CrawlResultDTO toCrawlResultDTO(LocalDateTime cra
.totalCommentCount(totalCommentCount)
.build();
}

public static CommentResponseDTO.CommentEventWinners toCommentEventWinners(Comment comment) {
return CommentResponseDTO.CommentEventWinners.builder()
.author(comment.getAuthor())
.commentText(comment.getCommentText())
.commentDate(comment.getCommentDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")))
.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
List<Comment> findByEventIdAndCommentDateBetween(Long eventId, LocalDateTime start, LocalDateTime end);

List<Comment> findByEventIdAndCommentTextContaining(Long eventId, String keyword);

List<Comment> findByEventIdAndIsWinnerTrue(Long eventId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,15 @@ public List<CommentResponseDTO.CommentDetailDTO> filterWinnersByKeyword(List<Com
.collect(Collectors.toList());
}

public List<CommentResponseDTO.CommentEventWinners> getCommentEventWinners(Long eventId, String userId) {
Member member = memberRepository.findByUserIdAndStatusNot(userId, UserStatus.DELETED)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));

List<Comment> winners = commentRepository.findByEventIdAndIsWinnerTrue(eventId);

return winners.stream()
.map(CommentConverter::toCommentEventWinners)
.collect(Collectors.toList());
}

}
26 changes: 26 additions & 0 deletions src/main/java/com/cmc/suppin/event/events/controller/EventApi.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.cmc.suppin.event.events.controller;

import com.cmc.suppin.event.crawl.controller.dto.CommentResponseDTO;
import com.cmc.suppin.event.crawl.service.CommentService;
import com.cmc.suppin.event.events.controller.dto.EventRequestDTO;
import com.cmc.suppin.event.events.controller.dto.EventResponseDTO;
import com.cmc.suppin.event.events.converter.EventConverter;
import com.cmc.suppin.event.events.domain.Event;
import com.cmc.suppin.event.events.service.EventService;
import com.cmc.suppin.event.survey.controller.dto.SurveyResponseDTO;
import com.cmc.suppin.event.survey.service.SurveyService;
import com.cmc.suppin.global.response.ApiResponse;
import com.cmc.suppin.global.response.ResponseCode;
import com.cmc.suppin.global.security.reslover.Account;
Expand All @@ -29,6 +33,8 @@
public class EventApi {

private final EventService eventService;
private final CommentService commentService;
private final SurveyService surveyService;

@GetMapping("/all")
@Operation(summary = "전체 이벤트 조회 API", description = "사용자의 모든 이벤트와 설문 및 댓글 수를 조회합니다.")
Expand Down Expand Up @@ -66,4 +72,24 @@ public ResponseEntity<ApiResponse<Void>> deleteEvent(@PathVariable("eventId") Lo
eventService.deleteEvent(eventId, account.userId());
return ResponseEntity.ok(ApiResponse.of(ResponseCode.SUCCESS));
}

@GetMapping("/comment-winners")
@Operation(summary = "댓글 이벤트 당첨자 조회 API", description = "댓글 이벤트의 당첨자 리스트를 조회합니다.")
public ResponseEntity<ApiResponse<List<CommentResponseDTO.CommentEventWinners>>> getCommentEventWinners(
@RequestParam("eventId") Long eventId,
@CurrentAccount Account account) {

List<CommentResponseDTO.CommentEventWinners> winners = commentService.getCommentEventWinners(eventId, account.userId());
return ResponseEntity.ok(ApiResponse.of(winners));
}

@GetMapping("/survey-winners")
@Operation(summary = "설문 이벤트 당첨자 조회 API", description = "설문 이벤트의 당첨자 리스트를 조회합니다.")
public ResponseEntity<ApiResponse<List<SurveyResponseDTO.SurveyEventWinners>>> getSurveyEventWinners(
@RequestParam("surveyId") Long surveyId,
@CurrentAccount Account account) {

List<SurveyResponseDTO.SurveyEventWinners> winners = surveyService.getSurveyEventWinners(surveyId, account.userId());
return ResponseEntity.ok(ApiResponse.of(winners));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,14 @@ public static class AnswerDetailDTO {
private List<String> selectedOptions; // 객관식 질문의 경우 선택된 옵션 리스트
}
}


@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class SurveyEventWinners {
private String name;
private List<WinnerDetailDTO.AnswerDetailDTO> answers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,20 @@ public static SurveyResponseDTO.WinnerDetailDTO toWinnerDetailDTO(AnonymousParti
.answers(answers)
.build();
}

public static SurveyResponseDTO.SurveyEventWinners toSurveyEventWinners(AnonymousParticipant participant) {
return SurveyResponseDTO.SurveyEventWinners.builder()
.name(participant.getName())
.answers(participant.getAnswerList().stream()
.map(answer -> SurveyResponseDTO.WinnerDetailDTO.AnswerDetailDTO.builder()
.questionText(answer.getQuestion().getQuestionText())
.answerText(answer.getAnswerText())
.selectedOptions(answer.getAnswerOptionList().stream()
.map(answerOption -> answerOption.getQuestionOption().getOptionText())
.collect(Collectors.toList()))
.build())
.collect(Collectors.toList()))
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cmc.suppin.event.survey.domain.repository;

import com.cmc.suppin.event.survey.domain.AnonymousParticipant;
import com.cmc.suppin.event.survey.domain.Survey;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -13,4 +14,5 @@ public interface AnonymousParticipantRepository extends JpaRepository<AnonymousP

List<AnonymousParticipant> findBySurveyIdAndIsWinnerTrue(Long surveyId);

List<AnonymousParticipant> findBySurveyAndIsWinnerTrue(Survey survey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,19 @@ public void deleteWinners(Long surveyId) {
anonymousParticipantRepository.save(participant);
}
}

public List<SurveyResponseDTO.SurveyEventWinners> getSurveyEventWinners(Long surveyId, String userId) {
Member member = memberRepository.findByUserIdAndStatusNot(userId, UserStatus.DELETED)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));

Survey survey = surveyRepository.findById(surveyId)
.orElseThrow(() -> new IllegalArgumentException("Survey not found"));

List<AnonymousParticipant> winners = anonymousParticipantRepository.findBySurveyAndIsWinnerTrue(survey);

return winners.stream()
.map(SurveyConverter::toSurveyEventWinners)
.collect(Collectors.toList());
}

}

0 comments on commit b388518

Please sign in to comment.