fix: gebruik create question functie

This commit is contained in:
Gabriellvl 2025-03-13 15:30:07 +01:00
parent 70ba819e46
commit e3e1fc3f05
4 changed files with 23 additions and 23 deletions

View file

@ -5,6 +5,10 @@
// Load .env file
// Dotenv.config();
import {Language} from "./entities/content/language.js";
export const DWENGO_API_BASE = 'https://dwengo.org/backend/api';
export const FALLBACK_LANG = 'nl';
export const FALLBACK_LANG: Language = Language.Dutch;
export const FALLBACK_SEQ_NUM = 1;

View file

@ -104,7 +104,10 @@ export async function createQuestionHandler(req: Request, res: Response): Promis
}
export async function deleteQuestionHandler(req: Request, res: Response): Promise<void> {
const questionId = getQuestionId(res, req)
const questionId = getQuestionId(req, res);
if (!questionId)
return
const question = await deleteQuestion(questionId);

View file

@ -1,7 +1,6 @@
import { Question } from '../entities/questions/question.entity.js';
import {mapToUserDTO, UserDTO} from "./user.js";
import {UserDTO} from "./user.js";
import {LearningObjectIdentifier} from "../entities/content/learning-object-identifier.js";
import {Teacher} from "../entities/users/teacher.entity";
export interface QuestionDTO {
learningObjectIdentifier: LearningObjectIdentifier;
@ -23,7 +22,7 @@ export function mapToQuestionDTO(question: Question): QuestionDTO {
return {
learningObjectIdentifier,
sequenceNumber: question.sequenceNumber,
sequenceNumber: question.sequenceNumber!,
author: question.author,
timestamp: question.timestamp.toISOString(),
content: question.content,
@ -38,19 +37,7 @@ export interface QuestionId {
export function mapToQuestionId(question: QuestionDTO): QuestionId {
return {
learningObjectIdentifier: question.learningObjectIdentifier,
sequenceNumber: question.sequenceNumber,
sequenceNumber: question.sequenceNumber!,
};
}
export function mapToQuestion(questionDTO: QuestionDTO): Question {
const question = new Question();
question.author = mapToUserDTO<Teacher>(questionDTO.author);
question.learningObjectHruid = questionDTO.learningObjectIdentifier.hruid;
question.learningObjectLanguage = questionDTO.learningObjectIdentifier.language;
question.learningObjectVersion = questionDTO.learningObjectIdentifier.version;
question.content = questionDTO.content;
question.sequenceNumber = questionDTO.sequenceNumber;
question.timestamp = questionDTO.timestamp;
return question;
}

View file

@ -1,10 +1,12 @@
import {getAnswerRepository, getQuestionRepository} from "../data/repositories.js";
import {mapToQuestion, mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from "../interfaces/question.js";
import {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";
import {mapToUser} from "../interfaces/user.js";
import {Student} from "../entities/users/student.entity.js";
export async function getAllQuestions(
id: LearningObjectIdentifier, full: boolean
@ -69,16 +71,20 @@ export async function getAnswersByQuestion(questionId: QuestionId, full: boolean
export async function createQuestion(questionDTO: QuestionDTO) {
const questionRepository = getQuestionRepository();
const question = mapToQuestion(questionDTO);
const author = mapToUser<Student>(questionDTO.author, new Student())
try {
const newQuestion = questionRepository.create(question)
await questionRepository.save(newQuestion);
await questionRepository.createQuestion({
loId: questionDTO.learningObjectIdentifier,
author,
content: questionDTO.content }
);
} catch (e) {
return null
}
return newQuestion;
return questionDTO;
}
export async function deleteQuestion(questionId: QuestionId) {