feat: alle assignments en student's assignments geimplementeerd
This commit is contained in:
		
							parent
							
								
									7e051d412a
								
							
						
					
					
						commit
						e0a5596994
					
				
					 6 changed files with 77 additions and 24 deletions
				
			
		|  | @ -1,12 +1,26 @@ | ||||||
| import { Request, Response } from 'express' | import { Request, Response } from 'express' | ||||||
| import { getAssignment } from '../services/assignments'; | import { getAllAssignments, getAssignment } from '../services/assignments'; | ||||||
| 
 | 
 | ||||||
| // typescript is annoywith with parameter forwarding from class.ts
 | // typescript is annoy with with parameter forwarding from class.ts
 | ||||||
| interface AssignmentParams { | interface AssignmentParams { | ||||||
|     classid: string; |     classid: string; | ||||||
|     id: string; |     id: string; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | export async function getAllAssignmentsHandler( | ||||||
|  |     req: Request<AssignmentParams>, | ||||||
|  |     res: Response, | ||||||
|  | ): Promise<void> { | ||||||
|  |     const classid = req.params.classid; | ||||||
|  |     const full = req.query.full === 'true'; | ||||||
|  | 
 | ||||||
|  |     const assignments = await getAllAssignments(classid, full); | ||||||
|  | 
 | ||||||
|  |     res.json({ | ||||||
|  |         assignments: assignments, | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export async function getAssignmentHandler( | export async function getAssignmentHandler( | ||||||
|     req: Request<AssignmentParams>, |     req: Request<AssignmentParams>, | ||||||
|     res: Response, |     res: Response, | ||||||
|  |  | ||||||
|  | @ -1,6 +1,7 @@ | ||||||
| import { Request, Response } from 'express'; | import { Request, Response } from 'express'; | ||||||
| import { getAllStudents, getStudent, getStudentClasses, getStudentClassIds } from '../services/students'; | import { getAllStudents, getStudent, getStudentClasses, getStudentClassIds } from '../services/students'; | ||||||
| import { ClassDTO } from '../interfaces/classes'; | import { ClassDTO } from '../interfaces/classes'; | ||||||
|  | import { getAllAssignments } from '../services/assignments'; | ||||||
| 
 | 
 | ||||||
| // TODO: accept arguments (full, ...)
 | // TODO: accept arguments (full, ...)
 | ||||||
| // TODO: endpoints
 | // TODO: endpoints
 | ||||||
|  | @ -73,3 +74,22 @@ export async function getStudentClassesHandler ( | ||||||
|         res.status(500).json({ error: 'Internal server error' }); |         res.status(500).json({ error: 'Internal server error' }); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // Might not be fully correct depending on if
 | ||||||
|  | // a class has an assignment, that all students
 | ||||||
|  | // have this assignment.
 | ||||||
|  | export async function getStudentAssignmentsHandler( | ||||||
|  |     req: Request, | ||||||
|  |     res: Response, | ||||||
|  | ): Promise<void> { | ||||||
|  |     const full = req.query.full === 'true'; | ||||||
|  |     const username = req.params.id; | ||||||
|  | 
 | ||||||
|  |     const classes = await getStudentClasses(username); | ||||||
|  | 
 | ||||||
|  |     const assignments = (await Promise.all(classes.map(async cls => await getAllAssignments(cls.id, full)))).flat(); | ||||||
|  | 
 | ||||||
|  |     res.json({ | ||||||
|  |         assignments: assignments | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| import { Assignment } from "../entities/assignments/assignment.entity"; | import { Assignment } from "../entities/assignments/assignment.entity"; | ||||||
| import { Class } from "../entities/classes/class.entity"; | import { Class } from "../entities/classes/class.entity"; | ||||||
| import { GroupDTO } from "./groups"; | import { GroupDTO, mapToGroupDTO } from "./groups"; | ||||||
| 
 | 
 | ||||||
| export interface AssignmentDTO { | export interface AssignmentDTO { | ||||||
|     id: number, |     id: number, | ||||||
|  | @ -9,17 +9,29 @@ export interface AssignmentDTO { | ||||||
|     description: string, |     description: string, | ||||||
|     learningPath: string, |     learningPath: string, | ||||||
|     language: string, |     language: string, | ||||||
|     groups?: GroupDTO[], // TODO
 |     groups?: GroupDTO[] | string[], // TODO
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function mapToAssignmentDTO(assignment: Assignment, cls: Class): AssignmentDTO { | export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { | ||||||
|     return { |     return { | ||||||
|         id: assignment.id, |         id: assignment.id, | ||||||
|         class: cls.classId, |         class: assignment.within.classId, | ||||||
|         title: assignment.title, |         title: assignment.title, | ||||||
|         description: assignment.description, |         description: assignment.description, | ||||||
|         learningPath: assignment.learningPathHruid, |         learningPath: assignment.learningPathHruid, | ||||||
|         language: assignment.learningPathLanguage, |         language: assignment.learningPathLanguage, | ||||||
|         //groups: assignment.groups.map(mapToGroupDTO),
 |         // groups: assignment.groups.map(group => group.groupNumber),
 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | ||||||
|  |     return { | ||||||
|  |         id: assignment.id, | ||||||
|  |         class: assignment.within.classId, | ||||||
|  |         title: assignment.title, | ||||||
|  |         description: assignment.description, | ||||||
|  |         learningPath: assignment.learningPathHruid, | ||||||
|  |         language: assignment.learningPathLanguage, | ||||||
|  |         // groups: assignment.groups.map(mapToGroupDTO),
 | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  | @ -1,18 +1,11 @@ | ||||||
| import express from 'express' | import express from 'express' | ||||||
| import { getAssignmentHandler } from '../controllers/assignments'; | import { getAllAssignmentsHandler, getAssignmentHandler } from '../controllers/assignments'; | ||||||
| import groupRouter from './group.js'; | import groupRouter from './group.js'; | ||||||
| 
 | 
 | ||||||
| const router = express.Router({ mergeParams: true }); | const router = express.Router({ mergeParams: true }); | ||||||
| 
 | 
 | ||||||
| // root endpoint used to search objects
 | // root endpoint used to search objects
 | ||||||
| router.get('/', (req, res) => { | router.get('/', getAllAssignmentsHandler); | ||||||
|     res.json({ |  | ||||||
|         assignments: [ |  | ||||||
|             '0', |  | ||||||
|             '1', |  | ||||||
|         ] |  | ||||||
|     }); |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| // information about an assignment with id 'id'
 | // information about an assignment with id 'id'
 | ||||||
| router.get('/:id', getAssignmentHandler); | router.get('/:id', getAssignmentHandler); | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| import express from 'express' | import express from 'express' | ||||||
| import { getAllStudentsHandler, getStudentClassesHandler, getStudentHandler } from '../controllers/students'; | import { getAllStudentsHandler, getStudentAssignmentsHandler, getStudentClassesHandler, getStudentHandler } from '../controllers/students'; | ||||||
| const router = express.Router(); | const router = express.Router(); | ||||||
| 
 | 
 | ||||||
| // root endpoint used to search objects
 | // root endpoint used to search objects
 | ||||||
|  | @ -20,11 +20,7 @@ router.get('/:id/submissions', (req, res) => { | ||||||
| 
 | 
 | ||||||
|    |    | ||||||
| // the list of assignments a student has
 | // the list of assignments a student has
 | ||||||
| router.get('/:id/assignments', (req, res) => { | router.get('/:id/assignments', getStudentAssignmentsHandler); | ||||||
|     res.json({ |  | ||||||
|         assignments: [ '0' ], |  | ||||||
|     }); |  | ||||||
| }) |  | ||||||
|    |    | ||||||
| // the list of groups a student is in
 | // the list of groups a student is in
 | ||||||
| router.get('/:id/groups', (req, res) => { | router.get('/:id/groups', (req, res) => { | ||||||
|  |  | ||||||
|  | @ -1,5 +1,23 @@ | ||||||
| import { getAssignmentRepository, getClassRepository } from "../data/repositories"; | import { getAssignmentRepository, getClassRepository } from "../data/repositories"; | ||||||
| import { AssignmentDTO, mapToAssignmentDTO } from "../interfaces/assignments"; | import { AssignmentDTO, mapToAssignmentDTO, mapToAssignmentDTOId } from "../interfaces/assignments"; | ||||||
|  | 
 | ||||||
|  | export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> { | ||||||
|  |     const classRepository = getClassRepository(); | ||||||
|  |     const cls = await classRepository.findById(classid); | ||||||
|  | 
 | ||||||
|  |     if (!cls) { | ||||||
|  |         return []; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     const assignmentRepository = getAssignmentRepository(); | ||||||
|  |     const assignments = await assignmentRepository.findAllAssignmentsInClass(cls); | ||||||
|  | 
 | ||||||
|  |     if (full) { | ||||||
|  |         return assignments.map(mapToAssignmentDTO); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return assignments.map(mapToAssignmentDTOId); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO | null> { | export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO | null> { | ||||||
|     const classRepository = getClassRepository(); |     const classRepository = getClassRepository(); | ||||||
|  | @ -16,5 +34,5 @@ export async function getAssignment(classid: string, id: number): Promise<Assign | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return mapToAssignmentDTO(assignment, cls); |     return mapToAssignmentDTO(assignment); | ||||||
| } | } | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet