Merge remote-tracking branch 'origin/feat/question-routes' into feat/question-routes
# Conflicts: # backend/src/data/questions/answer-repository.ts # backend/src/data/questions/question-repository.ts # backend/src/routes/answers.ts # backend/src/services/answers.ts
This commit is contained in:
		
						commit
						7f7a4fe936
					
				
					 7 changed files with 38 additions and 53 deletions
				
			
		|  | @ -1,9 +1,9 @@ | ||||||
| import {Request, Response} from "express"; | import { Request, Response } from 'express'; | ||||||
| import {requireFields} from "./error-helper"; | import { requireFields } from './error-helper'; | ||||||
| import {getLearningObjectId, getQuestionId} from "./questions"; | import { getLearningObjectId, getQuestionId } from './questions'; | ||||||
| import {createAnswer, deleteAnswer, getAnswer, getAnswersByQuestion, updateAnswer} from "../services/answers"; | import { createAnswer, deleteAnswer, getAnswer, getAnswersByQuestion, updateAnswer } from '../services/answers'; | ||||||
| import {FALLBACK_SEQ_NUM} from "../config"; | import { FALLBACK_SEQ_NUM } from '../config'; | ||||||
| import {AnswerData} from "@dwengo-1/common/interfaces/answer"; | import { AnswerData } from '@dwengo-1/common/interfaces/answer'; | ||||||
| 
 | 
 | ||||||
| export async function getAllAnswersHandler(req: Request, res: Response): Promise<void> { | export async function getAllAnswersHandler(req: Request, res: Response): Promise<void> { | ||||||
|     const hruid = req.params.hruid; |     const hruid = req.params.hruid; | ||||||
|  | @ -11,7 +11,7 @@ export async function getAllAnswersHandler(req: Request, res: Response): Promise | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     const seq = req.params.seq; |     const seq = req.params.seq; | ||||||
|     const full = req.query.full === 'true'; |     const full = req.query.full === 'true'; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const learningObjectId = getLearningObjectId(hruid, version, language); |     const learningObjectId = getLearningObjectId(hruid, version, language); | ||||||
|     const questionId = getQuestionId(learningObjectId, seq); |     const questionId = getQuestionId(learningObjectId, seq); | ||||||
|  | @ -27,7 +27,7 @@ export async function getAnswerHandler(req: Request, res: Response): Promise<voi | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     const seq = req.params.seq; |     const seq = req.params.seq; | ||||||
|     const seqAnswer = req.params.seqAnswer; |     const seqAnswer = req.params.seqAnswer; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const learningObjectId = getLearningObjectId(hruid, version, language); |     const learningObjectId = getLearningObjectId(hruid, version, language); | ||||||
|     const questionId = getQuestionId(learningObjectId, seq); |     const questionId = getQuestionId(learningObjectId, seq); | ||||||
|  | @ -43,7 +43,7 @@ export async function createAnswerHandler(req: Request, res: Response): Promise< | ||||||
|     const version = req.params.version; |     const version = req.params.version; | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     const seq = req.params.seq; |     const seq = req.params.seq; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const learningObjectId = getLearningObjectId(hruid, version, language); |     const learningObjectId = getLearningObjectId(hruid, version, language); | ||||||
|     const questionId = getQuestionId(learningObjectId, seq); |     const questionId = getQuestionId(learningObjectId, seq); | ||||||
|  | @ -65,7 +65,7 @@ export async function deleteAnswerHandler(req: Request, res: Response): Promise< | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     const seq = req.params.seq; |     const seq = req.params.seq; | ||||||
|     const seqAnswer = req.params.seqAnswer; |     const seqAnswer = req.params.seqAnswer; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const learningObjectId = getLearningObjectId(hruid, version, language); |     const learningObjectId = getLearningObjectId(hruid, version, language); | ||||||
|     const questionId = getQuestionId(learningObjectId, seq); |     const questionId = getQuestionId(learningObjectId, seq); | ||||||
|  | @ -76,15 +76,13 @@ export async function deleteAnswerHandler(req: Request, res: Response): Promise< | ||||||
|     res.json({ answer }); |     res.json({ answer }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| export async function updateAnswerHandler(req: Request, res: Response): Promise<void> { | export async function updateAnswerHandler(req: Request, res: Response): Promise<void> { | ||||||
|     const hruid = req.params.hruid; |     const hruid = req.params.hruid; | ||||||
|     const version = req.params.version; |     const version = req.params.version; | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     const seq = req.params.seq; |     const seq = req.params.seq; | ||||||
|     const seqAnswer = req.params.seqAnswer; |     const seqAnswer = req.params.seqAnswer; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const learningObjectId = getLearningObjectId(hruid, version, language); |     const learningObjectId = getLearningObjectId(hruid, version, language); | ||||||
|     const questionId = getQuestionId(learningObjectId, seq); |     const questionId = getQuestionId(learningObjectId, seq); | ||||||
|  |  | ||||||
|  | @ -1,15 +1,10 @@ | ||||||
| import { Request, Response } from 'express'; | import { Request, Response } from 'express'; | ||||||
| import { | import { createQuestion, deleteQuestion, getAllQuestions, getQuestion, updateQuestion } from '../services/questions.js'; | ||||||
|     createQuestion, |  | ||||||
|     deleteQuestion, |  | ||||||
|     getAllQuestions, |  | ||||||
|     getQuestion, updateQuestion |  | ||||||
| } from '../services/questions.js'; |  | ||||||
| import { FALLBACK_LANG, FALLBACK_SEQ_NUM, FALLBACK_VERSION_NUM } from '../config.js'; | import { FALLBACK_LANG, FALLBACK_SEQ_NUM, FALLBACK_VERSION_NUM } from '../config.js'; | ||||||
| import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | ||||||
| import { QuestionData, 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 { Language } from '@dwengo-1/common/util/language'; | ||||||
| import {requireFields} from "./error-helper"; | import { requireFields } from './error-helper'; | ||||||
| 
 | 
 | ||||||
| export function getLearningObjectId(hruid: string, version: string, lang: string): LearningObjectIdentifier { | export function getLearningObjectId(hruid: string, version: string, lang: string): LearningObjectIdentifier { | ||||||
|     return { |     return { | ||||||
|  | @ -31,7 +26,7 @@ export async function getAllQuestionsHandler(req: Request, res: Response): Promi | ||||||
|     const version = req.params.version; |     const version = req.params.version; | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     const full = req.query.full === 'true'; |     const full = req.query.full === 'true'; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const learningObjectId = getLearningObjectId(hruid, version, language); |     const learningObjectId = getLearningObjectId(hruid, version, language); | ||||||
| 
 | 
 | ||||||
|  | @ -45,7 +40,7 @@ export async function getQuestionHandler(req: Request, res: Response): Promise<v | ||||||
|     const version = req.params.version; |     const version = req.params.version; | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     const seq = req.params.seq; |     const seq = req.params.seq; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const learningObjectId = getLearningObjectId(hruid, version, language); |     const learningObjectId = getLearningObjectId(hruid, version, language); | ||||||
|     const questionId = getQuestionId(learningObjectId, seq); |     const questionId = getQuestionId(learningObjectId, seq); | ||||||
|  | @ -53,14 +48,13 @@ export async function getQuestionHandler(req: Request, res: Response): Promise<v | ||||||
|     const question = await getQuestion(questionId); |     const question = await getQuestion(questionId); | ||||||
| 
 | 
 | ||||||
|     res.json({ question }); |     res.json({ question }); | ||||||
| 
 |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function createQuestionHandler(req: Request, res: Response): Promise<void> { | export async function createQuestionHandler(req: Request, res: Response): Promise<void> { | ||||||
|     const hruid = req.params.hruid; |     const hruid = req.params.hruid; | ||||||
|     const version = req.params.version; |     const version = req.params.version; | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const loId = getLearningObjectId(hruid, version, language); |     const loId = getLearningObjectId(hruid, version, language); | ||||||
| 
 | 
 | ||||||
|  | @ -73,7 +67,6 @@ export async function createQuestionHandler(req: Request, res: Response): Promis | ||||||
|     const question = await createQuestion(loId, questionData); |     const question = await createQuestion(loId, questionData); | ||||||
| 
 | 
 | ||||||
|     res.json({ question }); |     res.json({ question }); | ||||||
| 
 |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function deleteQuestionHandler(req: Request, res: Response): Promise<void> { | export async function deleteQuestionHandler(req: Request, res: Response): Promise<void> { | ||||||
|  | @ -81,7 +74,7 @@ export async function deleteQuestionHandler(req: Request, res: Response): Promis | ||||||
|     const version = req.params.version; |     const version = req.params.version; | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     const seq = req.params.seq; |     const seq = req.params.seq; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const learningObjectId = getLearningObjectId(hruid, version, language); |     const learningObjectId = getLearningObjectId(hruid, version, language); | ||||||
|     const questionId = getQuestionId(learningObjectId, seq); |     const questionId = getQuestionId(learningObjectId, seq); | ||||||
|  | @ -96,7 +89,7 @@ export async function updateQuestionHandler(req: Request, res: Response): Promis | ||||||
|     const version = req.params.version; |     const version = req.params.version; | ||||||
|     const language = req.query.lang as string; |     const language = req.query.lang as string; | ||||||
|     const seq = req.params.seq; |     const seq = req.params.seq; | ||||||
|     requireFields({ hruid }) |     requireFields({ hruid }); | ||||||
| 
 | 
 | ||||||
|     const learningObjectId = getLearningObjectId(hruid, version, language); |     const learningObjectId = getLearningObjectId(hruid, version, language); | ||||||
|     const questionId = getQuestionId(learningObjectId, seq); |     const questionId = getQuestionId(learningObjectId, seq); | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| import { mapToQuestionDTO, mapToQuestionDTOId } from './question.js'; | import { mapToQuestionDTO, mapToQuestionDTOId } from './question.js'; | ||||||
| import { Answer } from '../entities/questions/answer.entity.js'; | import { Answer } from '../entities/questions/answer.entity.js'; | ||||||
| import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer'; | import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer'; | ||||||
| import {mapToTeacherDTO} from "./teacher"; | import { mapToTeacherDTO } from './teacher'; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Convert a Question entity to a DTO format. |  * Convert a Question entity to a DTO format. | ||||||
|  |  | ||||||
|  | @ -4,7 +4,6 @@ import { QuestionDTO, QuestionId} from '@dwengo-1/common/interfaces/question'; | ||||||
| import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content'; | import { LearningObjectIdentifierDTO } from '@dwengo-1/common/interfaces/learning-content'; | ||||||
| import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| function getLearningObjectIdentifier(question: Question): LearningObjectIdentifierDTO { | function getLearningObjectIdentifier(question: Question): LearningObjectIdentifierDTO { | ||||||
|     return { |     return { | ||||||
|         hruid: question.learningObjectHruid, |         hruid: question.learningObjectHruid, | ||||||
|  | @ -17,8 +16,8 @@ export function mapToLearningObjectID(loID: LearningObjectIdentifierDTO): Learni | ||||||
|     return { |     return { | ||||||
|         hruid: loID.hruid, |         hruid: loID.hruid, | ||||||
|         language: loID.language, |         language: loID.language, | ||||||
|         version: loID.version ?? 1 |         version: loID.version ?? 1, | ||||||
|     } |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  |  | ||||||
|  | @ -1,10 +1,5 @@ | ||||||
| import express from 'express'; | import express from 'express'; | ||||||
| import { | import { createQuestionHandler, deleteQuestionHandler, getAllQuestionsHandler, getQuestionHandler } from '../controllers/questions.js'; | ||||||
|     createQuestionHandler, |  | ||||||
|     deleteQuestionHandler, |  | ||||||
|     getAllQuestionsHandler, |  | ||||||
|     getQuestionHandler, |  | ||||||
| } from '../controllers/questions.js'; |  | ||||||
| import answerRoutes from './answers.js'; | import answerRoutes from './answers.js'; | ||||||
| 
 | 
 | ||||||
| const router = express.Router({ mergeParams: true }); | const router = express.Router({ mergeParams: true }); | ||||||
|  |  | ||||||
|  | @ -4,9 +4,9 @@ import { Question } from '../entities/questions/question.entity.js'; | ||||||
| import { QuestionRepository } from '../data/questions/question-repository.js'; | import { QuestionRepository } from '../data/questions/question-repository.js'; | ||||||
| import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | ||||||
| import { QuestionData, QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | import { QuestionData, QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | ||||||
| import {NotFoundException} from "../exceptions/not-found-exception"; | import { NotFoundException } from '../exceptions/not-found-exception'; | ||||||
| import {FALLBACK_VERSION_NUM} from "../config"; | import { FALLBACK_VERSION_NUM } from '../config'; | ||||||
| import {fetchStudent} from "./students"; | import { fetchStudent } from './students'; | ||||||
| 
 | 
 | ||||||
| export async function getAllQuestions(id: LearningObjectIdentifier, full: boolean): Promise<QuestionDTO[] | QuestionId[]> { | export async function getAllQuestions(id: LearningObjectIdentifier, full: boolean): Promise<QuestionDTO[] | QuestionId[]> { | ||||||
|     const questionRepository: QuestionRepository = getQuestionRepository(); |     const questionRepository: QuestionRepository = getQuestionRepository(); | ||||||
|  | @ -44,7 +44,9 @@ export async function createQuestion(loId: LearningObjectIdentifier, questionDat | ||||||
|     const content = questionData.content; |     const content = questionData.content; | ||||||
| 
 | 
 | ||||||
|     const question = await questionRepository.createQuestion({ |     const question = await questionRepository.createQuestion({ | ||||||
|         loId, author, content |         loId, | ||||||
|  |         author, | ||||||
|  |         content, | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     return mapToQuestionDTO(question); |     return mapToQuestionDTO(question); | ||||||
|  | @ -71,5 +73,3 @@ export async function updateQuestion(questionId: QuestionId, questionData: Quest | ||||||
|     const newQuestion = await questionRepository.updateContent(question, questionData.content); |     const newQuestion = await questionRepository.updateContent(question, questionData.content); | ||||||
|     return mapToQuestionDTO(newQuestion); |     return mapToQuestionDTO(newQuestion); | ||||||
| } | } | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| import { QuestionDTO, QuestionId } from './question'; | import { QuestionDTO, QuestionId } from './question'; | ||||||
| import {TeacherDTO} from "./teacher"; | import { TeacherDTO } from './teacher'; | ||||||
| 
 | 
 | ||||||
| export interface AnswerDTO { | export interface AnswerDTO { | ||||||
|     author: TeacherDTO; |     author: TeacherDTO; | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl