feat: answer routes en put question

This commit is contained in:
Gabriellvl 2025-04-06 23:37:54 +02:00
parent 6a1adb0ee3
commit e8e2466b76
12 changed files with 288 additions and 62 deletions

View file

@ -0,0 +1,68 @@
import {getAnswerRepository} from "../data/repositories";
import {Answer} from "../entities/questions/answer.entity";
import {mapToAnswerDTO, mapToAnswerDTOId} from "../interfaces/answer";
import {fetchTeacher} from "./teachers";
import {fetchQuestion} from "./questions";
import {QuestionId} from "@dwengo-1/common/interfaces/question";
import {AnswerData, AnswerDTO, AnswerId} from "@dwengo-1/common/interfaces/answer";
import {NotFoundException} from "../exceptions/not-found-exception";
export async function getAnswersByQuestion(questionId: QuestionId, full: boolean): Promise<AnswerDTO[] | AnswerId[]> {
const answerRepository = getAnswerRepository();
const question = await fetchQuestion(questionId);
const answers: Answer[] = await answerRepository.findAllAnswersToQuestion(question);
if (full) {
return answers.map(mapToAnswerDTO);
}
return answers.map(mapToAnswerDTOId);
}
export async function createAnswer(questionId: QuestionId, answerData: AnswerData): Promise<AnswerDTO> {
const answerRepository = getAnswerRepository();
const toQuestion = await fetchQuestion(questionId);
const author = await fetchTeacher(answerData.author);
const content = answerData.content;
const answer = await answerRepository.createAnswer({
toQuestion, author, content
});
return mapToAnswerDTO(answer);
}
async function fetchAnswer(questionId: QuestionId, sequenceNumber: number): Promise<Answer> {
const answerRepository = getAnswerRepository();
const question = await fetchQuestion(questionId);
const answer = await answerRepository.findAnswer(question, sequenceNumber);
if (!answer){
throw new NotFoundException('Answer with questionID and sequence number not found');
}
return answer;
}
export async function getAnswer(questionId: QuestionId, sequenceNumber: number): Promise<AnswerDTO> {
const answer = await fetchAnswer(questionId, sequenceNumber);
return mapToAnswerDTO(answer);
}
export async function deleteAnswer(questionId: QuestionId, sequenceNumber: number): Promise<AnswerDTO> {
const answerRepository = getAnswerRepository();
const question = await fetchQuestion(questionId);
const answer = await fetchAnswer(questionId, sequenceNumber);
await answerRepository.removeAnswerByQuestionAndSequenceNumber(question, sequenceNumber);
return mapToAnswerDTO(answer);
}
export async function updateAnswer(questionId: QuestionId, sequenceNumber: number, answerData: AnswerData){
const answerRepository = getAnswerRepository();
const answer = await fetchAnswer(questionId, sequenceNumber);
const newAnswer = await answerRepository.updateContent(answer, answerData.content);
return mapToAnswerDTO(newAnswer);
}

View file

@ -1,13 +1,11 @@
import { getAnswerRepository, getQuestionRepository } from '../data/repositories.js';
import { getQuestionRepository } from '../data/repositories.js';
import {mapToLearningObjectID, mapToQuestionDTO, mapToQuestionDTOId} from '../interfaces/question.js';
import { Question } from '../entities/questions/question.entity.js';
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 { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
import {QuestionData, QuestionDTO, QuestionId} from '@dwengo-1/common/interfaces/question';
import {NotFoundException} from "../exceptions/not-found-exception";
import {FALLBACK_VERSION_NUM} from "../config";
import {fetchStudent} from "./students";
export async function getAllQuestions(id: LearningObjectIdentifier, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
@ -21,7 +19,7 @@ export async function getAllQuestions(id: LearningObjectIdentifier, full: boolea
return questions.map(mapToQuestionDTOId);
}
async function fetchQuestion(questionId: QuestionId): Promise<Question> {
export async function fetchQuestion(questionId: QuestionId): Promise<Question> {
const questionRepository = getQuestionRepository();
const question = await questionRepository.findByLearningObjectAndSequenceNumber(
mapToLearningObjectID(questionId.learningObjectIdentifier),
@ -40,42 +38,37 @@ export async function getQuestion(questionId: QuestionId): Promise<QuestionDTO>
return mapToQuestionDTO(question);
}
export async function getAnswersByQuestion(questionId: QuestionId, full: boolean): Promise<AnswerDTO[] | AnswerId[]> {
const answerRepository = getAnswerRepository();
const question = await fetchQuestion(questionId);
const answers: Answer[] = await answerRepository.findAllAnswersToQuestion(question);
if (full) {
return answers.map(mapToAnswerDTO);
}
return answers.map(mapToAnswerDTOId);
}
export async function createQuestion(questionDTO: QuestionDTO): Promise<QuestionDTO | null> {
export async function createQuestion(loId: LearningObjectIdentifier, questionData: QuestionData): Promise<QuestionDTO> {
const questionRepository = getQuestionRepository();
const author = await fetchStudent(questionDTO.author);
const author = await fetchStudent(questionData.author!);
const content = questionData.content;
await questionRepository.createQuestion({
loId: mapToLearningObjectID(questionDTO.learningObjectIdentifier),
author,
content: questionDTO.content,
const question = await questionRepository.createQuestion({
loId, author, content
});
return questionDTO;
return mapToQuestionDTO(question);
}
export async function deleteQuestion(questionId: QuestionId): Promise<QuestionDTO | null> {
export async function deleteQuestion(questionId: QuestionId): Promise<QuestionDTO> {
const questionRepository = getQuestionRepository();
const question = await fetchQuestion(questionId);
const question = await fetchQuestion(questionId); // throws error if not found
const loId: LearningObjectIdentifier = {
...questionId.learningObjectIdentifier,
version: questionId.learningObjectIdentifier.version ?? 1,
version: questionId.learningObjectIdentifier.version ?? FALLBACK_VERSION_NUM,
};
await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(loId, questionId.sequenceNumber);
return mapToQuestionDTO(question);
}
export async function updateQuestion(questionId: QuestionId, questionData: QuestionData): Promise<QuestionDTO> {
const questionRepository = getQuestionRepository();
const question = await fetchQuestion(questionId);
const newQuestion = await questionRepository.updateContent(question, questionData.content);
return mapToQuestionDTO(newQuestion);
}