Merge remote-tracking branch 'origin/dev' into feat/endpoints-beschermen-met-authenticatie-#105
# Conflicts: # backend/src/controllers/questions.ts # backend/src/data/questions/question-repository.ts # backend/src/interfaces/question.ts # backend/src/services/questions.ts # common/src/interfaces/question.ts
This commit is contained in:
commit
8c387d6811
59 changed files with 2100 additions and 292 deletions
99
backend/src/controllers/answers.ts
Normal file
99
backend/src/controllers/answers.ts
Normal file
|
@ -0,0 +1,99 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { requireFields } from './error-helper.js';
|
||||
import { getLearningObjectId, getQuestionId } from './questions.js';
|
||||
import { createAnswer, deleteAnswer, getAnswer, getAnswersByQuestion, updateAnswer } from '../services/answers.js';
|
||||
import { FALLBACK_SEQ_NUM } from '../config.js';
|
||||
import { AnswerData } from '@dwengo-1/common/interfaces/answer';
|
||||
|
||||
export async function getAllAnswersHandler(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 });
|
||||
}
|
|
@ -28,7 +28,7 @@ export async function createClassHandler(req: Request, res: Response): Promise<v
|
|||
return;
|
||||
}
|
||||
|
||||
res.status(201).json(cls);
|
||||
res.status(201).json({ class: cls });
|
||||
}
|
||||
|
||||
export async function getClassHandler(req: Request, res: Response): Promise<void> {
|
||||
|
@ -40,7 +40,7 @@ export async function getClassHandler(req: Request, res: Response): Promise<void
|
|||
return;
|
||||
}
|
||||
|
||||
res.json(cls);
|
||||
res.json({ class: cls });
|
||||
}
|
||||
|
||||
export async function getClassStudentsHandler(req: Request, res: Response): Promise<void> {
|
||||
|
|
|
@ -6,9 +6,9 @@ import attachmentService from '../services/learning-objects/attachment-service.j
|
|||
import { BadRequestException } from '../exceptions/bad-request-exception.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import { envVars, getEnvVar } from '../util/envVars.js';
|
||||
import { FilteredLearningObject, LearningObjectIdentifier, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
|
||||
import { FilteredLearningObject, LearningObjectIdentifierDTO, LearningPathIdentifier } from '@dwengo-1/common/interfaces/learning-content';
|
||||
|
||||
function getLearningObjectIdentifierFromRequest(req: Request): LearningObjectIdentifier {
|
||||
function getLearningObjectIdentifierFromRequest(req: Request): LearningObjectIdentifierDTO {
|
||||
if (!req.params.hruid) {
|
||||
throw new BadRequestException('HRUID is required.');
|
||||
}
|
||||
|
|
|
@ -5,51 +5,35 @@ import {
|
|||
getAllQuestions,
|
||||
getAnswersByQuestion,
|
||||
getQuestion,
|
||||
getQuestionsAboutLearningObjectInAssignment,
|
||||
getQuestionsAboutLearningObjectInAssignment, updateQuestion,
|
||||
} from '../services/questions.js';
|
||||
import { FALLBACK_LANG, FALLBACK_SEQ_NUM } from '../config.js';
|
||||
import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js';
|
||||
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { QuestionData, QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { Language } from '@dwengo-1/common/util/language';
|
||||
import { AnswerDTO, AnswerId } from '@dwengo-1/common/interfaces/answer';
|
||||
|
||||
interface QuestionPathParams {
|
||||
hruid: string;
|
||||
version: string;
|
||||
}
|
||||
|
||||
interface QuestionQueryParams {
|
||||
lang: string;
|
||||
}
|
||||
|
||||
function getObjectId<ResBody, ReqBody>(
|
||||
req: Request<QuestionPathParams, ResBody, ReqBody, QuestionQueryParams>,
|
||||
res: Response
|
||||
): LearningObjectIdentifier | null {
|
||||
const { hruid, version } = req.params;
|
||||
const lang = req.query.lang;
|
||||
|
||||
if (!hruid || !version) {
|
||||
res.status(400).json({ error: 'Missing required parameters.' });
|
||||
return null;
|
||||
}
|
||||
import {requireFields} from "./error-helper";
|
||||
|
||||
export function getLearningObjectId(hruid: string, version: string, lang: string): LearningObjectIdentifier {
|
||||
return {
|
||||
hruid,
|
||||
language: (lang as Language) || FALLBACK_LANG,
|
||||
version: Number(version),
|
||||
language: (lang || FALLBACK_LANG) as Language,
|
||||
version: Number(version) || FALLBACK_VERSION_NUM,
|
||||
};
|
||||
}
|
||||
|
||||
interface GetQuestionIdPathParams extends QuestionPathParams {
|
||||
seq: string;
|
||||
export function getQuestionId(learningObjectIdentifier: LearningObjectIdentifier, seq: string): QuestionId {
|
||||
return {
|
||||
learningObjectIdentifier,
|
||||
sequenceNumber: seq ? Number(seq) : FALLBACK_SEQ_NUM,
|
||||
};
|
||||
}
|
||||
function getQuestionId<ReqBody, ResBody>(
|
||||
req: Request<GetQuestionIdPathParams, ReqBody, ResBody, QuestionQueryParams>,
|
||||
res: Response
|
||||
): QuestionId | null {
|
||||
|
||||
function getQuestionId(req: Request, res: Response): QuestionId | null {
|
||||
const seq = req.params.seq;
|
||||
const learningObjectIdentifier = getObjectId(req, res);
|
||||
const hruid = req.params.hruid;
|
||||
const version = req.params.version;
|
||||
const language = req.query.lang as string;
|
||||
const learningObjectIdentifier = getLearningObjectId(hruid, version, language);
|
||||
|
||||
if (!learningObjectIdentifier) {
|
||||
return null;
|
||||
|
@ -61,117 +45,117 @@ function getQuestionId<ReqBody, ResBody>(
|
|||
};
|
||||
}
|
||||
|
||||
interface GetAllQuestionsQueryParams extends QuestionQueryParams {
|
||||
classId?: string;
|
||||
assignmentId?: number;
|
||||
forStudent?: string;
|
||||
full?: boolean;
|
||||
}
|
||||
export async function getAllQuestionsHandler(req: Request, res: Response): Promise<void> {
|
||||
const hruid = req.params.hruid;
|
||||
const version = req.params.version;
|
||||
const language = req.query.lang as string;
|
||||
const full = req.query.full === 'true';
|
||||
requireFields({ hruid });
|
||||
|
||||
export async function getAllQuestionsHandler(
|
||||
req: Request<QuestionPathParams, QuestionDTO[] | QuestionId[], unknown, GetAllQuestionsQueryParams>,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const objectId = getObjectId(req, res);
|
||||
const full = req.query.full;
|
||||
const learningObjectId = getLearningObjectId(hruid, version, language);
|
||||
|
||||
if (!objectId) {
|
||||
return;
|
||||
}
|
||||
let questions: QuestionDTO[] | QuestionId[];
|
||||
if (req.query.classId && req.query.assignmentId) {
|
||||
questions = await getQuestionsAboutLearningObjectInAssignment(
|
||||
objectId,
|
||||
learningObjectId,
|
||||
req.query.classId,
|
||||
req.query.assignmentId,
|
||||
full ?? false,
|
||||
req.query.forStudent
|
||||
);
|
||||
} else {
|
||||
questions = await getAllQuestions(objectId, full ?? false);
|
||||
questions = await getAllQuestions(learningObjectId, full ?? false);
|
||||
}
|
||||
|
||||
if (!questions) {
|
||||
res.status(404).json({ error: `Questions not found.` });
|
||||
} else {
|
||||
res.json({ questions: questions });
|
||||
}
|
||||
res.json({ questions });
|
||||
}
|
||||
|
||||
export async function getQuestionHandler(
|
||||
req: Request<GetQuestionIdPathParams, QuestionDTO[] | QuestionId[], unknown, QuestionQueryParams>,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const questionId = getQuestionId(req, res);
|
||||
export async function getQuestionHandler(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 });
|
||||
|
||||
if (!questionId) {
|
||||
return;
|
||||
const learningObjectId = getLearningObjectId(hruid, version, language);
|
||||
const questionId = getQuestionId(learningObjectId, seq);
|
||||
|
||||
const question = await getQuestion(questionId);
|
||||
|
||||
res.json({ question });
|
||||
}
|
||||
|
||||
const question = await getQuestion(questionId);
|
||||
export async function getQuestionAnswersHandler(
|
||||
req: Request<GetQuestionIdPathParams, { answers: AnswerDTO[] | AnswerId[] }, unknown, GetQuestionAnswersQueryParams>,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const questionId = getQuestionId(req, res);
|
||||
const full = req.query.full;
|
||||
|
||||
if (!question) {
|
||||
res.status(404).json({ error: `Question not found.` });
|
||||
} else {
|
||||
res.json(question);
|
||||
if (!questionId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const answers = await getAnswersByQuestion(questionId, full);
|
||||
|
||||
if (!answers) {
|
||||
res.status(404).json({ error: `Questions not found` });
|
||||
} else {
|
||||
res.json({ answers: answers });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface GetQuestionAnswersQueryParams extends QuestionQueryParams {
|
||||
full: boolean;
|
||||
}
|
||||
export async function getQuestionAnswersHandler(
|
||||
req: Request<GetQuestionIdPathParams, { answers: AnswerDTO[] | AnswerId[] }, unknown, GetQuestionAnswersQueryParams>,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const questionId = getQuestionId(req, res);
|
||||
const full = req.query.full;
|
||||
|
||||
if (!questionId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const answers = await getAnswersByQuestion(questionId, full);
|
||||
|
||||
if (!answers) {
|
||||
res.status(404).json({ error: `Questions not found` });
|
||||
} else {
|
||||
res.json({ answers: answers });
|
||||
}
|
||||
}
|
||||
|
||||
export async function createQuestionHandler(req: Request, res: Response): Promise<void> {
|
||||
const questionDTO = req.body as QuestionDTO;
|
||||
const hruid = req.params.hruid;
|
||||
const version = req.params.version;
|
||||
const language = req.query.lang as string;
|
||||
requireFields({ hruid });
|
||||
|
||||
if (!questionDTO.learningObjectIdentifier || !questionDTO.author || !questionDTO.inGroup || !questionDTO.content) {
|
||||
res.status(400).json({ error: 'Missing required fields: identifier, author, inGroup, and content' });
|
||||
return;
|
||||
}
|
||||
const loId = getLearningObjectId(hruid, version, language);
|
||||
|
||||
const question = await createQuestion(questionDTO);
|
||||
const author = req.body.author as string;
|
||||
const content = req.body.content as string;
|
||||
const inGroup = req.body.inGroup as string;
|
||||
requireFields({ author, content, inGroup });
|
||||
|
||||
if (!question) {
|
||||
res.status(400).json({ error: 'Could not create question' });
|
||||
} else {
|
||||
res.json(question);
|
||||
}
|
||||
const questionData = req.body as QuestionData;
|
||||
|
||||
const question = await createQuestion(loId, questionData);
|
||||
|
||||
res.json({ question });
|
||||
}
|
||||
|
||||
export async function deleteQuestionHandler(
|
||||
req: Request<GetQuestionIdPathParams, QuestionDTO, unknown, QuestionQueryParams>,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const questionId = getQuestionId(req, res);
|
||||
export async function deleteQuestionHandler(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 });
|
||||
|
||||
if (!questionId) {
|
||||
return;
|
||||
}
|
||||
const learningObjectId = getLearningObjectId(hruid, version, language);
|
||||
const questionId = getQuestionId(learningObjectId, seq);
|
||||
|
||||
const question = await deleteQuestion(questionId);
|
||||
|
||||
if (!question) {
|
||||
res.status(400).json({ error: 'Could not find nor delete question' });
|
||||
} else {
|
||||
res.json(question);
|
||||
}
|
||||
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 });
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue