From 6dbc09ce3e77329044505c8670e8571a45b13aab Mon Sep 17 00:00:00 2001 From: eun-seong Date: Sat, 16 Mar 2024 04:26:24 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=8C=80=EC=8B=9C=EB=B3=B4=EB=93=9C=20?= =?UTF-8?q?=EC=B4=88=EA=B8=B0=ED=99=94=20=EB=B0=8F=20=EC=9E=AC=EA=B3=84?= =?UTF-8?q?=EC=82=B0=20api=20=EC=B6=94=EA=B0=80(#120)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../namuiwiki/config/EventConfiguration.java | 7 ++++++- .../domain/survey/SurveyController.java | 7 +++++++ .../domain/survey/SurveyEventHandler.java | 17 +++++++++++++++++ .../namuiwiki/domain/survey/SurveyService.java | 11 ++++++++++- .../survey/model/dto/ResetDashboardEvent.java | 4 ++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/ResetDashboardEvent.java diff --git a/src/main/java/com/dnd/namuiwiki/config/EventConfiguration.java b/src/main/java/com/dnd/namuiwiki/config/EventConfiguration.java index 9bfa4f6..6c3acd2 100644 --- a/src/main/java/com/dnd/namuiwiki/config/EventConfiguration.java +++ b/src/main/java/com/dnd/namuiwiki/config/EventConfiguration.java @@ -1,8 +1,10 @@ package com.dnd.namuiwiki.config; +import com.dnd.namuiwiki.domain.dashboard.DashboardRepository; import com.dnd.namuiwiki.domain.dashboard.DashboardService; import com.dnd.namuiwiki.domain.statistic.StatisticsService; import com.dnd.namuiwiki.domain.survey.SurveyEventHandler; +import com.dnd.namuiwiki.domain.survey.SurveyRepository; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -13,9 +15,12 @@ public class EventConfiguration { private final StatisticsService statisticsService; private final DashboardService dashboardService; + private final SurveyRepository surveyRepository; + private final DashboardRepository dashboardRepository; + @Bean public SurveyEventHandler surveyEventHandler() { - return new SurveyEventHandler(statisticsService, dashboardService); + return new SurveyEventHandler(statisticsService, dashboardService, surveyRepository, dashboardRepository); } } diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyController.java b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyController.java index ca132d2..da9a73a 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyController.java +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyController.java @@ -57,4 +57,11 @@ public ResponseEntity getSurvey(@PathVariable("surveyId") String surveyId) { return ResponseDto.ok(response); } + @Operation(hidden = true) + @PostMapping("/reset/dashboard") + public ResponseEntity resetDashboard(@RequestParam String pwd) { + surveyService.resetDashboard(pwd); + return ResponseDto.noContent(); + } + } diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyEventHandler.java b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyEventHandler.java index 49fdee9..1b28687 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyEventHandler.java +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyEventHandler.java @@ -1,8 +1,10 @@ package com.dnd.namuiwiki.domain.survey; +import com.dnd.namuiwiki.domain.dashboard.DashboardRepository; import com.dnd.namuiwiki.domain.dashboard.DashboardService; import com.dnd.namuiwiki.domain.statistic.StatisticsService; import com.dnd.namuiwiki.domain.survey.model.dto.CreateSurveySuccessEvent; +import com.dnd.namuiwiki.domain.survey.model.dto.ResetDashboardEvent; import com.dnd.namuiwiki.domain.survey.model.entity.Survey; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -12,9 +14,13 @@ @Slf4j @RequiredArgsConstructor public class SurveyEventHandler { + private final StatisticsService statisticsService; private final DashboardService dashboardService; + private final SurveyRepository surveyRepository; + private final DashboardRepository dashboardRepository; + @Async @EventListener public void handleSurveySuccessEvent(CreateSurveySuccessEvent event) { @@ -25,4 +31,15 @@ public void handleSurveySuccessEvent(CreateSurveySuccessEvent event) { statisticsService.updateStatistics(survey); } + @Async + @EventListener + public void handleResetDashboardEvent(ResetDashboardEvent event) { + log.info("SurveyHandler.handleResetDashboardEvent"); + + dashboardRepository.deleteAll(); + surveyRepository.findAll().forEach(dashboardService::updateStatistics); + + log.info("SurveyHandler.handleResetDashboardEvent done"); + } + } diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java index 7a15cc4..155957f 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java @@ -11,12 +11,13 @@ import com.dnd.namuiwiki.domain.survey.model.dto.AnswerDto; import com.dnd.namuiwiki.domain.survey.model.dto.CreateSurveyRequest; import com.dnd.namuiwiki.domain.survey.model.dto.CreateSurveyResponse; +import com.dnd.namuiwiki.domain.survey.model.dto.CreateSurveySuccessEvent; import com.dnd.namuiwiki.domain.survey.model.dto.GetAnswersByQuestionResponse; import com.dnd.namuiwiki.domain.survey.model.dto.GetSurveyResponse; import com.dnd.namuiwiki.domain.survey.model.dto.ReceivedSurveyDto; +import com.dnd.namuiwiki.domain.survey.model.dto.ResetDashboardEvent; import com.dnd.namuiwiki.domain.survey.model.dto.SentSurveyDto; import com.dnd.namuiwiki.domain.survey.model.dto.SingleAnswerWithSurveyDetailDto; -import com.dnd.namuiwiki.domain.survey.model.dto.CreateSurveySuccessEvent; import com.dnd.namuiwiki.domain.survey.model.entity.Answer; import com.dnd.namuiwiki.domain.survey.model.entity.Survey; import com.dnd.namuiwiki.domain.survey.type.AnswerType; @@ -221,4 +222,12 @@ public void setQuestionIdForSurveyAnswers(String pwd) { }); } + public void resetDashboard(String pwd) { + if (!SETTING_PASSWORD.equals(pwd)) { + throw new ApplicationErrorException(ApplicationErrorType.NO_PERMISSION); + } + + applicationEventPublisher.publishEvent(new ResetDashboardEvent()); + } + } diff --git a/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/ResetDashboardEvent.java b/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/ResetDashboardEvent.java new file mode 100644 index 0000000..4a7b1aa --- /dev/null +++ b/src/main/java/com/dnd/namuiwiki/domain/survey/model/dto/ResetDashboardEvent.java @@ -0,0 +1,4 @@ +package com.dnd.namuiwiki.domain.survey.model.dto; + +public class ResetDashboardEvent { +}