feat(backend): Added endpoint to fetch HTML version of learning object (from Dwengo backend)
Also refactored a bit to make this easier.
This commit is contained in:
parent
770c5c9879
commit
18ee991ce3
16 changed files with 264 additions and 178 deletions
62
backend/src/controllers/learning-objects.ts
Normal file
62
backend/src/controllers/learning-objects.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { FALLBACK_LANG } from '../config.js';
|
||||
import {FilteredLearningObject, LearningObjectIdentifier, LearningPathIdentifier} from '../interfaces/learning-content';
|
||||
import learningObjectService from "../services/learning-content/learning-object-service";
|
||||
import {EnvVars, getEnvVar} from "../util/envvars";
|
||||
import {Language} from "../entities/content/language";
|
||||
import {BadRequestException} from "../exceptions";
|
||||
|
||||
function getLearningObjectIdentifierFromRequest(req: Request): LearningObjectIdentifier {
|
||||
if (!req.params.hruid) {
|
||||
throw new BadRequestException("HRUID is required.");
|
||||
}
|
||||
return {
|
||||
hruid: req.params.hruid as string,
|
||||
language: (req.query.language || getEnvVar(EnvVars.FallbackLanguage)) as Language,
|
||||
version: req.query.version as string
|
||||
};
|
||||
}
|
||||
|
||||
function getLearningPathIdentifierFromRequest(req: Request): LearningPathIdentifier {
|
||||
if (!req.query.hruid) {
|
||||
throw new BadRequestException("HRUID is required.");
|
||||
}
|
||||
return {
|
||||
hruid: req.params.hruid as string,
|
||||
language: (req.query.language as Language) || FALLBACK_LANG
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllLearningObjects(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const learningPathId = getLearningPathIdentifierFromRequest(req);
|
||||
const full = req.query.full;
|
||||
|
||||
let learningObjects: FilteredLearningObject[] | string[];
|
||||
if (full) {
|
||||
learningObjects = await learningObjectService.getLearningObjectsFromPath(learningPathId);
|
||||
} else {
|
||||
learningObjects = await learningObjectService.getLearningObjectIdsFromPath(learningPathId);
|
||||
}
|
||||
|
||||
res.json(learningObjects);
|
||||
}
|
||||
|
||||
export async function getLearningObject(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const learningObjectId = getLearningObjectIdentifierFromRequest(req);
|
||||
|
||||
const learningObject = await learningObjectService.getLearningObjectById(learningObjectId);
|
||||
res.json(learningObject);
|
||||
}
|
||||
|
||||
export async function getLearningObjectHTML(req: Request, res: Response): Promise<void> {
|
||||
const learningObjectId = getLearningObjectIdentifierFromRequest(req);
|
||||
|
||||
const learningObject = await learningObjectService.getLearningObjectHTML(learningObjectId);
|
||||
res.send(learningObject);
|
||||
}
|
53
backend/src/controllers/learning-paths.ts
Normal file
53
backend/src/controllers/learning-paths.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { themes } from '../data/themes.js';
|
||||
import { FALLBACK_LANG } from '../config.js';
|
||||
import learningPathService from "../services/learning-content/learning-path-service";
|
||||
import {NotFoundException} from "../exceptions";
|
||||
|
||||
/**
|
||||
* Fetch learning paths based on query parameters.
|
||||
*/
|
||||
export async function getLearningPaths(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const hruids = req.query.hruid;
|
||||
const themeKey = req.query.theme as string;
|
||||
const searchQuery = req.query.search as string;
|
||||
const language = (req.query.language as string) || FALLBACK_LANG;
|
||||
|
||||
let hruidList;
|
||||
|
||||
if (hruids) {
|
||||
hruidList = Array.isArray(hruids)
|
||||
? hruids.map(String)
|
||||
: [String(hruids)];
|
||||
} else if (themeKey) {
|
||||
const theme = themes.find((t) => {
|
||||
return t.title === themeKey;
|
||||
});
|
||||
if (theme) {
|
||||
hruidList = theme.hruids;
|
||||
} else {
|
||||
throw new NotFoundException(`Theme "${themeKey}" not found.`);
|
||||
}
|
||||
} else if (searchQuery) {
|
||||
const searchResults = await learningPathService.searchLearningPaths(
|
||||
searchQuery,
|
||||
language
|
||||
);
|
||||
res.json(searchResults);
|
||||
return;
|
||||
} else {
|
||||
hruidList = themes.flatMap((theme) => {
|
||||
return theme.hruids;
|
||||
});
|
||||
}
|
||||
|
||||
const learningPaths = await learningPathService.fetchLearningPaths(
|
||||
hruidList,
|
||||
language,
|
||||
`HRUIDs: ${hruidList.join(', ')}`
|
||||
);
|
||||
res.json(learningPaths.data);
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { FALLBACK_LANG } from '../config.js';
|
||||
import { FilteredLearningObject } from '../interfaces/learningContent';
|
||||
import learningObjectService from "../services/learning-content/learning-object-service";
|
||||
|
||||
export async function getAllLearningObjects(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const hruid = req.query.hruid as string;
|
||||
const full = req.query.full === 'true';
|
||||
const language = (req.query.language as string) || FALLBACK_LANG;
|
||||
|
||||
if (!hruid) {
|
||||
res.status(400).json({ error: 'HRUID query is required.' });
|
||||
return;
|
||||
}
|
||||
|
||||
let learningObjects: FilteredLearningObject[] | string[];
|
||||
if (full) {
|
||||
learningObjects = await learningObjectService.getLearningObjectsFromPath(hruid, language);
|
||||
} else {
|
||||
learningObjects = await learningObjectService.getLearningObjectIdsFromPath(
|
||||
hruid,
|
||||
language
|
||||
);
|
||||
}
|
||||
|
||||
res.json(learningObjects);
|
||||
} catch (error) {
|
||||
console.error('Error fetching learning objects:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
}
|
||||
|
||||
export async function getLearningObject(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const { hruid } = req.params;
|
||||
const language = (req.query.language as string) || FALLBACK_LANG;
|
||||
|
||||
if (!hruid) {
|
||||
res.status(400).json({ error: 'HRUID parameter is required.' });
|
||||
return;
|
||||
}
|
||||
|
||||
const learningObject = await learningObjectService.getLearningObjectById(hruid, language);
|
||||
res.json(learningObject);
|
||||
} catch (error) {
|
||||
console.error('Error fetching learning object:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { themes } from '../data/themes.js';
|
||||
import { FALLBACK_LANG } from '../config.js';
|
||||
import learningPathService from "../services/learning-content/learning-path-service";
|
||||
/**
|
||||
* Fetch learning paths based on query parameters.
|
||||
*/
|
||||
export async function getLearningPaths(
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
try {
|
||||
const hruids = req.query.hruid;
|
||||
const themeKey = req.query.theme as string;
|
||||
const searchQuery = req.query.search as string;
|
||||
const language = (req.query.language as string) || FALLBACK_LANG;
|
||||
|
||||
let hruidList;
|
||||
|
||||
if (hruids) {
|
||||
hruidList = Array.isArray(hruids)
|
||||
? hruids.map(String)
|
||||
: [String(hruids)];
|
||||
} else if (themeKey) {
|
||||
const theme = themes.find((t) => {
|
||||
return t.title === themeKey;
|
||||
});
|
||||
if (theme) {
|
||||
hruidList = theme.hruids;
|
||||
} else {
|
||||
res.status(404).json({
|
||||
error: `Theme "${themeKey}" not found.`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
} else if (searchQuery) {
|
||||
const searchResults = await learningPathService.searchLearningPaths(
|
||||
searchQuery,
|
||||
language
|
||||
);
|
||||
res.json(searchResults);
|
||||
return;
|
||||
} else {
|
||||
hruidList = themes.flatMap((theme) => {
|
||||
return theme.hruids;
|
||||
});
|
||||
}
|
||||
|
||||
const learningPaths = await learningPathService.fetchLearningPaths(
|
||||
hruidList,
|
||||
language,
|
||||
`HRUIDs: ${hruidList.join(', ')}`
|
||||
);
|
||||
res.json(learningPaths.data);
|
||||
} catch (error) {
|
||||
console.error('❌ Unexpected error fetching learning paths:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue