From e8e2466b7608d61154cca33b6f0db199ba514d32 Mon Sep 17 00:00:00 2001 From: Gabriellvl Date: Sun, 6 Apr 2025 23:37:54 +0200 Subject: [PATCH] feat: answer routes en put question --- backend/src/controllers/answers.ts | 101 ++++++++++++++++++ backend/src/controllers/questions.ts | 57 ++++++---- .../src/data/questions/answer-repository.ts | 10 ++ .../src/data/questions/question-repository.ts | 6 ++ backend/src/interfaces/answer.ts | 6 +- backend/src/interfaces/question.ts | 4 +- backend/src/routes/answers.ts | 22 ++++ backend/src/routes/questions.ts | 5 +- backend/src/services/answers.ts | 68 ++++++++++++ backend/src/services/questions.ts | 53 ++++----- common/src/interfaces/answer.ts | 9 +- common/src/interfaces/question.ts | 9 +- 12 files changed, 288 insertions(+), 62 deletions(-) create mode 100644 backend/src/controllers/answers.ts create mode 100644 backend/src/routes/answers.ts create mode 100644 backend/src/services/answers.ts diff --git a/backend/src/controllers/answers.ts b/backend/src/controllers/answers.ts new file mode 100644 index 00000000..1bb53019 --- /dev/null +++ b/backend/src/controllers/answers.ts @@ -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 { + 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{ + 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 { + 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 { + 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 { + 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 }); +} diff --git a/backend/src/controllers/questions.ts b/backend/src/controllers/questions.ts index a4a6a76d..e239d9ca 100644 --- a/backend/src/controllers/questions.ts +++ b/backend/src/controllers/questions.ts @@ -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 { +export async function createQuestionHandler(req: Request, res: Response): Promise { 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 { - 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 { + 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 }); +} diff --git a/backend/src/data/questions/answer-repository.ts b/backend/src/data/questions/answer-repository.ts index a50bfd28..94798248 100644 --- a/backend/src/data/questions/answer-repository.ts +++ b/backend/src/data/questions/answer-repository.ts @@ -19,10 +19,20 @@ export class AnswerRepository extends DwengoEntityRepository { orderBy: { sequenceNumber: 'ASC' }, }); } + public async findAnswer(question: Question, sequenceNumber: number) { + return this.findOne({ + toQuestion: question, sequenceNumber + }); + } public async removeAnswerByQuestionAndSequenceNumber(question: Question, sequenceNumber: number): Promise { return this.deleteWhere({ toQuestion: question, sequenceNumber: sequenceNumber, }); } + public async updateContent(answer: Answer, newContent: string){ + answer.content = newContent; + await this.save(answer); + return answer; + } } diff --git a/backend/src/data/questions/question-repository.ts b/backend/src/data/questions/question-repository.ts index 90c144d8..31a856a6 100644 --- a/backend/src/data/questions/question-repository.ts +++ b/backend/src/data/questions/question-repository.ts @@ -70,4 +70,10 @@ export class QuestionRepository extends DwengoEntityRepository { sequenceNumber }); } + + public async updateContent(question: Question, newContent: string): Promise { + question.content = newContent; + await this.save(question); + return question; + } } diff --git a/backend/src/interfaces/answer.ts b/backend/src/interfaces/answer.ts index 1f0d0625..87464a0b 100644 --- a/backend/src/interfaces/answer.ts +++ b/backend/src/interfaces/answer.ts @@ -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(), diff --git a/backend/src/interfaces/question.ts b/backend/src/interfaces/question.ts index 9005654f..46f61825 100644 --- a/backend/src/interfaces/question.ts +++ b/backend/src/interfaces/question.ts @@ -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'; diff --git a/backend/src/routes/answers.ts b/backend/src/routes/answers.ts new file mode 100644 index 00000000..edb27813 --- /dev/null +++ b/backend/src/routes/answers.ts @@ -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; diff --git a/backend/src/routes/questions.ts b/backend/src/routes/questions.ts index 31a71f3b..ba39435c 100644 --- a/backend/src/routes/questions.ts +++ b/backend/src/routes/questions.ts @@ -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; diff --git a/backend/src/services/answers.ts b/backend/src/services/answers.ts new file mode 100644 index 00000000..27b9cb13 --- /dev/null +++ b/backend/src/services/answers.ts @@ -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 { + 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 { + 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 { + 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 { + const answer = await fetchAnswer(questionId, sequenceNumber); + return mapToAnswerDTO(answer); +} + +export async function deleteAnswer(questionId: QuestionId, sequenceNumber: number): Promise { + 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); +} diff --git a/backend/src/services/questions.ts b/backend/src/services/questions.ts index be361085..2e26c960 100644 --- a/backend/src/services/questions.ts +++ b/backend/src/services/questions.ts @@ -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 { @@ -21,7 +19,7 @@ export async function getAllQuestions(id: LearningObjectIdentifier, full: boolea return questions.map(mapToQuestionDTOId); } -async function fetchQuestion(questionId: QuestionId): Promise { +export async function fetchQuestion(questionId: QuestionId): Promise { const questionRepository = getQuestionRepository(); const question = await questionRepository.findByLearningObjectAndSequenceNumber( mapToLearningObjectID(questionId.learningObjectIdentifier), @@ -40,42 +38,37 @@ export async function getQuestion(questionId: QuestionId): Promise return mapToQuestionDTO(question); } -export async function getAnswersByQuestion(questionId: QuestionId, full: boolean): Promise { - 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 { +export async function createQuestion(loId: LearningObjectIdentifier, questionData: QuestionData): Promise { 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 { +export async function deleteQuestion(questionId: QuestionId): Promise { 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 { + const questionRepository = getQuestionRepository(); + const question = await fetchQuestion(questionId); + + const newQuestion = await questionRepository.updateContent(question, questionData.content); + return mapToQuestionDTO(newQuestion); +} + + diff --git a/common/src/interfaces/answer.ts b/common/src/interfaces/answer.ts index e9280f8a..efba9437 100644 --- a/common/src/interfaces/answer.ts +++ b/common/src/interfaces/answer.ts @@ -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; diff --git a/common/src/interfaces/question.ts b/common/src/interfaces/question.ts index 88e9c7b8..9866fbef 100644 --- a/common/src/interfaces/question.ts +++ b/common/src/interfaces/question.ts @@ -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; }