Skip to content

Commit

Permalink
Fix: add experience three times check
Browse files Browse the repository at this point in the history
  • Loading branch information
emost22 committed Jun 16, 2024
1 parent 9fca668 commit 281e754
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ endif::[]
|7000
|질문은 3초에 1번만 작성이 가능합니다.

|7001
|경험은 3초에 1번만 작성이 가능합니다.

|===

== 현재 오류 사항
Expand Down
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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ ExperienceEntity findByExperienceIdAndUserEntityUserId(

List<ExperienceEntity> findByUserEntityUserId(final String userId);

ExperienceEntity findTopByUserEntityOrderByCreateTimestampDesc(UserEntity userEntity);

void deleteByUserEntity(UserEntity userEntity);
}

0 comments on commit 281e754

Please sign in to comment.