Skip to content

Commit

Permalink
Merge pull request #149 from dnd-side-project/dev
Browse files Browse the repository at this point in the history
release v0.2.9
  • Loading branch information
eun-seong authored Mar 26, 2024
2 parents 68f9ae5 + e18e41d commit 6c3623e
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.dnd.namuiwiki.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

import java.lang.reflect.Method;

@Slf4j
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

@Override
public void handleUncaughtException(Throwable ex, Method method, Object... params) {
log.error("AsyncException message={}, declaringClass={}, methodName={}", ex.getMessage(), method.getDeclaringClass(), method.getName(), ex);

for (Object param : params) {
log.error("Parameter value - {}, declaringClass={}, methodName={}", param, method.getDeclaringClass(), method.getName());
}
}

}
24 changes: 24 additions & 0 deletions src/main/java/com/dnd/namuiwiki/config/AsyncConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.dnd.namuiwiki.config;

import com.dnd.namuiwiki.common.exception.AsyncExceptionHandler;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@Configuration
public class AsyncConfiguration implements AsyncConfigurer {

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return asyncExceptionHandler();
}

@Bean
public AsyncExceptionHandler asyncExceptionHandler() {
return new AsyncExceptionHandler();
}

}
28 changes: 28 additions & 0 deletions src/main/java/com/dnd/namuiwiki/config/CacheConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.dnd.namuiwiki.config;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@EnableCaching
@Configuration
public class CacheConfiguration {

@Bean
CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<Cache> caches = new ArrayList<>();
caches.add(new ConcurrentMapCache("questions"));
caches.add(new ConcurrentMapCache("question"));
cacheManager.setCaches(caches);
return cacheManager;
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/dnd/namuiwiki/config/EventConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.dnd.namuiwiki.config;

import com.dnd.namuiwiki.domain.statistic.StatisticsService;
import com.dnd.namuiwiki.domain.survey.SurveyEventHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class EventConfiguration {
private final StatisticsService statisticsService;

@Bean
public SurveyEventHandler surveyEventHandler() {
return new SurveyEventHandler(statisticsService);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package com.dnd.namuiwiki.domain.question;

import com.dnd.namuiwiki.domain.question.entity.Question;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.lang.NonNull;

import java.util.List;
import java.util.Optional;

public interface QuestionRepository extends MongoRepository<Question, String> {

@NonNull
@Cacheable("questions")
List<Question> findAll();

@NonNull
@Cacheable("question")
Optional<Question> findById(@NonNull String id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Question extends BaseTimeEntity {
private Long surveyOrder;
private boolean reasonRequired;

@DocumentReference
@DocumentReference(collection = "options")
private Map<String, Option> options;

public Optional<Option> getOption(String optionId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dnd.namuiwiki.domain.survey;

import com.dnd.namuiwiki.domain.statistic.StatisticsService;
import com.dnd.namuiwiki.domain.survey.model.dto.SurveyCreatedEvent;
import com.dnd.namuiwiki.domain.survey.model.entity.Survey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;

@Slf4j
@RequiredArgsConstructor
public class SurveyEventHandler {
private final StatisticsService statisticsService;

@Async
@EventListener
public void handleSurveySuccessEvent(SurveyCreatedEvent event) {
Survey survey = event.getSurvey();
log.info("SurveyEventHandler.handleSurveySuccessEvent: surveyId={}", survey.getId());

statisticsService.updateStatistics(survey);
}

}
11 changes: 8 additions & 3 deletions src/main/java/com/dnd/namuiwiki/domain/survey/SurveyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.dnd.namuiwiki.domain.option.entity.Option;
import com.dnd.namuiwiki.domain.question.QuestionRepository;
import com.dnd.namuiwiki.domain.question.entity.Question;
import com.dnd.namuiwiki.domain.statistic.StatisticsService;
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;
Expand All @@ -18,6 +17,7 @@
import com.dnd.namuiwiki.domain.survey.model.dto.ReceivedSurveyDto;
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.SurveyCreatedEvent;
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 All @@ -26,6 +26,8 @@
import com.dnd.namuiwiki.domain.user.UserRepository;
import com.dnd.namuiwiki.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -34,6 +36,7 @@

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SurveyService {
Expand All @@ -42,7 +45,7 @@ public class SurveyService {
private final QuestionRepository questionRepository;
private final OptionRepository optionRepository;
private final JwtProvider jwtProvider;
private final StatisticsService statisticsService;
private final ApplicationEventPublisher applicationEventPublisher;

public CreateSurveyResponse createSurvey(CreateSurveyRequest request, String accessToken) {
User owner = getUserByWikiId(request.getOwner());
Expand All @@ -59,7 +62,9 @@ public CreateSurveyResponse createSurvey(CreateSurveyRequest request, String acc
.answers(surveyAnswer)
.build());

statisticsService.updateStatistics(survey);
log.info("SurveyService.createSurvey: surveyId={} done", survey.getId());

applicationEventPublisher.publishEvent(new SurveyCreatedEvent(survey));

return new CreateSurveyResponse(survey.getId());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.dnd.namuiwiki.domain.survey.model.dto;

import com.dnd.namuiwiki.domain.survey.model.entity.Survey;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class SurveyCreatedEvent {
private final Survey survey;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public class Survey extends BaseTimeEntity {
@Id
private String id;

@DocumentReference
@DocumentReference(collection = "users", lazy = true)
private User owner;

@DocumentReference
@DocumentReference(collection = "users", lazy = true)
private User sender;

private String senderName;
Expand Down

0 comments on commit 6c3623e

Please sign in to comment.