fix: splits controller en service beter op met querries
This commit is contained in:
		
							parent
							
								
									6bf4193da9
								
							
						
					
					
						commit
						308661d72b
					
				
					 2 changed files with 36 additions and 93 deletions
				
			
		|  | @ -1,113 +1,44 @@ | ||||||
| import { Request, Response } from 'express'; | import { Request, Response } from 'express'; | ||||||
| import { themes } from '../data/themes.js'; | import { themes } from '../data/themes.js'; | ||||||
| import { DWENGO_API_BASE, FALLBACK_LANG } from '../config.js'; | import { FALLBACK_LANG } from '../config.js'; | ||||||
| import { fetchWithLogging } from '../util/apiHelper.js'; | import { fetchLearningPaths, searchLearningPaths } from '../services/learningPaths.js'; | ||||||
| import { fetchLearningPaths } from '../services/learningPaths.js'; |  | ||||||
| import { LearningPath } from '../interfaces/learningPath'; |  | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * Fetch learning paths based on HRUIDs or return all if no HRUIDs are provided. |  * Fetch learning paths based on query parameters. | ||||||
|  * - If `hruids` are given -> fetch specific learning paths. |  | ||||||
|  * - If `hruids` is missing -> return all available learning paths. |  | ||||||
|  */ |  */ | ||||||
| export async function getLearningPaths( | export async function getLearningPaths( | ||||||
|     req: Request, |     req: Request, | ||||||
|     res: Response |     res: Response | ||||||
| ):  Promise<void> { | ):  Promise<void> { | ||||||
|     try { |     try { | ||||||
|         const hruids = req.query.hruids; // Can be string or array
 |         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; |         const language = (req.query.language as string) || FALLBACK_LANG; | ||||||
| 
 | 
 | ||||||
|         let hruidList: string[]; |         let hruidList = []; | ||||||
| 
 | 
 | ||||||
|         if (hruids) { |         if (hruids) { | ||||||
|             hruidList = Array.isArray(hruids) |             hruidList = Array.isArray(hruids) ? hruids.map(String) : [String(hruids)]; | ||||||
|                 ? hruids.map(String) |         } else if (themeKey) { | ||||||
|                 : [String(hruids)]; |             const theme = themes.find((t) => t.title === themeKey); | ||||||
|  |             if (theme) { | ||||||
|  |                 hruidList = theme.hruids; | ||||||
|             } else { |             } else { | ||||||
|             // If no hruids are provided, fetch ALL learning paths
 |                 res.status(404).json({ error: `Theme "${themeKey}" not found.` }); | ||||||
|             hruidList = themes.flatMap((theme) => { |                 return; | ||||||
|                 return theme.hruids; |             } | ||||||
|             }); |         } else if (searchQuery) { | ||||||
|  |             const searchResults = await searchLearningPaths(searchQuery, language); | ||||||
|  |             res.json(searchResults); | ||||||
|  |             return; | ||||||
|  |         } else { | ||||||
|  |             hruidList = themes.flatMap((theme) => theme.hruids); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const learningPaths = await fetchLearningPaths( |         const learningPaths = await fetchLearningPaths(hruidList, language, `HRUIDs: ${hruidList.join(', ')}`); | ||||||
|             hruidList, |         res.json(learningPaths.data); | ||||||
|             language, |  | ||||||
|             `HRUIDs: ${hruidList.join(', ')}` |  | ||||||
|         ); |  | ||||||
| 
 |  | ||||||
|         res.json(learningPaths); |  | ||||||
|     } catch (error) { |     } catch (error) { | ||||||
|         console.error('❌ Unexpected error fetching learning paths:', error); |         console.error('❌ Unexpected error fetching learning paths:', error); | ||||||
|         res.status(500).json({ error: 'Internal server error' }); |         res.status(500).json({ error: 'Internal server error' }); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Fetch all learning paths for a specific theme. |  | ||||||
|  */ |  | ||||||
| export async function getLearningPathsByTheme( |  | ||||||
|     req: Request, |  | ||||||
|     res: Response |  | ||||||
| ): Promise<void> { |  | ||||||
|     try { |  | ||||||
|         const themeKey = req.params.theme; |  | ||||||
|         const language = (req.query.language as string) || FALLBACK_LANG; |  | ||||||
| 
 |  | ||||||
|         const theme = themes.find((t) => { |  | ||||||
|             return t.title === themeKey; |  | ||||||
|         }); |  | ||||||
|         if (!theme) { |  | ||||||
|             console.error(`⚠️ WARNING: Theme "${themeKey}" not found.`); |  | ||||||
|             res.status(404).json({ error: 'Theme not found' }); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         const response = await fetchLearningPaths( |  | ||||||
|             theme.hruids, |  | ||||||
|             language, |  | ||||||
|             `theme "${themeKey}"` |  | ||||||
|         ); |  | ||||||
|         res.json({ |  | ||||||
|             theme: themeKey, |  | ||||||
|             hruids: theme.hruids, |  | ||||||
|             ...response, |  | ||||||
|         }); |  | ||||||
|     } catch (error) { |  | ||||||
|         console.error( |  | ||||||
|             '❌ Unexpected error fetching learning paths by theme:', |  | ||||||
|             error |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Search learning paths by query. |  | ||||||
|  */ |  | ||||||
| export async function searchLearningPaths( |  | ||||||
|     req: Request, |  | ||||||
|     res: Response |  | ||||||
| ): Promise<void> { |  | ||||||
|     try { |  | ||||||
|         const query = req.query.query as string; |  | ||||||
|         const language = (req.query.language as string) || FALLBACK_LANG; |  | ||||||
| 
 |  | ||||||
|         if (!query) { |  | ||||||
|             res.status(400).json({ error: 'Missing search query' }); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         const apiUrl = `${DWENGO_API_BASE}/learningPath/search`; |  | ||||||
|         const params = { all: query, language }; |  | ||||||
| 
 |  | ||||||
|         const searchResults = await fetchWithLogging<LearningPath[]>( |  | ||||||
|             apiUrl, |  | ||||||
|             `Search learning paths with query "${query}"`, |  | ||||||
|             params |  | ||||||
|         ); |  | ||||||
|         res.json(searchResults ?? []); |  | ||||||
|     } catch (error) { |  | ||||||
|         console.error('❌ Unexpected error searching learning paths:', error); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -44,3 +44,15 @@ export async function fetchLearningPaths( | ||||||
|         data: learningPaths, |         data: learningPaths, | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | export async function searchLearningPaths( | ||||||
|  |     query: string, | ||||||
|  |     language: string | ||||||
|  | ):  Promise<LearningPath[]>  { | ||||||
|  |     const apiUrl = `${DWENGO_API_BASE}/learningPath/search`; | ||||||
|  |     const params = { all: query, language }; | ||||||
|  | 
 | ||||||
|  |     const searchResults = await fetchWithLogging<LearningPath[]>(apiUrl, `Search learning paths with query "${query}"`, params); | ||||||
|  |     return searchResults ?? []; | ||||||
|  | } | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl