feat: question route als subroute van learning objects
This commit is contained in:
parent
3e2c73320b
commit
3dd1edc95d
6 changed files with 310 additions and 49 deletions
103
backend/src/services/questions.ts
Normal file
103
backend/src/services/questions.ts
Normal file
|
@ -0,0 +1,103 @@
|
|||
import {getAnswerRepository, getQuestionRepository} from "../data/repositories.js";
|
||||
import {mapToQuestion, mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from "../interfaces/question.js";
|
||||
import {Question} from "../entities/questions/question.entity.js";
|
||||
import {Answer} from "../entities/questions/answer.entity.js";
|
||||
import {mapToAnswerDTO, mapToAnswerId} from "../interfaces/answer.js";
|
||||
import {QuestionRepository} from "../data/questions/question-repository.js";
|
||||
import {LearningObjectIdentifier} from "../entities/content/learning-object-identifier.js";
|
||||
|
||||
export async function getAllQuestions(
|
||||
id: LearningObjectIdentifier, full: boolean
|
||||
): Promise<QuestionDTO[] | QuestionId[]> {
|
||||
const questionRepository: QuestionRepository = getQuestionRepository();
|
||||
const questions = await questionRepository.findAllQuestionsAboutLearningObject(id);
|
||||
|
||||
if (!questions) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const questionsDTO: QuestionDTO[] = questions.map(mapToQuestionDTO);
|
||||
|
||||
if (full) {
|
||||
return questionsDTO;
|
||||
}
|
||||
|
||||
return questionsDTO.map(mapToQuestionId);
|
||||
}
|
||||
|
||||
async function fetchQuestion(questionId: QuestionId): Promise<Question | null> {
|
||||
const questionRepository = getQuestionRepository();
|
||||
|
||||
return await questionRepository.findOne(
|
||||
{
|
||||
learningObjectHruid: questionId.learningObjectIdentifier.hruid,
|
||||
learningObjectLanguage: questionId.learningObjectIdentifier.language,
|
||||
learningObjectVersion: questionId.learningObjectIdentifier.version,
|
||||
sequenceNumber: questionId.sequenceNumber
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export async function getQuestion(questionId: QuestionId): Promise<QuestionDTO | null> {
|
||||
const question = await fetchQuestion(questionId);
|
||||
|
||||
if (!question)
|
||||
return null
|
||||
|
||||
return mapToQuestionDTO(question);
|
||||
}
|
||||
|
||||
export async function getAnswersByQuestion(questionId: QuestionId, full: boolean) {
|
||||
const answerRepository = getAnswerRepository();
|
||||
const question = await fetchQuestion(questionId);
|
||||
|
||||
if (!question)
|
||||
return [];
|
||||
|
||||
const answers: Answer[] = await answerRepository.findAllAnswersToQuestion(question);
|
||||
|
||||
if (!answers)
|
||||
return []
|
||||
|
||||
const answersDTO = answers.map(mapToAnswerDTO);
|
||||
|
||||
if (full)
|
||||
return answersDTO
|
||||
|
||||
return answersDTO.map(mapToAnswerId);
|
||||
}
|
||||
|
||||
export async function createQuestion(questionDTO: QuestionDTO) {
|
||||
const questionRepository = getQuestionRepository();
|
||||
const question = mapToQuestion(questionDTO);
|
||||
|
||||
try {
|
||||
const newQuestion = questionRepository.create(question)
|
||||
await questionRepository.save(newQuestion);
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
|
||||
return newQuestion;
|
||||
}
|
||||
|
||||
export async function deleteQuestion(questionId: QuestionId) {
|
||||
const questionRepository = getQuestionRepository();
|
||||
|
||||
const question = await fetchQuestion(questionId);
|
||||
|
||||
if (!question)
|
||||
return null
|
||||
|
||||
try {
|
||||
await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(
|
||||
questionId.learningObjectIdentifier, questionId.sequenceNumber
|
||||
);
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
|
||||
return question
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue