Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b5d091989 | ||
|
|
ab2129a965 | ||
|
|
d8ef029bef | ||
|
|
995e69e302 | ||
|
|
698bb0e99f | ||
|
|
3c0455bff7 | ||
|
|
ed109df111 | ||
|
|
e8de49732b | ||
|
|
123261aad3 |
No files matched your search
@@ -2,6 +2,7 @@ package com.example.questionbank.repository;
|
|||||||
|
|
||||||
import com.example.questionbank.domain.ExamAttempt;
|
import com.example.questionbank.domain.ExamAttempt;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
@@ -14,6 +15,8 @@ import org.springframework.stereotype.Repository;
|
|||||||
*/
|
*/
|
||||||
@Repository
|
@Repository
|
||||||
public interface ExamAttemptRepository extends JpaRepository<ExamAttempt, Long> {
|
public interface ExamAttemptRepository extends JpaRepository<ExamAttempt, Long> {
|
||||||
|
List<ExamAttempt> findAllByUserLogin(String login);
|
||||||
|
|
||||||
@Query("select examAttempt from ExamAttempt examAttempt where examAttempt.user.login = ?#{authentication.name}")
|
@Query("select examAttempt from ExamAttempt examAttempt where examAttempt.user.login = ?#{authentication.name}")
|
||||||
List<ExamAttempt> findByUserIsCurrentUser();
|
List<ExamAttempt> findByUserIsCurrentUser();
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.example.questionbank.repository;
|
|||||||
|
|
||||||
import com.example.questionbank.domain.Question;
|
import com.example.questionbank.domain.Question;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
@@ -14,6 +15,8 @@ import org.springframework.stereotype.Repository;
|
|||||||
*/
|
*/
|
||||||
@Repository
|
@Repository
|
||||||
public interface QuestionRepository extends JpaRepository<Question, Long> {
|
public interface QuestionRepository extends JpaRepository<Question, Long> {
|
||||||
|
List<Question> findAllBySubjectIdAndActiveTrue(Long subjectId);
|
||||||
|
|
||||||
default Optional<Question> findOneWithEagerRelationships(Long id) {
|
default Optional<Question> findOneWithEagerRelationships(Long id) {
|
||||||
return this.findOneWithToOneRelationships(id);
|
return this.findOneWithToOneRelationships(id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.example.questionbank.service.quiz;
|
||||||
|
|
||||||
|
import com.example.questionbank.domain.enumeration.OptionKey;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ExamScoringService {
|
||||||
|
|
||||||
|
public ScoringResult score(OptionKey correctOption, OptionKey selectedOption) {
|
||||||
|
boolean correct = selectedOption != null && selectedOption == correctOption;
|
||||||
|
return new ScoringResult(correct, correct ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public record ScoringResult(boolean correct, int awardedPoints) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package com.example.questionbank.service.quiz;
|
||||||
|
|
||||||
|
import com.example.questionbank.domain.ExamAttempt;
|
||||||
|
import com.example.questionbank.domain.Question;
|
||||||
|
import com.example.questionbank.domain.Subject;
|
||||||
|
import com.example.questionbank.domain.User;
|
||||||
|
import com.example.questionbank.domain.enumeration.AttemptStatus;
|
||||||
|
import com.example.questionbank.repository.ExamAttemptRepository;
|
||||||
|
import com.example.questionbank.repository.QuestionRepository;
|
||||||
|
import com.example.questionbank.repository.SubjectRepository;
|
||||||
|
import com.example.questionbank.repository.UserRepository;
|
||||||
|
import com.example.questionbank.web.rest.dto.ExamAttemptStartResult;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class QuizAttemptService {
|
||||||
|
|
||||||
|
private final ExamAttemptRepository attemptRepository;
|
||||||
|
private final SubjectRepository subjectRepository;
|
||||||
|
private final QuestionRepository questionRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
public QuizAttemptService(
|
||||||
|
ExamAttemptRepository attemptRepository,
|
||||||
|
SubjectRepository subjectRepository,
|
||||||
|
QuestionRepository questionRepository,
|
||||||
|
UserRepository userRepository
|
||||||
|
) {
|
||||||
|
this.attemptRepository = attemptRepository;
|
||||||
|
this.subjectRepository = subjectRepository;
|
||||||
|
this.questionRepository = questionRepository;
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public ExamAttemptStartResult start(Long subjectId, String login) {
|
||||||
|
Subject subject = subjectRepository
|
||||||
|
.findById(subjectId)
|
||||||
|
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "과목을 찾을 수 없습니다."));
|
||||||
|
|
||||||
|
if (!Boolean.TRUE.equals(subject.getActive())) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "비활성 과목은 응시할 수 없습니다.");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Question> activeQuestions = questionRepository.findAllBySubjectIdAndActiveTrue(subjectId);
|
||||||
|
|
||||||
|
if (activeQuestions.isEmpty()) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.CONFLICT, "출제된 활성 문항이 없는 과목입니다.");
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = userRepository
|
||||||
|
.findOneByLogin(login)
|
||||||
|
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "사용자를 찾을 수 없습니다."));
|
||||||
|
|
||||||
|
ExamAttempt attempt = new ExamAttempt();
|
||||||
|
attempt.setSubject(subject);
|
||||||
|
attempt.setUser(user);
|
||||||
|
attempt.setStartedAt(Instant.now());
|
||||||
|
attempt.setStatus(AttemptStatus.IN_PROGRESS);
|
||||||
|
attempt.setScore(0);
|
||||||
|
|
||||||
|
ExamAttempt saved = attemptRepository.save(attempt);
|
||||||
|
return new ExamAttemptStartResult(saved.getId(), saved.getStartedAt());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.example.questionbank.web.rest.dto;
|
||||||
|
|
||||||
|
import com.example.questionbank.domain.enumeration.OptionKey;
|
||||||
|
|
||||||
|
public record AnswerResult(
|
||||||
|
Long questionId,
|
||||||
|
String questionText,
|
||||||
|
OptionKey selectedOption,
|
||||||
|
String selectedContent,
|
||||||
|
boolean correct,
|
||||||
|
int awardedPoints,
|
||||||
|
String explanation
|
||||||
|
) {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.example.questionbank.web.rest.dto;
|
||||||
|
|
||||||
|
import com.example.questionbank.domain.enumeration.OptionKey;
|
||||||
|
|
||||||
|
public record ChoiceView(OptionKey optionKey, String content, Integer displayOrder) {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.example.questionbank.web.rest.dto;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
public record ExamAttemptStartResult(Long attemptId, Instant startedAt) {}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.example.questionbank.web.rest.dto;
|
||||||
|
|
||||||
|
import com.example.questionbank.domain.enumeration.AttemptStatus;
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
public record ExamAttemptSummary(
|
||||||
|
Long attemptId,
|
||||||
|
Long subjectId,
|
||||||
|
String subjectName,
|
||||||
|
Instant startedAt,
|
||||||
|
Instant submittedAt,
|
||||||
|
AttemptStatus status,
|
||||||
|
Integer score
|
||||||
|
) {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.example.questionbank.web.rest.dto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record ExamQuestionView(Long questionId, String prompt, List<ChoiceView> choices) {}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.example.questionbank.web.rest.dto;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record ExamResult(Long attemptId, int totalScore, Instant submittedAt, List<AnswerResult> answers) {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.example.questionbank.web.rest.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public record StartExamRequest(@NotNull Long subjectId) {}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.example.questionbank.web.rest.dto;
|
||||||
|
|
||||||
|
import com.example.questionbank.domain.enumeration.OptionKey;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public record SubmitAnswerRequest(@NotNull Long questionId, OptionKey selectedOption) {}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.questionbank.web.rest.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record SubmitExamRequest(@NotEmpty List<@Valid SubmitAnswerRequest> answers) {}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.example.questionbank.service.quiz;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import com.example.questionbank.domain.enumeration.OptionKey;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class ExamScoringServiceTest {
|
||||||
|
|
||||||
|
private final ExamScoringService service = new ExamScoringService();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void correctAnswerGetsOnePoint() {
|
||||||
|
var result = service.score(OptionKey.A, OptionKey.A);
|
||||||
|
|
||||||
|
assertTrue(result.correct());
|
||||||
|
assertEquals(1, result.awardedPoints());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void wrongAnswerGetsZeroPoints() {
|
||||||
|
var result = service.score(OptionKey.A, OptionKey.D);
|
||||||
|
|
||||||
|
assertFalse(result.correct());
|
||||||
|
assertEquals(0, result.awardedPoints());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void unansweredQuestionGetsZeroPoints() {
|
||||||
|
var result = service.score(OptionKey.A, null);
|
||||||
|
|
||||||
|
assertFalse(result.correct());
|
||||||
|
assertEquals(0, result.awardedPoints());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user