feat: class/:id service en controller laag geimplementeerd (BEVAT NOG BUG)
This commit is contained in:
		
							parent
							
								
									ceef74f1af
								
							
						
					
					
						commit
						e9d9e52f9d
					
				
					 5 changed files with 60 additions and 33 deletions
				
			
		
							
								
								
									
										29
									
								
								backend/src/controllers/classes.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								backend/src/controllers/classes.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,29 @@ | |||
| import { Request, Response } from 'express'; | ||||
| import { getClass } from '../services/class'; | ||||
| 
 | ||||
| export async function getClassHandler( | ||||
|     req: Request, | ||||
|     res: Response, | ||||
| ): Promise<void> { | ||||
|     try { | ||||
|         const classId = req.params.id; | ||||
|         const cls = await getClass(classId); | ||||
| 
 | ||||
|         if (!cls) { | ||||
|             res.status(404).json({ error: "Student not found" }); | ||||
|             return; | ||||
|         } else { | ||||
|             cls.endpoints = { | ||||
|                 self: `${req.baseUrl}/${req.params.id}`, | ||||
|                 invitations: `${req.baseUrl}/${req.params.id}/invitations`, | ||||
|                 assignments: `${req.baseUrl}/${req.params.id}/assignments`, | ||||
|                 students: `${req.baseUrl}/${req.params.id}/students`, | ||||
|             } | ||||
| 
 | ||||
|             res.json(cls); | ||||
|         } | ||||
|     } catch (error) { | ||||
|         console.error('Error fetching learning objects:', error); | ||||
|         res.status(500).json({ error: 'Internal server error' }); | ||||
|     } | ||||
| } | ||||
|  | @ -1,6 +1,4 @@ | |||
| import { ClassJoinRequest } from "../entities/classes/class-join-request.entity"; | ||||
| import { Student } from "../entities/users/student.entity"; | ||||
| import { Teacher } from "../entities/users/teacher.entity"; | ||||
| import { Class } from "../entities/classes/class.entity"; | ||||
| 
 | ||||
| export interface ClassDTO { | ||||
|     id: string; | ||||
|  | @ -9,9 +7,19 @@ export interface ClassDTO { | |||
|     students: string[]; | ||||
|     joinRequests: string[]; | ||||
|     endpoints?: { | ||||
|         classes: string; | ||||
|         questions: string; | ||||
|         self: string; | ||||
|         invitations: string; | ||||
|         groups: string; | ||||
|         assignments: string; | ||||
|         students: string; | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| export function mapToClassDTO(cls: Class): ClassDTO { | ||||
|     return { | ||||
|         id: cls.classId, | ||||
|         displayName: cls.displayName, | ||||
|         teachers: cls.teachers.map(teacher => teacher.username), | ||||
|         students: cls.students.map(student => student.username), | ||||
|         joinRequests: [], // TODO
 | ||||
|     } | ||||
| }; | ||||
|  |  | |||
|  | @ -1,4 +1,5 @@ | |||
| import express from 'express' | ||||
| import { getClassHandler } from '../controllers/classes'; | ||||
| const router = express.Router(); | ||||
| 
 | ||||
| // root endpoint used to search objects
 | ||||
|  | @ -12,21 +13,7 @@ router.get('/', (req, res) => { | |||
| }); | ||||
| 
 | ||||
| // information about an class with id 'id'
 | ||||
| router.get('/:id', (req, res) => { | ||||
|     res.json({ | ||||
|         id: req.params.id, | ||||
|         displayName: 'Klas 4B', | ||||
|         teachers: [ '0' ], | ||||
|         students: [ '0' ], | ||||
|         joinRequests: [ '0' ], | ||||
|         endpoints: { | ||||
|             self: `${req.baseUrl}/${req.params.id}`, | ||||
|             classes: `${req.baseUrl}/${req.params.id}/invitations`, | ||||
|             questions: `${req.baseUrl}/${req.params.id}/assignments`, | ||||
|             students: `${req.baseUrl}/${req.params.id}/students`, | ||||
|         } | ||||
|     }); | ||||
| }) | ||||
| router.get('/:id', getClassHandler); | ||||
| 
 | ||||
| router.get('/:id/invitations', (req, res) => { | ||||
|     res.json({ | ||||
|  |  | |||
							
								
								
									
										12
									
								
								backend/src/services/class.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								backend/src/services/class.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,12 @@ | |||
| import { getClassRepository } from "../data/repositories"; | ||||
| import { ClassDTO, mapToClassDTO } from "../interfaces/classes"; | ||||
| 
 | ||||
| export async function getClass(classId: string): Promise<ClassDTO | null> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classId); | ||||
| 
 | ||||
|     if (!cls) return null; | ||||
|     else { | ||||
|         return mapToClassDTO(cls); | ||||
|     } | ||||
| } | ||||
|  | @ -1,6 +1,6 @@ | |||
| import { getClassRepository, getStudentRepository } from "../data/repositories"; | ||||
| import { Class } from "../entities/classes/class.entity"; | ||||
| import { ClassDTO } from "../interfaces/classes"; | ||||
| import { ClassDTO, mapToClassDTO } from "../interfaces/classes"; | ||||
| import { StudentDTO } from "../interfaces/students"; | ||||
| 
 | ||||
| export async function getAllStudents(): Promise<StudentDTO[]> { | ||||
|  | @ -46,20 +46,11 @@ async function fetchStudentClasses(username: string): Promise<Class[]> { | |||
| 
 | ||||
| export async function getStudentClasses(username: string): Promise<ClassDTO[]> { | ||||
|     const classes = await fetchStudentClasses(username); | ||||
| 
 | ||||
|     return classes.map((cls: Class): ClassDTO => { | ||||
|         return { | ||||
|             id: cls.classId, | ||||
|             displayName: cls.displayName, | ||||
|             teachers: cls.teachers.map(teacher => teacher.username), | ||||
|             students: cls.students.map(student => student.username), | ||||
|             joinRequests: [], // TODO
 | ||||
|         } | ||||
|     }); | ||||
|     return classes.map(mapToClassDTO); | ||||
| } | ||||
| 
 | ||||
| export async function getStudentClassIds(username: string): Promise<string[]> { | ||||
|     const classes = await fetchStudentClasses(username); | ||||
|     return classes.map(cls => cls.classId); // class is a native keyword
 | ||||
|     return classes.map(cls => cls.classId); // 'class' is a native keyword
 | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet