Skip to content

Commit a56a4de

Browse files
authored
Merge pull request #92 from UMC-7th-A1Grade/refactor/temp
2 parents 8960913 + 742bee4 commit a56a4de

4 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/main/java/com/umc7th/a1grade/domain/question/exception/status/QuestionErrorStatus.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public enum QuestionErrorStatus implements BaseErrorCode {
2020
INVALID_QUESTION_TYPE(HttpStatus.BAD_REQUEST, "QUESTION4009", "유효하지 않은 문제 타입입니다."),
2121
NO_QUESTIONS_FOUND(HttpStatus.NOT_FOUND, "QUESTION4010", "조건에 맞는 문제가 없습니다."),
2222
QUESTIONLOG_NOT_FOUND(HttpStatus.NOT_FOUND, "QUESTION4009", "문제 로그가 존재하지 않습니다."),
23+
DUPLICATE_QUESTIONS(HttpStatus.BAD_REQUEST, "QUESTION4091", "이미 저장된 문제입니다."),
2324

2425
QUESTION_DATABASE_ERROR(
2526
HttpStatus.INTERNAL_SERVER_ERROR, "QUESTION5000", "문제 조회 중 데이터베이스 오류가 발생하였습니다.");

src/main/java/com/umc7th/a1grade/domain/question/repository/QuestionRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ ORDER BY RAND()
5555
// ID 값만 rand()함수를 적용하여 쿼리 최적화 진행
5656
// TIMESTAMPDIFF 대신에 INTERVAL을 사용하여 가독성 개선
5757
List<Question> findQuestionsByUserAndType(@Param("userId") Long userId);
58+
59+
Optional<Question> findByImageUrlAndMemo(String imageUrl, String memo);
5860
}

src/main/java/com/umc7th/a1grade/domain/question/repository/UserQuestionRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ List<UserQuestion> findByUserIdAndOld(
5151
+ "WHERE uq.id = :userQuestionId "
5252
+ "ORDER BY ql.submissionTime DESC")
5353
Optional<UserQuestion> findUserQuestion(@Param("userQuestionId") Long userQuestionId);
54+
55+
boolean existsByUserIdAndQuestionImageUrl(Long userId, String imageUrl);
5456
}

src/main/java/com/umc7th/a1grade/domain/question/service/QuestionServiceImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ public QuestionResponseDTO.SaveUserQuestionDTO saveQuestion(
153153
throw new GeneralException(QuestionErrorStatus.INVALID_QUESTION_TYPE);
154154
}
155155

156-
Question question = QuestionConverter.toQuestion(requestSaveQuestionDTO);
157-
158-
try {
159-
questionRepository.save(question);
160-
} catch (DataAccessException e) {
161-
throw new GeneralException(QuestionErrorStatus.QUESTION_DATABASE_ERROR);
156+
if (userQuestionRepository.existsByUserIdAndQuestionImageUrl(
157+
user.getId(), requestSaveQuestionDTO.getImageUrl())) {
158+
throw new GeneralException(QuestionErrorStatus.DUPLICATE_QUESTIONS);
162159
}
163160

161+
Question question =
162+
questionRepository.save(QuestionConverter.toQuestion(requestSaveQuestionDTO));
163+
164164
UserQuestion userQuestion =
165165
UserQuestion.builder()
166166
.user(user)

0 commit comments

Comments
 (0)