Merge branch 'dev' into feat/home-data

This commit is contained in:
Gabriellvl 2025-03-02 12:41:24 +01:00 committed by GitHub
commit 1531fa36fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 931 additions and 35 deletions

View file

@ -0,0 +1,60 @@
import { Request, Response } from 'express';
import {
getLearningObjectById,
getLearningObjectIdsFromPath,
getLearningObjectsFromPath,
} from '../services/learningObjects.js';
import { FALLBACK_LANG } from '../config.js';
import { FilteredLearningObject } from '../interfaces/learningPath';
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 getLearningObjectsFromPath(hruid, language);
} else {
learningObjects = await 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 getLearningObjectById(hruid, language);
res.json(learningObject);
} catch (error) {
console.error('Error fetching learning object:', error);
res.status(500).json({ error: 'Internal server error' });
}
}

View file

@ -0,0 +1,62 @@
import { Request, Response } from 'express';
import { themes } from '../data/themes.js';
import { FALLBACK_LANG } from '../config.js';
import {
fetchLearningPaths,
searchLearningPaths,
} from '../services/learningPaths.js';
/**
* 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 searchLearningPaths(
searchQuery,
language
);
res.json(searchResults);
return;
} else {
hruidList = themes.flatMap((theme) => {
return theme.hruids;
});
}
const learningPaths = await 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' });
}
}

View file

@ -1,6 +1,7 @@
import { Request, Response } from 'express';
import { themes } from '../data/themes.js';
import { loadTranslations } from "../util/translationHelper.js";
import { FALLBACK_LANG } from '../config.js';
interface Translations {
curricula_page: {
@ -8,13 +9,9 @@ interface Translations {
};
}
/**
* GET /themes Haalt de lijst met thema's op inclusief vertalingen
*/
export function getThemes(req: Request, res: Response) {
const language = (req.query.language as string)?.toLowerCase() || 'nl';
const translations = loadTranslations<Translations>(language);
const themeList = themes.map((theme) => {
return {
key: theme.title,
@ -28,9 +25,6 @@ export function getThemes(req: Request, res: Response) {
res.json(themeList);
}
/**
* GET /themes/:theme Geeft de HRUIDs terug voor een specifiek thema
*/
export function getThemeByTitle(req: Request, res: Response) {
const themeKey = req.params.theme;
const theme = themes.find((t) => {
@ -40,6 +34,6 @@ export function getThemeByTitle(req: Request, res: Response) {
if (theme) {
res.json(theme.hruids);
} else {
res.status(404).json({ error: 'Thema niet gevonden' });
res.status(404).json({ error: 'Theme not found' });
}
}