diff --git a/src/docs/asciidoc/index.adoc b/src/docs/asciidoc/index.adoc index 35c20fc..cd142c4 100644 --- a/src/docs/asciidoc/index.adoc +++ b/src/docs/asciidoc/index.adoc @@ -73,6 +73,9 @@ endif::[] |7000 |질문은 3초에 1번만 작성이 가능합니다. +|7001 +|경험은 3초에 1번만 작성이 가능합니다. + |=== == 현재 오류 사항 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 2f40044..33e2368 100644 --- a/src/main/java/com/sirius/spurt/common/meta/ResultCode.java +++ b/src/main/java/com/sirius/spurt/common/meta/ResultCode.java @@ -10,7 +10,8 @@ public enum ResultCode { TIME_FORMAT_ERROR(4000, "시간 형식이 맞지 않습니다."), NOT_ALL_CATEGORY(5000, "ALL category는 저장할 수 없습니다."), NO_CONTENT(6000, "조회 결과가 없습니다."), - QUESTION_THREE_SECONDS(7000, "질문은 3초에 1번만 작성이 가능합니다."); + QUESTION_THREE_SECONDS(7000, "질문은 3초에 1번만 작성이 가능합니다."), + EXPERIENCE_THREE_SECONDS(7001, "경험은 3초에 1번만 작성이 가능합니다."); private Integer code; private String message; diff --git a/src/main/java/com/sirius/spurt/common/validator/ExperienceValidator.java b/src/main/java/com/sirius/spurt/common/validator/ExperienceValidator.java new file mode 100644 index 0000000..6a8556c --- /dev/null +++ b/src/main/java/com/sirius/spurt/common/validator/ExperienceValidator.java @@ -0,0 +1,30 @@ +package com.sirius.spurt.common.validator; + +import static com.sirius.spurt.common.meta.ResultCode.EXPERIENCE_THREE_SECONDS; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.store.repository.database.entity.ExperienceEntity; +import java.sql.Timestamp; + +public class ExperienceValidator { + private static long EXPERIENCE_DUPLICATE_TIME = 3000L; + + public static void validateTimestamp(ExperienceEntity experienceEntity) { + if (!isExistExperience(experienceEntity)) { + return; + } + + if (isWithin3SecondsDifference(experienceEntity.getCreateTimestamp())) { + throw new GlobalException(EXPERIENCE_THREE_SECONDS); + } + } + + private static boolean isExistExperience(ExperienceEntity experienceEntity) { + return experienceEntity != null; + } + + private static boolean isWithin3SecondsDifference(Timestamp timestamp) { + return new Timestamp(System.currentTimeMillis()).getTime() - timestamp.getTime() + < EXPERIENCE_DUPLICATE_TIME; + } +} diff --git a/src/main/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImpl.java b/src/main/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImpl.java index 9838193..06712bb 100644 --- a/src/main/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImpl.java +++ b/src/main/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImpl.java @@ -6,6 +6,7 @@ import static com.sirius.spurt.common.meta.ResultCode.TIME_FORMAT_ERROR; import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.common.validator.ExperienceValidator; import com.sirius.spurt.common.validator.UserValidator; import com.sirius.spurt.store.provider.experience.ExperienceProvider; import com.sirius.spurt.store.provider.experience.vo.CategoryVo; @@ -57,6 +58,10 @@ public ExperienceVo saveExperience( throw new GlobalException(NOT_EXIST_USER); } + ExperienceEntity prevExperience = + experienceRepository.findTopByUserEntityOrderByCreateTimestampDesc(userEntity); + ExperienceValidator.validateTimestamp(prevExperience); + ExperienceEntity experienceEntity = ExperienceEntity.builder() .title(title) diff --git a/src/main/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepository.java b/src/main/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepository.java index ae52708..51ecb0e 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepository.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepository.java @@ -11,5 +11,7 @@ ExperienceEntity findByExperienceIdAndUserEntityUserId( List findByUserEntityUserId(final String userId); + ExperienceEntity findTopByUserEntityOrderByCreateTimestampDesc(UserEntity userEntity); + void deleteByUserEntity(UserEntity userEntity); }