diff --git a/src/main/java/com/sirius/spurt/common/meta/ResultCode.java b/src/main/java/com/sirius/spurt/common/meta/ResultCode.java index f60ca6c..2f40044 100644 --- a/src/main/java/com/sirius/spurt/common/meta/ResultCode.java +++ b/src/main/java/com/sirius/spurt/common/meta/ResultCode.java @@ -9,7 +9,8 @@ public enum ResultCode { NOT_EXPERIENCE_OWNER(3002, "경험이 존재하지 않거나 작성자가 아닙니다."), TIME_FORMAT_ERROR(4000, "시간 형식이 맞지 않습니다."), NOT_ALL_CATEGORY(5000, "ALL category는 저장할 수 없습니다."), - NO_CONTENT(6000, "조회 결과가 없습니다."); + NO_CONTENT(6000, "조회 결과가 없습니다."), + QUESTION_THREE_SECONDS(7000, "질문은 3초에 1번만 작성이 가능합니다."); private Integer code; private String message; diff --git a/src/main/java/com/sirius/spurt/common/validator/QuestionValidator.java b/src/main/java/com/sirius/spurt/common/validator/QuestionValidator.java new file mode 100644 index 0000000..1454b8e --- /dev/null +++ b/src/main/java/com/sirius/spurt/common/validator/QuestionValidator.java @@ -0,0 +1,20 @@ +package com.sirius.spurt.common.validator; + +import static com.sirius.spurt.common.meta.ResultCode.QUESTION_THREE_SECONDS; + +import com.sirius.spurt.common.exception.GlobalException; +import java.sql.Timestamp; + +public class QuestionValidator { + private static long BOARD_DUPLICATE_TIME = 3000L; + + public static void validate(Timestamp timestamp) { + if (isWithin3SecondsDifference(timestamp)) { + throw new GlobalException(QUESTION_THREE_SECONDS); + } + } + + private static boolean isWithin3SecondsDifference(Timestamp timestamp) { + return new Timestamp(System.currentTimeMillis()).getTime() - timestamp.getTime() < BOARD_DUPLICATE_TIME; + } +} diff --git a/src/main/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImpl.java b/src/main/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImpl.java index dd72f54..b7e5ad4 100644 --- a/src/main/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImpl.java +++ b/src/main/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImpl.java @@ -6,6 +6,7 @@ import com.sirius.spurt.common.exception.GlobalException; import com.sirius.spurt.common.meta.Category; import com.sirius.spurt.common.meta.JobGroup; +import com.sirius.spurt.common.validator.QuestionValidator; import com.sirius.spurt.store.provider.question.QuestionProvider; import com.sirius.spurt.store.provider.question.vo.QuestionVo; import com.sirius.spurt.store.provider.question.vo.QuestionVoList; @@ -189,6 +190,9 @@ public QuestionVo saveQuestion( throw new GlobalException(NOT_EXIST_USER); } + QuestionEntity prevQuestion = questionRepository.findTopByUserIdOrderByCreateTimestampDesc(userId); + QuestionValidator.validate(prevQuestion.getCreateTimestamp()); + List categoryEntityList = categoryList.stream() .map(category -> CategoryEntity.builder().category(category).build()) diff --git a/src/main/java/com/sirius/spurt/store/repository/database/repository/QuestionRepository.java b/src/main/java/com/sirius/spurt/store/repository/database/repository/QuestionRepository.java index 130ff78..dbba810 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/repository/QuestionRepository.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/repository/QuestionRepository.java @@ -11,4 +11,6 @@ public interface QuestionRepository QuestionEntity findByQuestionId(final Long id); QuestionEntity findByQuestionIdAndUserId(final Long id, final String userId); + + QuestionEntity findTopByUserIdOrderByCreateTimestampDesc(String userId); }