Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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,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