Skip to content

Commit

Permalink
feat: 대시보드 초기화 및 재계산 api 추가(#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-seong committed Mar 15, 2024
1 parent 7aba98b commit 6dbc09c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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");
}

}
11 changes: 10 additions & 1 deletion src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.dnd.namuiwiki.domain.survey.model.dto;

public class ResetDashboardEvent {
}

0 comments on commit 6dbc09c

Please sign in to comment.