Skip to content

Commit

Permalink
Fix: question save three seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
emost22 committed Dec 10, 2023
1 parent e4cd6f2 commit 1e8ccbf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/com/sirius/spurt/common/meta/ResultCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -189,6 +190,9 @@ public QuestionVo saveQuestion(
throw new GlobalException(NOT_EXIST_USER);
}

QuestionEntity prevQuestion = questionRepository.findTopByUserIdOrderByCreateTimestampDesc(userId);
QuestionValidator.validate(prevQuestion.getCreateTimestamp());

List<CategoryEntity> categoryEntityList =
categoryList.stream()
.map(category -> CategoryEntity.builder().category(category).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface QuestionRepository
QuestionEntity findByQuestionId(final Long id);

QuestionEntity findByQuestionIdAndUserId(final Long id, final String userId);

QuestionEntity findTopByUserIdOrderByCreateTimestampDesc(String userId);
}

0 comments on commit 1e8ccbf

Please sign in to comment.