From e3e1fc3f05d2d817e958471e329d32c6b2559a76 Mon Sep 17 00:00:00 2001 From: Gabriellvl Date: Thu, 13 Mar 2025 15:30:07 +0100 Subject: [PATCH] fix: gebruik create question functie --- backend/src/config.ts | 6 +++++- backend/src/controllers/questions.ts | 5 ++++- backend/src/interfaces/question.ts | 19 +++---------------- backend/src/services/questions.ts | 16 +++++++++++----- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/backend/src/config.ts b/backend/src/config.ts index 8fd8ec3f..5c7dd858 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -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; diff --git a/backend/src/controllers/questions.ts b/backend/src/controllers/questions.ts index 850dcdf7..aa39853a 100644 --- a/backend/src/controllers/questions.ts +++ b/backend/src/controllers/questions.ts @@ -104,7 +104,10 @@ export async function createQuestionHandler(req: Request, res: Response): Promis } export async function deleteQuestionHandler(req: Request, res: Response): Promise { - const questionId = getQuestionId(res, req) + const questionId = getQuestionId(req, res); + + if (!questionId) + return const question = await deleteQuestion(questionId); diff --git a/backend/src/interfaces/question.ts b/backend/src/interfaces/question.ts index 33dced69..23c3edea 100644 --- a/backend/src/interfaces/question.ts +++ b/backend/src/interfaces/question.ts @@ -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(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; -} diff --git a/backend/src/services/questions.ts b/backend/src/services/questions.ts index e3c64bbb..a2fa01c6 100644 --- a/backend/src/services/questions.ts +++ b/backend/src/services/questions.ts @@ -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(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) {