Merge remote-tracking branch 'origin/feat/question-routes' into feat/question-routes
This commit is contained in:
		
						commit
						24105004fc
					
				
					 12 changed files with 104 additions and 121 deletions
				
			
		|  | @ -2,7 +2,7 @@ import { DwengoEntityRepository } from '../dwengo-entity-repository.js'; | ||||||
| import { Answer } from '../../entities/questions/answer.entity.js'; | import { Answer } from '../../entities/questions/answer.entity.js'; | ||||||
| import { Question } from '../../entities/questions/question.entity.js'; | import { Question } from '../../entities/questions/question.entity.js'; | ||||||
| import { Teacher } from '../../entities/users/teacher.entity.js'; | import { Teacher } from '../../entities/users/teacher.entity.js'; | ||||||
| import {Loaded} from "@mikro-orm/core"; | import { Loaded } from '@mikro-orm/core'; | ||||||
| 
 | 
 | ||||||
| export class AnswerRepository extends DwengoEntityRepository<Answer> { | export class AnswerRepository extends DwengoEntityRepository<Answer> { | ||||||
|     public async createAnswer(answer: { toQuestion: Question; author: Teacher; content: string }): Promise<Answer> { |     public async createAnswer(answer: { toQuestion: Question; author: Teacher; content: string }): Promise<Answer> { | ||||||
|  | @ -22,7 +22,8 @@ export class AnswerRepository extends DwengoEntityRepository<Answer> { | ||||||
|     } |     } | ||||||
|     public async findAnswer(question: Question, sequenceNumber: number): Promise<Loaded<Answer> | null> { |     public async findAnswer(question: Question, sequenceNumber: number): Promise<Loaded<Answer> | null> { | ||||||
|         return this.findOne({ |         return this.findOne({ | ||||||
|             toQuestion: question, sequenceNumber |             toQuestion: question, | ||||||
|  |             sequenceNumber, | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|     public async removeAnswerByQuestionAndSequenceNumber(question: Question, sequenceNumber: number): Promise<void> { |     public async removeAnswerByQuestionAndSequenceNumber(question: Question, sequenceNumber: number): Promise<void> { | ||||||
|  |  | ||||||
|  | @ -3,7 +3,7 @@ import { Question } from '../../entities/questions/question.entity.js'; | ||||||
| import { LearningObjectIdentifier } from '../../entities/content/learning-object-identifier.js'; | import { LearningObjectIdentifier } from '../../entities/content/learning-object-identifier.js'; | ||||||
| import { Student } from '../../entities/users/student.entity.js'; | import { Student } from '../../entities/users/student.entity.js'; | ||||||
| import { LearningObject } from '../../entities/content/learning-object.entity.js'; | import { LearningObject } from '../../entities/content/learning-object.entity.js'; | ||||||
| import {Loaded} from "@mikro-orm/core"; | import { Loaded } from '@mikro-orm/core'; | ||||||
| 
 | 
 | ||||||
| export class QuestionRepository extends DwengoEntityRepository<Question> { | export class QuestionRepository extends DwengoEntityRepository<Question> { | ||||||
|     public async createQuestion(question: { loId: LearningObjectIdentifier; author: Student; content: string }): Promise<Question> { |     public async createQuestion(question: { loId: LearningObjectIdentifier; author: Student; content: string }): Promise<Question> { | ||||||
|  | @ -68,7 +68,7 @@ export class QuestionRepository extends DwengoEntityRepository<Question> { | ||||||
|             learningObjectHruid: loId.hruid, |             learningObjectHruid: loId.hruid, | ||||||
|             learningObjectLanguage: loId.language, |             learningObjectLanguage: loId.language, | ||||||
|             learningObjectVersion: loId.version, |             learningObjectVersion: loId.version, | ||||||
|             sequenceNumber |             sequenceNumber, | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,19 +1,13 @@ | ||||||
| import express from "express"; | import express from 'express'; | ||||||
| import { | import { createAnswerHandler, deleteAnswerHandler, getAnswerHandler, getAllAnswersHandler, updateAnswerHandler } from '../controllers/answers'; | ||||||
|     createAnswerHandler, |  | ||||||
|     deleteAnswerHandler, |  | ||||||
|     getAnswerHandler, |  | ||||||
|     getAllAnswersHandler, |  | ||||||
|     updateAnswerHandler |  | ||||||
| } from "../controllers/answers"; |  | ||||||
| 
 | 
 | ||||||
| const router = express.Router({ mergeParams: true }); | const router = express.Router({ mergeParams: true }); | ||||||
| 
 | 
 | ||||||
| router.get('/', getAllAnswersHandler); | router.get('/', getAllAnswersHandler); | ||||||
| 
 | 
 | ||||||
| router.post('/', createAnswerHandler) | router.post('/', createAnswerHandler); | ||||||
| 
 | 
 | ||||||
| router.get('/:seqAnswer', getAnswerHandler) | router.get('/:seqAnswer', getAnswerHandler); | ||||||
| 
 | 
 | ||||||
| router.delete('/:seqAnswer', deleteAnswerHandler); | router.delete('/:seqAnswer', deleteAnswerHandler); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,11 +1,11 @@ | ||||||
| import {getAnswerRepository} from "../data/repositories"; | import { getAnswerRepository } from '../data/repositories'; | ||||||
| import {Answer} from "../entities/questions/answer.entity"; | import { Answer } from '../entities/questions/answer.entity'; | ||||||
| import {mapToAnswerDTO, mapToAnswerDTOId} from "../interfaces/answer"; | import { mapToAnswerDTO, mapToAnswerDTOId } from '../interfaces/answer'; | ||||||
| import {fetchTeacher} from "./teachers"; | import { fetchTeacher } from './teachers'; | ||||||
| import {fetchQuestion} from "./questions"; | import { fetchQuestion } from './questions'; | ||||||
| import {QuestionId} from "@dwengo-1/common/interfaces/question"; | import { QuestionId } from '@dwengo-1/common/interfaces/question'; | ||||||
| import {AnswerData, AnswerDTO, AnswerId} from "@dwengo-1/common/interfaces/answer"; | import { AnswerData, AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer'; | ||||||
| import {NotFoundException} from "../exceptions/not-found-exception"; | import { NotFoundException } from '../exceptions/not-found-exception'; | ||||||
| 
 | 
 | ||||||
| export async function getAnswersByQuestion(questionId: QuestionId, full: boolean): Promise<AnswerDTO[] | AnswerId[]> { | export async function getAnswersByQuestion(questionId: QuestionId, full: boolean): Promise<AnswerDTO[] | AnswerId[]> { | ||||||
|     const answerRepository = getAnswerRepository(); |     const answerRepository = getAnswerRepository(); | ||||||
|  | @ -27,7 +27,9 @@ export async function createAnswer(questionId: QuestionId, answerData: AnswerDat | ||||||
|     const content = answerData.content; |     const content = answerData.content; | ||||||
| 
 | 
 | ||||||
|     const answer = await answerRepository.createAnswer({ |     const answer = await answerRepository.createAnswer({ | ||||||
|         toQuestion, author, content |         toQuestion, | ||||||
|  |         author, | ||||||
|  |         content, | ||||||
|     }); |     }); | ||||||
|     return mapToAnswerDTO(answer); |     return mapToAnswerDTO(answer); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -59,8 +59,8 @@ export async function deleteQuestion(questionId: QuestionId): Promise<QuestionDT | ||||||
|     const loId: LearningObjectIdentifier = { |     const loId: LearningObjectIdentifier = { | ||||||
|         hruid: questionId.learningObjectIdentifier.hruid, |         hruid: questionId.learningObjectIdentifier.hruid, | ||||||
|         language: questionId.learningObjectIdentifier.language, |         language: questionId.learningObjectIdentifier.language, | ||||||
|         version: questionId.learningObjectIdentifier.version || FALLBACK_VERSION_NUM |         version: questionId.learningObjectIdentifier.version || FALLBACK_VERSION_NUM, | ||||||
|     } |     }; | ||||||
| 
 | 
 | ||||||
|     await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(loId, questionId.sequenceNumber); |     await questionRepository.removeQuestionByLearningObjectAndSequenceNumber(loId, questionId.sequenceNumber); | ||||||
|     return mapToQuestionDTO(question); |     return mapToQuestionDTO(question); | ||||||
|  |  | ||||||
|  | @ -1,10 +1,10 @@ | ||||||
| import {Request, Response} from "express"; | import { Request, Response } from 'express'; | ||||||
| import {beforeAll, beforeEach, describe, expect, it, Mock, vi} from "vitest"; | import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest'; | ||||||
| import {setupTestApp} from "../setup-tests"; | import { setupTestApp } from '../setup-tests'; | ||||||
| import {Language} from "@dwengo-1/common/util/language"; | import { Language } from '@dwengo-1/common/util/language'; | ||||||
| import {getAllAnswersHandler, getAnswerHandler, updateAnswerHandler} from "../../src/controllers/answers"; | import { getAllAnswersHandler, getAnswerHandler, updateAnswerHandler } from '../../src/controllers/answers'; | ||||||
| import {BadRequestException} from "../../src/exceptions/bad-request-exception"; | import { BadRequestException } from '../../src/exceptions/bad-request-exception'; | ||||||
| import {NotFoundException} from "../../src/exceptions/not-found-exception"; | import { NotFoundException } from '../../src/exceptions/not-found-exception'; | ||||||
| 
 | 
 | ||||||
| describe('Questions controllers', () => { | describe('Questions controllers', () => { | ||||||
|     let req: Partial<Request>; |     let req: Partial<Request>; | ||||||
|  | @ -56,9 +56,8 @@ describe('Questions controllers', () => { | ||||||
|             query: { lang: Language.English, full: 'true' }, |             query: { lang: Language.English, full: 'true' }, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         await expect( async () => getAnswerHandler(req as Request, res as Response)) |         await expect(async () => getAnswerHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||||
|             .rejects.toThrow(NotFoundException); |     }); | ||||||
|     }) |  | ||||||
| 
 | 
 | ||||||
|     it('Get answer no hruid given', async () => { |     it('Get answer no hruid given', async () => { | ||||||
|         req = { |         req = { | ||||||
|  | @ -66,16 +65,15 @@ describe('Questions controllers', () => { | ||||||
|             query: { lang: Language.English, full: 'true' }, |             query: { lang: Language.English, full: 'true' }, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         await expect( async () => getAnswerHandler(req as Request, res as Response)) |         await expect(async () => getAnswerHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException); | ||||||
|             .rejects.toThrow(BadRequestException); |     }); | ||||||
|     }) |  | ||||||
| 
 | 
 | ||||||
|     it('Update question', async () => { |     it('Update question', async () => { | ||||||
|         const newContent = "updated question"; |         const newContent = 'updated question'; | ||||||
|         req = { |         req = { | ||||||
|             params: { hruid: 'id05', version: '1', seq: '2', seqAnswer: '2' }, |             params: { hruid: 'id05', version: '1', seq: '2', seqAnswer: '2' }, | ||||||
|             query: { lang: Language.English }, |             query: { lang: Language.English }, | ||||||
|             body: { content: newContent } |             body: { content: newContent }, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         await updateAnswerHandler(req as Request, res as Response); |         await updateAnswerHandler(req as Request, res as Response); | ||||||
|  | @ -86,5 +84,4 @@ describe('Questions controllers', () => { | ||||||
|         // Console.log(result.question);
 |         // Console.log(result.question);
 | ||||||
|         expect(result.answer.content).to.eq(newContent); |         expect(result.answer.content).to.eq(newContent); | ||||||
|     }); |     }); | ||||||
| 
 |  | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | @ -1,14 +1,10 @@ | ||||||
| import {beforeAll, beforeEach, describe, expect, it, Mock, vi} from "vitest"; | import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest'; | ||||||
| import {Request, Response} from "express"; | import { Request, Response } from 'express'; | ||||||
| import {setupTestApp} from "../setup-tests"; | import { setupTestApp } from '../setup-tests'; | ||||||
| import { | import { getAllQuestionsHandler, getQuestionHandler, updateQuestionHandler } from '../../src/controllers/questions'; | ||||||
|     getAllQuestionsHandler, | import { Language } from '@dwengo-1/common/util/language'; | ||||||
|     getQuestionHandler, updateQuestionHandler | import { NotFoundException } from '../../src/exceptions/not-found-exception'; | ||||||
| } from "../../src/controllers/questions"; | import { BadRequestException } from '../../src/exceptions/bad-request-exception'; | ||||||
| import {Language} from "@dwengo-1/common/util/language"; |  | ||||||
| import {NotFoundException} from "../../src/exceptions/not-found-exception"; |  | ||||||
| import {BadRequestException} from "../../src/exceptions/bad-request-exception"; |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| describe('Questions controllers', () => { | describe('Questions controllers', () => { | ||||||
|     let req: Partial<Request>; |     let req: Partial<Request>; | ||||||
|  | @ -52,7 +48,7 @@ describe('Questions controllers', () => { | ||||||
| 
 | 
 | ||||||
|         // Const result = jsonMock.mock.lastCall?.[0];
 |         // Const result = jsonMock.mock.lastCall?.[0];
 | ||||||
|         // Console.log(result.question);
 |         // Console.log(result.question);
 | ||||||
|     }) |     }); | ||||||
| 
 | 
 | ||||||
|     it('Get question with fallback sequence number and version', async () => { |     it('Get question with fallback sequence number and version', async () => { | ||||||
|         req = { |         req = { | ||||||
|  | @ -65,7 +61,7 @@ describe('Questions controllers', () => { | ||||||
| 
 | 
 | ||||||
|         // Const result = jsonMock.mock.lastCall?.[0];
 |         // Const result = jsonMock.mock.lastCall?.[0];
 | ||||||
|         // Console.log(result.question);
 |         // Console.log(result.question);
 | ||||||
|     }) |     }); | ||||||
| 
 | 
 | ||||||
|     it('Get question hruid does not exist', async () => { |     it('Get question hruid does not exist', async () => { | ||||||
|         req = { |         req = { | ||||||
|  | @ -73,9 +69,8 @@ describe('Questions controllers', () => { | ||||||
|             query: { lang: Language.English, full: 'true' }, |             query: { lang: Language.English, full: 'true' }, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         await expect( async () => getQuestionHandler(req as Request, res as Response)) |         await expect(async () => getQuestionHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||||
|             .rejects.toThrow(NotFoundException); |     }); | ||||||
|     }) |  | ||||||
| 
 | 
 | ||||||
|     it('Get question no hruid given', async () => { |     it('Get question no hruid given', async () => { | ||||||
|         req = { |         req = { | ||||||
|  | @ -83,9 +78,8 @@ describe('Questions controllers', () => { | ||||||
|             query: { lang: Language.English, full: 'true' }, |             query: { lang: Language.English, full: 'true' }, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         await expect( async () => getQuestionHandler(req as Request, res as Response)) |         await expect(async () => getQuestionHandler(req as Request, res as Response)).rejects.toThrow(BadRequestException); | ||||||
|             .rejects.toThrow(BadRequestException); |     }); | ||||||
|     }) |  | ||||||
| 
 | 
 | ||||||
|     /* |     /* | ||||||
|     It('Create and delete question', async() => { |     It('Create and delete question', async() => { | ||||||
|  | @ -105,11 +99,11 @@ describe('Questions controllers', () => { | ||||||
|      */ |      */ | ||||||
| 
 | 
 | ||||||
|     it('Update question', async () => { |     it('Update question', async () => { | ||||||
|         const newContent = "updated question"; |         const newContent = 'updated question'; | ||||||
|         req = { |         req = { | ||||||
|             params: { hruid: 'id05', version: '1', seq: '1' }, |             params: { hruid: 'id05', version: '1', seq: '1' }, | ||||||
|             query: { lang: Language.English }, |             query: { lang: Language.English }, | ||||||
|             body: { content: newContent } |             body: { content: newContent }, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         await updateQuestionHandler(req as Request, res as Response); |         await updateQuestionHandler(req as Request, res as Response); | ||||||
|  |  | ||||||
|  | @ -2,20 +2,19 @@ import type {AnswerData, AnswerDTO, AnswerId} from "@dwengo-1/common/interfaces/ | ||||||
| import { BaseController } from "@/controllers/base-controller.ts"; | import { BaseController } from "@/controllers/base-controller.ts"; | ||||||
| import type { QuestionId } from "@dwengo-1/common/interfaces/question"; | import type { QuestionId } from "@dwengo-1/common/interfaces/question"; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| export interface AnswersResponse { | export interface AnswersResponse { | ||||||
|     answers: AnswerDTO[] | AnswerId[] |     answers: AnswerDTO[] | AnswerId[]; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface AnswerResponse { | export interface AnswerResponse { | ||||||
|     answer: AnswerDTO |     answer: AnswerDTO; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export class AnswerController extends BaseController { | export class AnswerController extends BaseController { | ||||||
|     constructor(questionId: QuestionId) { |     constructor(questionId: QuestionId) { | ||||||
|         this.loId = questionId.learningObjectIdentifier; |         this.loId = questionId.learningObjectIdentifier; | ||||||
|         this.sequenceNumber = questionId.sequenceNumber; |         this.sequenceNumber = questionId.sequenceNumber; | ||||||
|         super(`learningObject/${loId.hruid}/:${loId.version}/questions/${this.sequenceNumber}/answers`) |         super(`learningObject/${loId.hruid}/:${loId.version}/questions/${this.sequenceNumber}/answers`); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async getAll(full = true): Promise<AnswersResponse> { |     async getAll(full = true): Promise<AnswersResponse> { | ||||||
|  |  | ||||||
|  | @ -25,7 +25,7 @@ export class QuestionController extends BaseController { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async create(questionData: QuestionData): Promise<QuestionResponse> { |     async create(questionData: QuestionData): Promise<QuestionResponse> { | ||||||
|         return this.post<QuestionResponse>("/", questionData, {lang: this.loId.lang}) |         return this.post<QuestionResponse>("/", questionData, { lang: this.loId.lang }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async remove(sequenceNumber: number): Promise<QuestionResponse> { |     async remove(sequenceNumber: number): Promise<QuestionResponse> { | ||||||
|  |  | ||||||
|  | @ -1,11 +1,6 @@ | ||||||
| import type { QuestionId } from "@dwengo-1/common/dist/interfaces/question.ts"; | import type { QuestionId } from "@dwengo-1/common/dist/interfaces/question.ts"; | ||||||
| import { type MaybeRefOrGetter, toValue } from "vue"; | import { type MaybeRefOrGetter, toValue } from "vue"; | ||||||
| import { | import { useMutation, type UseMutationReturnType, useQuery, type UseQueryReturnType } from "@tanstack/vue-query"; | ||||||
|     useMutation, |  | ||||||
|     type UseMutationReturnType, |  | ||||||
|     useQuery, |  | ||||||
|     type UseQueryReturnType |  | ||||||
| } from "@tanstack/vue-query"; |  | ||||||
| import { AnswerController, type AnswerResponse, type AnswersResponse } from "@/controllers/answers.ts"; | import { AnswerController, type AnswerResponse, type AnswersResponse } from "@/controllers/answers.ts"; | ||||||
| import type { AnswerData } from "@dwengo-1/common/dist/interfaces/answer.ts"; | import type { AnswerData } from "@dwengo-1/common/dist/interfaces/answer.ts"; | ||||||
| 
 | 
 | ||||||
|  | @ -23,7 +18,7 @@ export function useAnswersQuery( | ||||||
| 
 | 
 | ||||||
| export function useAnswerQuery( | export function useAnswerQuery( | ||||||
|     questionId: MaybeRefOrGetter<QuestionId>, |     questionId: MaybeRefOrGetter<QuestionId>, | ||||||
|     sequenceNumber: MaybeRefOrGetter<number> |     sequenceNumber: MaybeRefOrGetter<number>, | ||||||
| ): UseQueryReturnType<AnswerResponse, Error> { | ): UseQueryReturnType<AnswerResponse, Error> { | ||||||
|     return useQuery({ |     return useQuery({ | ||||||
|         queryFn: async () => new AnswerController(toValue(questionId)).getBy(toValue(sequenceNumber)), |         queryFn: async () => new AnswerController(toValue(questionId)).getBy(toValue(sequenceNumber)), | ||||||
|  | @ -49,9 +44,8 @@ export function useDeleteAnswerMutation( | ||||||
| 
 | 
 | ||||||
| export function useUpdateAnswerMutation( | export function useUpdateAnswerMutation( | ||||||
|     questionId: MaybeRefOrGetter<QuestionId>, |     questionId: MaybeRefOrGetter<QuestionId>, | ||||||
| ): UseMutationReturnType<AnswerResponse, Error, { answerData: AnswerData, seq: number }, unknown> { | ): UseMutationReturnType<AnswerResponse, Error, { answerData: AnswerData; seq: number }, unknown> { | ||||||
|     return useMutation({ |     return useMutation({ | ||||||
|         mutationFn: async (data, seq) => new AnswerController(toValue(questionId)).update(seq, data), |         mutationFn: async (data, seq) => new AnswerController(toValue(questionId)).update(seq, data), | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 |  | ||||||
|  |  | ||||||
|  | @ -7,11 +7,13 @@ import { | ||||||
|     type UseMutationReturnType, |     type UseMutationReturnType, | ||||||
|     useQuery, |     useQuery, | ||||||
|     useQueryClient, |     useQueryClient, | ||||||
|     type UseQueryReturnType |     type UseQueryReturnType, | ||||||
| } from "@tanstack/vue-query"; | } from "@tanstack/vue-query"; | ||||||
| 
 | 
 | ||||||
| 
 | export function questionsQueryKey( | ||||||
| export function questionsQueryKey(loId: LearningObjectIdentifierDTO, full: boolean): [string, string, number, string, boolean] { |     loId: LearningObjectIdentifierDTO, | ||||||
|  |     full: boolean, | ||||||
|  | ): [string, string, number, string, boolean] { | ||||||
|     return ["questions", loId.hruid, loId.version, loId.language, full]; |     return ["questions", loId.hruid, loId.version, loId.language, full]; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl