Skip to content

Commit

Permalink
Merge pull request #50 from SAIG-KMITL/feat/pretest-with-exam-answer
Browse files Browse the repository at this point in the history
Feat/pretest with exam answer
  • Loading branch information
khris-xp authored Nov 28, 2024
2 parents a7c0074 + 959234b commit f95050f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/exam-answer/dtos/exam-answer-pretest-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,24 @@ export class ExamAnswerPretestResponseDto {
questionId: '1e251a62-6339-4a59-bb56-e338f1dae55b',
isCorrect: false,
explanation: 'Rock is not biology.',
optionText: 'ROM',
},
})
selectedOption: QuestionOption;

@ApiProperty({
description: 'Select Question Data',
type: QuestionOption,
example: {
id: '9d77c1d9-b0d1-4025-880c-48073e9dc7d5',
questionId: '1e251a62-6339-4a59-bb56-e338f1dae55b',
isCorrect: true,
explanation: 'Rock is not biology.',
optionText: 'ROM',
},
})
correctAnswer: QuestionOption;

@ApiProperty({
description: 'Answer text',
type: String,
Expand Down Expand Up @@ -107,6 +121,7 @@ export class ExamAnswerPretestResponseDto {
this.examAttempt = examAnswer.examAttempt;
this.question = examAnswer.question;
this.selectedOption = examAnswer.selectedOption;
this.correctAnswer = examAnswer.correctAnswer;
this.isCorrect = examAnswer.isCorrect;
this.points = examAnswer.points;
this.createdAt = examAnswer.createdAt;
Expand Down
14 changes: 14 additions & 0 deletions src/exam-answer/dtos/exam-answer-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ export class ExamAnswerResponseDto {
})
selectedOption: QuestionOption;

@ApiProperty({
description: 'Select Question Data',
type: QuestionOption,
example: {
id: '9d77c1d9-b0d1-4025-880c-48073e9dc7d5',
questionId: '1e251a62-6339-4a59-bb56-e338f1dae55b',
isCorrect: true,
explanation: 'Rock is not biology.',
optionText: 'ROM',
},
})
correctAnswer: QuestionOption;

@ApiProperty({
description: 'Answer text',
type: String,
Expand Down Expand Up @@ -95,6 +108,7 @@ export class ExamAnswerResponseDto {
this.examAttempt = examAnswer.examAttempt;
this.question = examAnswer.question;
this.selectedOption = examAnswer.selectedOption;
this.correctAnswer = examAnswer.correctAnswer;
this.isCorrect = examAnswer.isCorrect;
this.points = examAnswer.points;
this.createdAt = examAnswer.createdAt;
Expand Down
14 changes: 14 additions & 0 deletions src/exam-answer/exam-answer.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ export class ExamAnswer {
@Column({ name: 'question_option_id' })
selectedOptionId: string;

@ManyToOne(
() => QuestionOption,
(questionOption) => questionOption.examAnswerCorrect,
{
onDelete: 'CASCADE',
nullable: false,
},
)
@JoinColumn({ name: 'correct_answer_id' })
correctAnswer: QuestionOption;

@Column({ name: 'correct_answer_id' })
correctAnswerId: string;

@Column({
nullable: false,
})
Expand Down
29 changes: 29 additions & 0 deletions src/exam-answer/exam-answer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ export class ExamAnswerService {
'question',
'selectedOption',
'examAttempt.user',
'correctAnswer',
],
select: {
examAttempt: this.selectPopulateExamAttempt(),
question: this.selectPopulateQuestion(),
selectedOption: this.selectPopulateSelectedOption(),
correctAnswer: this.selectPopulateSelectedOption(),
},
}).run();

Expand Down Expand Up @@ -186,11 +188,13 @@ export class ExamAnswerService {
'question',
'selectedOption',
'examAttempt.user',
'correctAnswer',
],
select: {
examAttempt: this.selectPopulateExamAttempt(),
question: this.selectPopulateQuestion(),
selectedOption: this.selectPopulateSelectedOption(),
correctAnswer: this.selectPopulateSelectedOption(),
},
});

Expand Down Expand Up @@ -242,11 +246,13 @@ export class ExamAnswerService {
'question',
'selectedOption',
'examAttempt.user',
'correctAnswer',
],
select: {
examAttempt: this.selectPopulateExamAttempt(),
question: this.selectPopulateQuestion(),
selectedOption: this.selectPopulateSelectedOption(),
correctAnswer: this.selectPopulateSelectedOption(),
},
}).run();
}
Expand Down Expand Up @@ -292,11 +298,13 @@ export class ExamAnswerService {
'question',
'selectedOption',
'examAttempt.user',
'correctAnswer',
],
select: {
examAttempt: this.selectPopulateExamAttempt(),
question: this.selectPopulateQuestion(),
selectedOption: this.selectPopulateSelectedOption(),
correctAnswer: this.selectPopulateSelectedOption(),
},
}).run();
}
Expand Down Expand Up @@ -341,11 +349,13 @@ export class ExamAnswerService {
'question',
'selectedOption',
'examAttempt.user',
'correctAnswer',
],
select: {
examAttempt: this.selectPopulateExamAttempt(),
question: this.selectPopulateQuestionPretest(),
selectedOption: this.selectPopulateSelectedOption(),
correctAnswer: this.selectPopulateSelectedOption(),
},
}).run();
}
Expand All @@ -368,6 +378,14 @@ export class ExamAnswerService {

if (!question) throw new NotFoundException('Not Found Question');

const correctAnswer = await this.questionOptionRepository.findOne({
where: { questionId: selectedOption.questionId, isCorrect: true },
select: this.selectPopulateSelectedOption(),
});

if (!correctAnswer)
throw new NotFoundException('Not found correct answer in this question');

const examAttempt = createExamAnswerDto.examAttemptId
? await this.examAttemptRepository.findOne({
where: { id: createExamAnswerDto.examAttemptId },
Expand All @@ -380,6 +398,8 @@ export class ExamAnswerService {
selectedOption,
question,
examAttempt,
correctAnswer,
correctAnswerId: correctAnswer.id,
});

if (!examAnswer) throw new NotFoundException("Can't create exam");
Expand Down Expand Up @@ -435,11 +455,13 @@ export class ExamAnswerService {
'question',
'selectedOption',
'examAttempt.user',
'correctAnswer',
],
select: {
examAttempt: this.selectPopulateExamAttempt(),
question: this.selectPopulateQuestion(),
selectedOption: this.selectPopulateSelectedOption(),
correctAnswer: this.selectPopulateSelectedOption(),
},
});
}
Expand Down Expand Up @@ -487,6 +509,7 @@ export class ExamAnswerService {
isCorrect: true,
explanation: true,
questionId: true,
optionText: true,
};
}

Expand Down Expand Up @@ -521,11 +544,13 @@ export class ExamAnswerService {
'selectedOption',
'question.pretest',
'question.pretest.user',
'correctAnswer',
],
select: {
examAttempt: this.selectPopulateExamAttempt(),
question: this.selectPopulateQuestionPretest(),
selectedOption: this.selectPopulateSelectedOption(),
correctAnswer: this.selectPopulateSelectedOption(),
},
}).run();

Expand Down Expand Up @@ -614,11 +639,13 @@ export class ExamAnswerService {
'selectedOption',
'question.pretest',
'question.pretest.user',
'correctAnswer',
],
select: {
examAttempt: this.selectPopulateExamAttempt(),
question: this.selectPopulateQuestionPretest(),
selectedOption: this.selectPopulateSelectedOption(),
correctAnswer: this.selectPopulateSelectedOption(),
},
}).run();
}
Expand Down Expand Up @@ -664,11 +691,13 @@ export class ExamAnswerService {
'selectedOption',
'question.pretest',
'question.pretest.user',
'correctAnswer',
],
select: {
examAttempt: this.selectPopulateExamAttempt(),
question: this.selectPopulateQuestionPretest(),
selectedOption: this.selectPopulateSelectedOption(),
correctAnswer: this.selectPopulateSelectedOption(),
},
}).run();
}
Expand Down
5 changes: 5 additions & 0 deletions src/question-option/question-option.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export class QuestionOption {
})
examAnswer: ExamAnswer[];

@OneToMany(() => ExamAnswer, (examAnswer) => examAnswer.correctAnswer, {
cascade: true,
})
examAnswerCorrect: ExamAnswer[];

@Column({
nullable: false,
})
Expand Down

0 comments on commit f95050f

Please sign in to comment.