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,101 @@
import {Request, Response} from "express";
import {requireFields} from "./error-helper";
import {getLearningObjectId, getQuestionId} from "./questions";
import {createAnswer, deleteAnswer, getAnswer, getAnswersByQuestion, updateAnswer} from "../services/answers";
import {FALLBACK_SEQ_NUM} from "../config";
import {AnswerData} from "@dwengo-1/common/interfaces/answer";
export async function getAnswersHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
const full = req.query.full === 'true';
requireFields({ hruid })
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const answers = await getAnswersByQuestion(questionId, full);
res.json({ answers });
}
export async function getAnswerHandler(req: Request, res: Response): Promise<void>{
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
const seqAnswer = req.params.seqAnswer;
requireFields({ hruid })
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const sequenceNumber = Number(seqAnswer) || FALLBACK_SEQ_NUM;
const answer = await getAnswer(questionId, sequenceNumber);
res.json({ answer });
}
export async function createAnswerHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
requireFields({ hruid })
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const author = req.body.author as string;
const content = req.body.content as string;
requireFields({ author, content });
const answerData = req.body as AnswerData;
const answer = await createAnswer(questionId, answerData);
res.json({ answer });
}
export async function deleteAnswerHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
const seqAnswer = req.params.seqAnswer;
requireFields({ hruid })
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const sequenceNumber = Number(seqAnswer) || FALLBACK_SEQ_NUM;
const answer = await deleteAnswer(questionId, sequenceNumber);
res.json({ answer });
}
export async function updateAnswerHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
const seqAnswer = req.params.seqAnswer;
requireFields({ hruid })
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const content = req.body.content as string;
requireFields({ content });
const answerData = req.body as AnswerData;
const sequenceNumber = Number(seqAnswer) || FALLBACK_SEQ_NUM;
const answer = await updateAnswer(questionId, sequenceNumber, answerData);
res.json({ answer });
}

View file

@ -1,12 +1,17 @@
import { Request, Response } from 'express';
import { createQuestion, deleteQuestion, getAllQuestions, getAnswersByQuestion, getQuestion } from '../services/questions.js';
import {
createQuestion,
deleteQuestion,
getAllQuestions,
getQuestion, updateQuestion
} from '../services/questions.js';
import {FALLBACK_LANG, FALLBACK_SEQ_NUM, FALLBACK_VERSION_NUM} from '../config.js';
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import {QuestionData, QuestionId} from '@dwengo-1/common/interfaces/question';
import { Language } from '@dwengo-1/common/util/language';
import {requireFields} from "./error-helper";
function getLearningObjectId(hruid: string, version: string, lang: string): LearningObjectIdentifier {
export function getLearningObjectId(hruid: string, version: string, lang: string): LearningObjectIdentifier {
return {
hruid,
language: (lang || FALLBACK_LANG) as Language,
@ -14,7 +19,7 @@ function getLearningObjectId(hruid: string, version: string, lang: string): Lear
};
}
function getQuestionId(learningObjectIdentifier: LearningObjectIdentifier, seq: string): QuestionId {
export function getQuestionId(learningObjectIdentifier: LearningObjectIdentifier, seq: string): QuestionId {
return {
learningObjectIdentifier,
sequenceNumber: seq ? Number(seq) : FALLBACK_SEQ_NUM,
@ -51,31 +56,21 @@ export async function getQuestionHandler(req: Request, res: Response): Promise<v
}
export async function getQuestionAnswersHandler(req: Request, res: Response): Promise<void> {
export async function createQuestionHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
const full = req.query.full === 'true';
requireFields({ hruid })
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const loId = getLearningObjectId(hruid, version, language);
const answers = await getAnswersByQuestion(questionId, full);
const author = req.body.author as string;
const content = req.body.content as string;
requireFields({ author, content });
res.json({ answers });
}
const questionData = req.body as QuestionData;
export async function createQuestionHandler(req: Request, res: Response): Promise<void> {
const learningObjectIdentifier = req.body.learningObjectIdentifier;
const author = req.body.author;
const content = req.body.content;
requireFields({ learningObjectIdentifier, author, content });
const questionDTO = req.body as QuestionDTO;
const question = await createQuestion(questionDTO);
const question = await createQuestion(loId, questionData);
res.json({ question });
@ -95,3 +90,23 @@ export async function deleteQuestionHandler(req: Request, res: Response): Promis
res.json({ question });
}
export async function updateQuestionHandler(req: Request, res: Response): Promise<void> {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
const seq = req.params.seq;
requireFields({ hruid })
const learningObjectId = getLearningObjectId(hruid, version, language);
const questionId = getQuestionId(learningObjectId, seq);
const content = req.body.content as string;
requireFields({ content });
const questionData = req.body as QuestionData;
const question = await updateQuestion(questionId, questionData);
res.json({ question });
}

View file

@ -19,10 +19,20 @@ export class AnswerRepository extends DwengoEntityRepository<Answer> {
orderBy: { sequenceNumber: 'ASC' },
});
}
public async findAnswer(question: Question, sequenceNumber: number) {
return this.findOne({
toQuestion: question, sequenceNumber
});
}
public async removeAnswerByQuestionAndSequenceNumber(question: Question, sequenceNumber: number): Promise<void> {
return this.deleteWhere({
toQuestion: question,
sequenceNumber: sequenceNumber,
});
}
public async updateContent(answer: Answer, newContent: string){
answer.content = newContent;
await this.save(answer);
return answer;
}
}

View file

@ -70,4 +70,10 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
sequenceNumber
});
}
public async updateContent(question: Question, newContent: string): Promise<Question> {
question.content = newContent;
await this.save(question);
return question;
}
}

View file

@ -1,14 +1,14 @@
import { mapToUserDTO } from './user.js';
import { mapToQuestionDTO, mapToQuestionDTOId } from './question.js';
import {mapToQuestionDTO, mapToQuestionDTOId} from './question.js';
import { Answer } from '../entities/questions/answer.entity.js';
import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
import {mapToTeacherDTO} from "./teacher";
/**
* Convert a Question entity to a DTO format.
*/
export function mapToAnswerDTO(answer: Answer): AnswerDTO {
return {
author: mapToUserDTO(answer.author),
author: mapToTeacherDTO(answer.author),
toQuestion: mapToQuestionDTO(answer.toQuestion),
sequenceNumber: answer.sequenceNumber!,
timestamp: answer.timestamp.toISOString(),

View file

@ -1,6 +1,6 @@
import { Question } from '../entities/questions/question.entity.js';
import { mapToStudentDTO } from './student.js';
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
import { mapToStudentDTO} from './student.js';
import { QuestionDTO, QuestionId} from '@dwengo-1/common/interfaces/question';
import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content';
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';

View file

@ -0,0 +1,22 @@
import express from "express";
import {
createAnswerHandler,
deleteAnswerHandler,
getAnswerHandler,
getAnswersHandler,
updateAnswerHandler
} from "../controllers/answers";
const router = express.Router({ mergeParams: true });
router.get('/', getAnswersHandler);
router.post('/', createAnswerHandler)
router.get('/:seqAnswer', getAnswerHandler)
router.delete('/:seqAnswer', deleteAnswerHandler);
router.put('/:seqAnswer', updateAnswerHandler);
export default router;

View file

@ -3,9 +3,10 @@ import {
createQuestionHandler,
deleteQuestionHandler,
getAllQuestionsHandler,
getQuestionAnswersHandler,
getQuestionHandler,
} from '../controllers/questions.js';
import answerRoutes from './answers.js';
const router = express.Router({ mergeParams: true });
// Query language
@ -20,6 +21,6 @@ router.delete('/:seq', deleteQuestionHandler);
// Information about a question with id
router.get('/:seq', getQuestionHandler);
router.get('/answers/:seq', getQuestionAnswersHandler);
router.use('/:seq/answer', answerRoutes);
export default router;

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);
}

View file

@ -1,14 +1,19 @@
import { UserDTO } from './user';
import { QuestionDTO, QuestionId } from './question';
import {TeacherDTO} from "./teacher";
export interface AnswerDTO {
author: UserDTO;
author: TeacherDTO;
toQuestion: QuestionDTO;
sequenceNumber: number;
timestamp: string;
content: string;
}
export interface AnswerData {
author: string;
content: string;
}
export interface AnswerId {
author: string;
toQuestion: QuestionId;

View file

@ -4,8 +4,13 @@ import { StudentDTO } from './student';
export interface QuestionDTO {
learningObjectIdentifier: LearningObjectIdentifierDTO;
sequenceNumber?: number;
author: string;
timestamp?: string;
author: StudentDTO;
timestamp: string;
content: string;
}
export interface QuestionData {
author?: string;
content: string;
}