feat: answer routes en put question
This commit is contained in:
parent
6a1adb0ee3
commit
e8e2466b76
12 changed files with 288 additions and 62 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue