From 85ee842868d9b7d35bb2152d34570302dba198d3 Mon Sep 17 00:00:00 2001 From: yxhwxn Date: Thu, 15 Aug 2024 21:08:40 +0900 Subject: [PATCH 1/6] =?UTF-8?q?Refactor:=20NullPointerException=20?= =?UTF-8?q?=EB=B0=A9=EC=A7=80=EC=9A=A9=20=EC=96=B4=EB=85=B8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cmc/suppin/event/survey/domain/AnonymousParticipant.java | 1 + src/main/java/com/cmc/suppin/event/survey/domain/Answer.java | 1 + src/main/java/com/cmc/suppin/event/survey/domain/Question.java | 2 ++ .../com/cmc/suppin/event/survey/domain/QuestionOption.java | 1 + src/main/java/com/cmc/suppin/member/domain/Member.java | 3 ++- 5 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/cmc/suppin/event/survey/domain/AnonymousParticipant.java b/src/main/java/com/cmc/suppin/event/survey/domain/AnonymousParticipant.java index 5bdac04..7ae3757 100644 --- a/src/main/java/com/cmc/suppin/event/survey/domain/AnonymousParticipant.java +++ b/src/main/java/com/cmc/suppin/event/survey/domain/AnonymousParticipant.java @@ -25,6 +25,7 @@ public class AnonymousParticipant extends BaseDateTimeEntity { private Survey survey; @OneToMany(mappedBy = "anonymousParticipant") + @Builder.Default private List answerList = new ArrayList<>(); private String name; diff --git a/src/main/java/com/cmc/suppin/event/survey/domain/Answer.java b/src/main/java/com/cmc/suppin/event/survey/domain/Answer.java index 4cbd141..073f89e 100644 --- a/src/main/java/com/cmc/suppin/event/survey/domain/Answer.java +++ b/src/main/java/com/cmc/suppin/event/survey/domain/Answer.java @@ -30,6 +30,7 @@ public class Answer extends BaseDateTimeEntity { private AnonymousParticipant anonymousParticipant; @OneToMany(mappedBy = "answer") + @Builder.Default private List answerOptionList = new ArrayList<>(); @Column(columnDefinition = "TEXT") diff --git a/src/main/java/com/cmc/suppin/event/survey/domain/Question.java b/src/main/java/com/cmc/suppin/event/survey/domain/Question.java index 98c86a5..d5b1d92 100644 --- a/src/main/java/com/cmc/suppin/event/survey/domain/Question.java +++ b/src/main/java/com/cmc/suppin/event/survey/domain/Question.java @@ -25,9 +25,11 @@ public class Question { private Survey survey; @OneToMany(mappedBy = "question") + @Builder.Default private List questionOptionList = new ArrayList<>(); @OneToMany(mappedBy = "question") + @Builder.Default private List answerList = new ArrayList<>(); @Enumerated(EnumType.STRING) diff --git a/src/main/java/com/cmc/suppin/event/survey/domain/QuestionOption.java b/src/main/java/com/cmc/suppin/event/survey/domain/QuestionOption.java index d21d337..44fc85a 100644 --- a/src/main/java/com/cmc/suppin/event/survey/domain/QuestionOption.java +++ b/src/main/java/com/cmc/suppin/event/survey/domain/QuestionOption.java @@ -27,6 +27,7 @@ public class QuestionOption { private String optionText; @OneToMany(mappedBy = "questionOption") + @Builder.Default private List answerOptionList = new ArrayList<>(); } diff --git a/src/main/java/com/cmc/suppin/member/domain/Member.java b/src/main/java/com/cmc/suppin/member/domain/Member.java index 8fd23dd..acea8ea 100644 --- a/src/main/java/com/cmc/suppin/member/domain/Member.java +++ b/src/main/java/com/cmc/suppin/member/domain/Member.java @@ -25,6 +25,7 @@ public class Member extends BaseDateTimeEntity { private Long id; @OneToMany(mappedBy = "member", cascade = CascadeType.ALL) + @Builder.Default private List eventList = new ArrayList<>(); @Column(columnDefinition = "VARCHAR(30)", nullable = false) @@ -41,7 +42,7 @@ public class Member extends BaseDateTimeEntity { @Column(columnDefinition = "VARCHAR(13)", nullable = false) private String phoneNumber; - + @Enumerated(EnumType.STRING) private UserRole role; From 7dc4a22129a94c6670b6e0880637a1c3758f0090 Mon Sep 17 00:00:00 2001 From: yxhwxn Date: Thu, 15 Aug 2024 21:25:43 +0900 Subject: [PATCH 2/6] =?UTF-8?q?Feat:=20=EC=84=A4=EB=AC=B8=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=8B=9C,=20=EA=B0=9C=EC=9D=B8=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EC=88=98=EC=A7=91=20=EB=8F=99=EC=9D=98=EC=84=9C(consentForm?= =?UTF-8?q?)=20=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/survey/controller/dto/SurveyRequestDTO.java | 1 + .../cmc/suppin/event/survey/converter/SurveyConverter.java | 3 ++- .../java/com/cmc/suppin/event/survey/domain/Survey.java | 6 ++++++ .../com/cmc/suppin/event/survey/service/SurveyService.java | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyRequestDTO.java b/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyRequestDTO.java index 992e07b..ed6c178 100644 --- a/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyRequestDTO.java +++ b/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyRequestDTO.java @@ -22,6 +22,7 @@ public class SurveyRequestDTO { public static class SurveyCreateDTO { @NotNull private Long eventId; + private String consentFormHtml; // 개인정보 수집 동의서 HTML 필드 private List personalInfoOptionList; private List questionList; diff --git a/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java b/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java index 498f47f..b9a6ba0 100644 --- a/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java +++ b/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java @@ -11,10 +11,11 @@ public class SurveyConverter { - public static Survey toSurveyEntity(Event event, String uuid) { + public static Survey toSurveyEntity(Event event, String uuid, String consentFormHtml) { return Survey.builder() .event(event) .uuid(uuid) + .consentFormHtml(consentFormHtml) .build(); } diff --git a/src/main/java/com/cmc/suppin/event/survey/domain/Survey.java b/src/main/java/com/cmc/suppin/event/survey/domain/Survey.java index f359fb1..d9d797e 100644 --- a/src/main/java/com/cmc/suppin/event/survey/domain/Survey.java +++ b/src/main/java/com/cmc/suppin/event/survey/domain/Survey.java @@ -26,12 +26,15 @@ public class Survey extends BaseDateTimeEntity { private Event event; @OneToMany(mappedBy = "survey", cascade = CascadeType.ALL, orphanRemoval = true) + @Builder.Default private List personalInfoList = new ArrayList<>(); @OneToMany(mappedBy = "survey") + @Builder.Default private List questionList = new ArrayList<>(); @OneToMany(mappedBy = "survey") + @Builder.Default private List anonymousParticipantList = new ArrayList<>(); @Column(columnDefinition = "TEXT") @@ -39,5 +42,8 @@ public class Survey extends BaseDateTimeEntity { @Column(nullable = false, updatable = false, unique = true) private String uuid; + + @Column(columnDefinition = "TEXT") + private String consentFormHtml; } diff --git a/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java b/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java index 85148cf..28f0544 100644 --- a/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java +++ b/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java @@ -54,7 +54,7 @@ public SurveyResponseDTO.SurveyCreateResponse createSurvey(SurveyRequestDTO.Surv // Survey 엔티티 생성 및 저장 String uuid = UUID.randomUUID().toString(); - Survey survey = SurveyConverter.toSurveyEntity(event, uuid); + Survey survey = SurveyConverter.toSurveyEntity(event, uuid, request.getConsentFormHtml()); surveyRepository.save(survey); // 각 개인정보 항목 처리 및 저장 From a4197820acbdb24e1429849e4788ffeee0aeffc4 Mon Sep 17 00:00:00 2001 From: yxhwxn Date: Thu, 15 Aug 2024 21:30:42 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Feat:=20=EC=84=A4=EB=AC=B8=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EC=8B=9C,=20=EA=B0=9C=EC=9D=B8=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EC=88=98=EC=A7=91=20=EB=8F=99=EC=9D=98=EC=84=9C(consentForm?= =?UTF-8?q?)=20=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cmc/suppin/event/survey/controller/SurveyApi.java | 2 +- .../suppin/event/survey/controller/dto/SurveyResponseDTO.java | 1 + .../com/cmc/suppin/event/survey/converter/SurveyConverter.java | 2 ++ .../java/com/cmc/suppin/event/survey/service/SurveyService.java | 2 +- 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cmc/suppin/event/survey/controller/SurveyApi.java b/src/main/java/com/cmc/suppin/event/survey/controller/SurveyApi.java index a4219cf..81f9b21 100644 --- a/src/main/java/com/cmc/suppin/event/survey/controller/SurveyApi.java +++ b/src/main/java/com/cmc/suppin/event/survey/controller/SurveyApi.java @@ -47,7 +47,7 @@ public ResponseEntity> getSurvey( if (uuid != null) { response = surveyService.getSurveyByUuid(uuid); } else if (surveyId != null) { - response = surveyService.getSurvey(surveyId); + response = surveyService.getSurveyBySurveyId(surveyId); } else { throw new IllegalArgumentException("Either surveyId or uuid must be provided"); } diff --git a/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyResponseDTO.java b/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyResponseDTO.java index a70df15..42bf215 100644 --- a/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyResponseDTO.java +++ b/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyResponseDTO.java @@ -31,6 +31,7 @@ public static class SurveyResultDTO { private String startDate; private String endDate; private String announcementDate; + private String consentFormHtml; private List personalInfoOptions; private List questions; diff --git a/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java b/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java index b9a6ba0..7013e46 100644 --- a/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java +++ b/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java @@ -69,11 +69,13 @@ public static SurveyResponseDTO.SurveyResultDTO toSurveyResultDTO(Survey survey, .startDate(event.getStartDate().toString()) .endDate(event.getEndDate().toString()) .announcementDate(event.getAnnouncementDate().toString()) + .consentFormHtml(survey.getConsentFormHtml()) .personalInfoOptions(personalInfoOptions) .questions(questions) .build(); } + public static SurveyResponseDTO.SurveyAnswerResultDTO toSurveyAnswerResultDTO(Question question, Page answersPage) { List answers = answersPage.stream() .map(answer -> SurveyResponseDTO.SurveyAnswerResultDTO.AnswerDTO.builder() diff --git a/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java b/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java index 28f0544..181efb8 100644 --- a/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java +++ b/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java @@ -84,7 +84,7 @@ public SurveyResponseDTO.SurveyCreateResponse createSurvey(SurveyRequestDTO.Surv // 생성된 설문지 조회 @Transactional(readOnly = true) - public SurveyResponseDTO.SurveyResultDTO getSurvey(Long surveyId) { + public SurveyResponseDTO.SurveyResultDTO getSurveyBySurveyId(Long surveyId) { Survey survey = surveyRepository.findById(surveyId) .orElseThrow(() -> new IllegalArgumentException("Survey not found")); From dfb11afe88b3fa36dd57ae1395da7aaee2f9284c Mon Sep 17 00:00:00 2001 From: yxhwxn Date: Thu, 15 Aug 2024 21:47:13 +0900 Subject: [PATCH 4/6] =?UTF-8?q?Fix:=20=ED=81=AC=EB=A1=A4=EB=A7=81=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EA=B2=80=EC=A6=9D=20=EC=8B=9C=20=EB=82=A0?= =?UTF-8?q?=EC=A7=9C=20=ED=8F=AC=EB=A7=B7=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cmc/suppin/event/crawl/service/CrawlService.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cmc/suppin/event/crawl/service/CrawlService.java b/src/main/java/com/cmc/suppin/event/crawl/service/CrawlService.java index 41730c6..05f4284 100644 --- a/src/main/java/com/cmc/suppin/event/crawl/service/CrawlService.java +++ b/src/main/java/com/cmc/suppin/event/crawl/service/CrawlService.java @@ -26,6 +26,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -46,8 +47,9 @@ public String checkExistingComments(String url, String userId) { List existingComments = commentRepository.findByUrl(url); if (!existingComments.isEmpty()) { - LocalDateTime firstCommentDate = existingComments.get(0).getCreatedAt(); - return "동일한 URL의 댓글을 " + firstCommentDate.toLocalDate() + " 일자에 수집한 이력이 있습니다."; + LocalDateTime firstCommentDate = existingComments.get(0).getCrawlTime(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + return "동일한 URL의 댓글을 " + firstCommentDate.format(formatter) + " 일자에 수집한 이력이 있습니다."; } return null; From f53a93b8e306b304749aa937a54f0ce9e0656589 Mon Sep 17 00:00:00 2001 From: yxhwxn Date: Thu, 15 Aug 2024 23:12:22 +0900 Subject: [PATCH 5/6] =?UTF-8?q?Refactor:=20=EB=9E=9C=EB=8D=A4=20=EC=B6=94?= =?UTF-8?q?=EC=B2=A8=20API=20=ED=98=B8=EC=B6=9C=20=EC=8B=9C,=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=EC=9E=90=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80(isWinner=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../suppin/event/crawl/controller/CommentApi.java | 2 +- .../com/cmc/suppin/event/crawl/domain/Comment.java | 8 ++++++++ .../suppin/event/crawl/service/CommentService.java | 7 +++++++ .../cmc/suppin/event/crawl/service/CrawlService.java | 11 ++++++++--- .../survey/controller/dto/SurveyRequestDTO.java | 8 +++++--- .../event/survey/converter/SurveyConverter.java | 8 ++++++-- .../event/survey/domain/AnonymousParticipant.java | 12 ++++++++++-- .../suppin/event/survey/service/SurveyService.java | 6 +++++- 8 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/cmc/suppin/event/crawl/controller/CommentApi.java b/src/main/java/com/cmc/suppin/event/crawl/controller/CommentApi.java index 49a88c5..578071a 100644 --- a/src/main/java/com/cmc/suppin/event/crawl/controller/CommentApi.java +++ b/src/main/java/com/cmc/suppin/event/crawl/controller/CommentApi.java @@ -44,7 +44,7 @@ public ResponseEntity> get } @PostMapping("/draft-winners") - @Operation(summary = "조건별 당첨자 추첨 API(댓글 이벤트)", description = "주어진 조건에 따라 이벤트의 당첨자를 추첨합니다.") + @Operation(summary = "당첨자 랜덤 추첨 결과 리스트 조회 API(댓글 이벤트)", description = "주어진 조건에 따라 이벤트의 당첨자를 추첨합니다.") public ResponseEntity> drawWinners( @RequestBody @Valid CommentRequestDTO.WinnerRequestDTO request, @CurrentAccount Account account) { diff --git a/src/main/java/com/cmc/suppin/event/crawl/domain/Comment.java b/src/main/java/com/cmc/suppin/event/crawl/domain/Comment.java index e47fcb6..0ac7958 100644 --- a/src/main/java/com/cmc/suppin/event/crawl/domain/Comment.java +++ b/src/main/java/com/cmc/suppin/event/crawl/domain/Comment.java @@ -41,8 +41,16 @@ public class Comment extends BaseDateTimeEntity { @Column(nullable = false) private LocalDateTime crawlTime; + @Builder.Default + @Column(nullable = false) + private boolean isWinner = false; + public void setCrawlTime(LocalDateTime crawlTime) { this.crawlTime = crawlTime; } + public void setIsWinner(boolean isWinner) { + this.isWinner = isWinner; + } + } diff --git a/src/main/java/com/cmc/suppin/event/crawl/service/CommentService.java b/src/main/java/com/cmc/suppin/event/crawl/service/CommentService.java index 2ad0965..12e2fcf 100644 --- a/src/main/java/com/cmc/suppin/event/crawl/service/CommentService.java +++ b/src/main/java/com/cmc/suppin/event/crawl/service/CommentService.java @@ -54,6 +54,7 @@ public CommentResponseDTO.CrawledCommentListDTO getComments(Long eventId, String } // 당첨자 조건별 랜덤 추첨(댓글 이벤트) + @Transactional public CommentResponseDTO.WinnerResponseDTO drawWinners(CommentRequestDTO.WinnerRequestDTO request, String userId) { Member member = memberRepository.findByUserIdAndStatusNot(userId, UserStatus.DELETED) .orElseThrow(() -> new IllegalArgumentException("Member not found")); @@ -77,6 +78,12 @@ public CommentResponseDTO.WinnerResponseDTO drawWinners(CommentRequestDTO.Winner Collections.shuffle(filteredComments); List winners = filteredComments.stream().limit(request.getWinnerCount()).collect(Collectors.toList()); + // 당첨된 댓글의 isWinner 값을 true로 업데이트 + winners.forEach(winner -> { + winner.setIsWinner(true); + commentRepository.save(winner); // 업데이트된 Comment 엔티티를 저장 + }); + return CommentConverter.toWinnerResponseDTO(winners, request); } diff --git a/src/main/java/com/cmc/suppin/event/crawl/service/CrawlService.java b/src/main/java/com/cmc/suppin/event/crawl/service/CrawlService.java index 05f4284..6706393 100644 --- a/src/main/java/com/cmc/suppin/event/crawl/service/CrawlService.java +++ b/src/main/java/com/cmc/suppin/event/crawl/service/CrawlService.java @@ -65,10 +65,14 @@ public CrawlResponseDTO.CrawlResultDTO crawlYoutubeComments(String url, Long eve if (forceUpdate) { // 기존 댓글 삭제 commentRepository.deleteByUrlAndEventId(url, eventId); + + // 삭제 후, 확인을 위한 로그 출력 또는 추가 검증 + List deletedComments = commentRepository.findByUrlAndEventId(url, eventId); + if (!deletedComments.isEmpty()) { + throw new RuntimeException("기존 댓글 삭제에 실패했습니다."); + } } else { // 기존 댓글이 존재하는 경우: 크롤링을 중지하고 예외를 던집니다. - // 기존 댓글이 존재하지 않는 경우: 새로운 댓글을 크롤링하고 이를 DB에 저장합니다. - List existingComments = commentRepository.findByUrlAndEventId(url, eventId); if (!existingComments.isEmpty()) { throw new CrawlException(CrawlErrorCode.DUPLICATE_URL); @@ -112,7 +116,7 @@ public CrawlResponseDTO.CrawlResultDTO crawlYoutubeComments(String url, Long eve while (System.currentTimeMillis() < endTime) { jsExecutor.executeScript("window.scrollTo(0, document.documentElement.scrollHeight);"); - Thread.sleep(1000); // 1초 대기 + Thread.sleep(3000); // 3초 대기 String pageSource = driver.getPageSource(); Document doc = Jsoup.parse(pageSource); @@ -151,5 +155,6 @@ public CrawlResponseDTO.CrawlResultDTO crawlYoutubeComments(String url, Long eve } return CommentConverter.toCrawlResultDTO(LocalDateTime.now(), uniqueComments.size()); } + } diff --git a/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyRequestDTO.java b/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyRequestDTO.java index ed6c178..42b8654 100644 --- a/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyRequestDTO.java +++ b/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyRequestDTO.java @@ -4,12 +4,12 @@ import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import java.time.LocalDateTime; import java.util.List; public class SurveyRequestDTO { @@ -108,9 +108,11 @@ public static class RandomSelectionRequestDTO { @NotNull private Integer winnerCount; @NotNull - private LocalDateTime startDate; + @Pattern(regexp = "\\d{4}\\. \\d{2}\\. \\d{2} \\d{2}:\\d{2}", message = "날짜 형식은 yyyy. MM. dd HH:mm 이어야 합니다.") + private String startDate; @NotNull - private LocalDateTime endDate; + @Pattern(regexp = "\\d{4}\\. \\d{2}\\. \\d{2} \\d{2}:\\d{2}", message = "날짜 형식은 yyyy. MM. dd HH:mm 이어야 합니다.") + private String endDate; @NotNull private Integer minLength; @NotNull diff --git a/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java b/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java index 7013e46..56bc1e8 100644 --- a/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java +++ b/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java @@ -6,6 +6,8 @@ import com.cmc.suppin.event.survey.domain.*; import org.springframework.data.domain.Page; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.List; import java.util.stream.Collectors; @@ -123,10 +125,12 @@ public static AnswerOption toAnswerOption(SurveyRequestDTO.SurveyAnswerDTO.Answe } public static SurveyResponseDTO.RandomSelectionResponseDTO.SelectionCriteriaDTO toSelectionCriteriaDTO(SurveyRequestDTO.RandomSelectionRequestDTO request) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy. MM. dd HH:mm"); + return SurveyResponseDTO.RandomSelectionResponseDTO.SelectionCriteriaDTO.builder() .winnerCount(request.getWinnerCount()) - .startDate(request.getStartDate()) - .endDate(request.getEndDate()) + .startDate(LocalDateTime.parse(request.getStartDate(), formatter)) + .endDate(LocalDateTime.parse(request.getEndDate(), formatter)) .minLength(request.getMinLength()) .keywords(request.getKeywords()) .build(); diff --git a/src/main/java/com/cmc/suppin/event/survey/domain/AnonymousParticipant.java b/src/main/java/com/cmc/suppin/event/survey/domain/AnonymousParticipant.java index 7ae3757..e75d3ea 100644 --- a/src/main/java/com/cmc/suppin/event/survey/domain/AnonymousParticipant.java +++ b/src/main/java/com/cmc/suppin/event/survey/domain/AnonymousParticipant.java @@ -42,11 +42,19 @@ public class AnonymousParticipant extends BaseDateTimeEntity { @Column(nullable = false) private Boolean isAgreed; - private Boolean isWinner; + @Builder.Default + @Column(nullable = false) + private Boolean isWinner = false; - private Boolean isChecked; + @Builder.Default + @Column(nullable = false) + private Boolean isChecked = false; public void setIsWinner(Boolean isWinner) { this.isWinner = isWinner; } + + public void setIsChecked(Boolean isChecked) { + this.isChecked = isChecked; + } } diff --git a/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java b/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java index 181efb8..69b51ef 100644 --- a/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java +++ b/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java @@ -20,6 +20,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Collections; import java.util.List; import java.util.UUID; @@ -172,8 +174,10 @@ public SurveyResponseDTO.RandomSelectionResponseDTO selectRandomWinners(SurveyRe .collect(Collectors.toList()); // 조건에 맞는 주관식 답변 조회 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy. MM. dd HH:mm"); + List eligibleAnswers = answerCustomRepository.findEligibleAnswers( - request.getQuestionId(), request.getStartDate(), request.getEndDate(), + request.getQuestionId(), LocalDateTime.parse(request.getStartDate(), formatter), LocalDateTime.parse(request.getEndDate(), formatter), request.getMinLength(), request.getKeywords()); // 랜덤 추첨 From b388518f724f2d95fdbcdbdd7d767637de128871 Mon Sep 17 00:00:00 2001 From: yxhwxn Date: Fri, 16 Aug 2024 00:47:39 +0900 Subject: [PATCH 6/6] =?UTF-8?q?Feat:=20=EB=8C=93=EA=B8=80/=EC=84=A4?= =?UTF-8?q?=EB=AC=B8=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=EC=9E=90=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EA=B0=84=ED=8E=B8=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/dto/CommentResponseDTO.java | 10 +++++++ .../crawl/converter/CommentConverter.java | 8 ++++++ .../domain/repository/CommentRepository.java | 2 ++ .../event/crawl/service/CommentService.java | 11 ++++++++ .../event/events/controller/EventApi.java | 26 +++++++++++++++++++ .../controller/dto/SurveyResponseDTO.java | 10 +++++++ .../survey/converter/SurveyConverter.java | 16 ++++++++++++ .../AnonymousParticipantRepository.java | 2 ++ .../event/survey/service/SurveyService.java | 15 +++++++++++ 9 files changed, 100 insertions(+) diff --git a/src/main/java/com/cmc/suppin/event/crawl/controller/dto/CommentResponseDTO.java b/src/main/java/com/cmc/suppin/event/crawl/controller/dto/CommentResponseDTO.java index a8f5fcf..2b3f462 100644 --- a/src/main/java/com/cmc/suppin/event/crawl/controller/dto/CommentResponseDTO.java +++ b/src/main/java/com/cmc/suppin/event/crawl/controller/dto/CommentResponseDTO.java @@ -41,4 +41,14 @@ public static class WinnerResponseDTO { private String endDate; private List winners; } + + @Getter + @NoArgsConstructor + @AllArgsConstructor + @Builder + public static class CommentEventWinners { + private String author; + private String commentText; + private String commentDate; + } } diff --git a/src/main/java/com/cmc/suppin/event/crawl/converter/CommentConverter.java b/src/main/java/com/cmc/suppin/event/crawl/converter/CommentConverter.java index 581a6c2..5cf58be 100644 --- a/src/main/java/com/cmc/suppin/event/crawl/converter/CommentConverter.java +++ b/src/main/java/com/cmc/suppin/event/crawl/converter/CommentConverter.java @@ -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(); + } } diff --git a/src/main/java/com/cmc/suppin/event/crawl/domain/repository/CommentRepository.java b/src/main/java/com/cmc/suppin/event/crawl/domain/repository/CommentRepository.java index 0896ba0..872a5f1 100644 --- a/src/main/java/com/cmc/suppin/event/crawl/domain/repository/CommentRepository.java +++ b/src/main/java/com/cmc/suppin/event/crawl/domain/repository/CommentRepository.java @@ -24,4 +24,6 @@ public interface CommentRepository extends JpaRepository { List findByEventIdAndCommentDateBetween(Long eventId, LocalDateTime start, LocalDateTime end); List findByEventIdAndCommentTextContaining(Long eventId, String keyword); + + List findByEventIdAndIsWinnerTrue(Long eventId); } diff --git a/src/main/java/com/cmc/suppin/event/crawl/service/CommentService.java b/src/main/java/com/cmc/suppin/event/crawl/service/CommentService.java index 12e2fcf..0ca9d05 100644 --- a/src/main/java/com/cmc/suppin/event/crawl/service/CommentService.java +++ b/src/main/java/com/cmc/suppin/event/crawl/service/CommentService.java @@ -109,4 +109,15 @@ public List filterWinnersByKeyword(List getCommentEventWinners(Long eventId, String userId) { + Member member = memberRepository.findByUserIdAndStatusNot(userId, UserStatus.DELETED) + .orElseThrow(() -> new IllegalArgumentException("Member not found")); + + List winners = commentRepository.findByEventIdAndIsWinnerTrue(eventId); + + return winners.stream() + .map(CommentConverter::toCommentEventWinners) + .collect(Collectors.toList()); + } + } diff --git a/src/main/java/com/cmc/suppin/event/events/controller/EventApi.java b/src/main/java/com/cmc/suppin/event/events/controller/EventApi.java index ad5b034..833902f 100644 --- a/src/main/java/com/cmc/suppin/event/events/controller/EventApi.java +++ b/src/main/java/com/cmc/suppin/event/events/controller/EventApi.java @@ -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; @@ -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 = "사용자의 모든 이벤트와 설문 및 댓글 수를 조회합니다.") @@ -66,4 +72,24 @@ public ResponseEntity> 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>> getCommentEventWinners( + @RequestParam("eventId") Long eventId, + @CurrentAccount Account account) { + + List winners = commentService.getCommentEventWinners(eventId, account.userId()); + return ResponseEntity.ok(ApiResponse.of(winners)); + } + + @GetMapping("/survey-winners") + @Operation(summary = "설문 이벤트 당첨자 조회 API", description = "설문 이벤트의 당첨자 리스트를 조회합니다.") + public ResponseEntity>> getSurveyEventWinners( + @RequestParam("surveyId") Long surveyId, + @CurrentAccount Account account) { + + List winners = surveyService.getSurveyEventWinners(surveyId, account.userId()); + return ResponseEntity.ok(ApiResponse.of(winners)); + } } diff --git a/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyResponseDTO.java b/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyResponseDTO.java index 42bf215..d31b596 100644 --- a/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyResponseDTO.java +++ b/src/main/java/com/cmc/suppin/event/survey/controller/dto/SurveyResponseDTO.java @@ -129,4 +129,14 @@ public static class AnswerDetailDTO { private List selectedOptions; // 객관식 질문의 경우 선택된 옵션 리스트 } } + + + @Getter + @NoArgsConstructor + @AllArgsConstructor + @Builder + public static class SurveyEventWinners { + private String name; + private List answers; + } } diff --git a/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java b/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java index 56bc1e8..47fdfe0 100644 --- a/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java +++ b/src/main/java/com/cmc/suppin/event/survey/converter/SurveyConverter.java @@ -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(); + } + } diff --git a/src/main/java/com/cmc/suppin/event/survey/domain/repository/AnonymousParticipantRepository.java b/src/main/java/com/cmc/suppin/event/survey/domain/repository/AnonymousParticipantRepository.java index 6e94add..9d894dc 100644 --- a/src/main/java/com/cmc/suppin/event/survey/domain/repository/AnonymousParticipantRepository.java +++ b/src/main/java/com/cmc/suppin/event/survey/domain/repository/AnonymousParticipantRepository.java @@ -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; @@ -13,4 +14,5 @@ public interface AnonymousParticipantRepository extends JpaRepository findBySurveyIdAndIsWinnerTrue(Long surveyId); + List findBySurveyAndIsWinnerTrue(Survey survey); } diff --git a/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java b/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java index 69b51ef..e1b2d26 100644 --- a/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java +++ b/src/main/java/com/cmc/suppin/event/survey/service/SurveyService.java @@ -231,4 +231,19 @@ public void deleteWinners(Long surveyId) { anonymousParticipantRepository.save(participant); } } + + public List 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 winners = anonymousParticipantRepository.findBySurveyAndIsWinnerTrue(survey); + + return winners.stream() + .map(SurveyConverter::toSurveyEventWinners) + .collect(Collectors.toList()); + } + }