fix: question controller

This commit is contained in:
Gabriellvl 2025-04-06 18:30:43 +02:00
parent bd75ab8af9
commit 6a1adb0ee3
4 changed files with 49 additions and 88 deletions

View file

@ -5,12 +5,10 @@ import { Answer } from '../entities/questions/answer.entity.js';
import { mapToAnswerDTO, mapToAnswerDTOId } from '../interfaces/answer.js';
import { QuestionRepository } from '../data/questions/question-repository.js';
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
import {mapToStudent, mapToStudentDTO} from '../interfaces/student.js';
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
import {NotFoundException} from "../exceptions/not-found-exception";
import {fetchStudent} from "./students";
import {Student} from "../entities/users/student.entity";
export async function getAllQuestions(id: LearningObjectIdentifier, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
const questionRepository: QuestionRepository = getQuestionRepository();
@ -57,13 +55,7 @@ export async function getAnswersByQuestion(questionId: QuestionId, full: boolean
export async function createQuestion(questionDTO: QuestionDTO): Promise<QuestionDTO | null> {
const questionRepository = getQuestionRepository();
let author: Student;
if (typeof questionDTO.author === "string" ){
author = await fetchStudent(questionDTO.author);
} else {
author = mapToStudent(questionDTO.author)
}
const author = await fetchStudent(questionDTO.author);
await questionRepository.createQuestion({
loId: mapToLearningObjectID(questionDTO.learningObjectIdentifier),
@ -71,7 +63,6 @@ export async function createQuestion(questionDTO: QuestionDTO): Promise<Question
content: questionDTO.content,
});
return questionDTO;
}
@ -80,20 +71,11 @@ export async function deleteQuestion(questionId: QuestionId): Promise<QuestionDT
const question = await fetchQuestion(questionId);
if (!question) {
return null;
}
const loId: LearningObjectIdentifier = {
...questionId.learningObjectIdentifier,
version: questionId.learningObjectIdentifier.version ?? 1,
};
try {
await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(loId, questionId.sequenceNumber);
} catch (_) {
return null;
}
await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(loId, questionId.sequenceNumber);
return mapToQuestionDTO(question);
}