Merge pull request #175 from SELab-2/fix/class-join-request
fix: Class join request ontbrekende functionaliteit
This commit is contained in:
		
						commit
						45ca433e09
					
				
					 30 changed files with 688 additions and 465 deletions
				
			
		|  | @ -1,77 +1,94 @@ | |||
| import { Request, Response } from 'express'; | ||||
| import { createAssignment, getAllAssignments, getAssignment, getAssignmentsSubmissions } from '../services/assignments.js'; | ||||
| import { | ||||
|     createAssignment, | ||||
|     deleteAssignment, | ||||
|     getAllAssignments, | ||||
|     getAssignment, | ||||
|     getAssignmentsSubmissions, | ||||
|     putAssignment, | ||||
| } from '../services/assignments.js'; | ||||
| import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | ||||
| import { requireFields } from './error-helper.js'; | ||||
| import { BadRequestException } from '../exceptions/bad-request-exception.js'; | ||||
| import { Assignment } from '../entities/assignments/assignment.entity.js'; | ||||
| import { EntityDTO } from '@mikro-orm/core'; | ||||
| 
 | ||||
| // Typescript is annoying with parameter forwarding from class.ts
 | ||||
| interface AssignmentParams { | ||||
|     classid: string; | ||||
|     id: string; | ||||
| } | ||||
| 
 | ||||
| export async function getAllAssignmentsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> { | ||||
|     const classid = req.params.classid; | ||||
| export async function getAllAssignmentsHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.classid; | ||||
|     const full = req.query.full === 'true'; | ||||
| 
 | ||||
|     const assignments = await getAllAssignments(classid, full); | ||||
|     const assignments = await getAllAssignments(classId, full); | ||||
| 
 | ||||
|     res.json({ | ||||
|         assignments: assignments, | ||||
|     }); | ||||
|     res.json({ assignments }); | ||||
| } | ||||
| 
 | ||||
| export async function createAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> { | ||||
| export async function createAssignmentHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classid = req.params.classid; | ||||
|     const description = req.body.description; | ||||
|     const language = req.body.language; | ||||
|     const learningPath = req.body.learningPath; | ||||
|     const title = req.body.title; | ||||
| 
 | ||||
|     requireFields({ description, language, learningPath, title }); | ||||
| 
 | ||||
|     const assignmentData = req.body as AssignmentDTO; | ||||
| 
 | ||||
|     if (!assignmentData.description || !assignmentData.language || !assignmentData.learningPath || !assignmentData.title) { | ||||
|         res.status(400).json({ | ||||
|             error: 'Missing one or more required fields: title, description, learningPath, language', | ||||
|         }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     const assignment = await createAssignment(classid, assignmentData); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         res.status(500).json({ error: 'Could not create assignment ' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.status(201).json(assignment); | ||||
|     res.json({ assignment }); | ||||
| } | ||||
| 
 | ||||
| export async function getAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> { | ||||
| export async function getAssignmentHandler(req: Request, res: Response): Promise<void> { | ||||
|     const id = Number(req.params.id); | ||||
|     const classid = req.params.classid; | ||||
|     requireFields({ id, classid }); | ||||
| 
 | ||||
|     if (isNaN(id)) { | ||||
|         res.status(400).json({ error: 'Assignment id must be a number' }); | ||||
|         return; | ||||
|         throw new BadRequestException('Assignment id should be a number'); | ||||
|     } | ||||
| 
 | ||||
|     const assignment = await getAssignment(classid, id); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         res.status(404).json({ error: 'Assignment not found' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.json(assignment); | ||||
|     res.json({ assignment }); | ||||
| } | ||||
| 
 | ||||
| export async function getAssignmentsSubmissionsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> { | ||||
| export async function putAssignmentHandler(req: Request, res: Response): Promise<void> { | ||||
|     const id = Number(req.params.id); | ||||
|     const classid = req.params.classid; | ||||
|     requireFields({ id, classid }); | ||||
| 
 | ||||
|     if (isNaN(id)) { | ||||
|         throw new BadRequestException('Assignment id should be a number'); | ||||
|     } | ||||
| 
 | ||||
|     const assignmentData = req.body as Partial<EntityDTO<Assignment>>; | ||||
|     const assignment = await putAssignment(classid, id, assignmentData); | ||||
| 
 | ||||
|     res.json({ assignment }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteAssignmentHandler(req: Request, _res: Response): Promise<void> { | ||||
|     const id = Number(req.params.id); | ||||
|     const classid = req.params.classid; | ||||
|     requireFields({ id, classid }); | ||||
| 
 | ||||
|     if (isNaN(id)) { | ||||
|         throw new BadRequestException('Assignment id should be a number'); | ||||
|     } | ||||
| 
 | ||||
|     await deleteAssignment(classid, id); | ||||
| } | ||||
| 
 | ||||
| export async function getAssignmentsSubmissionsHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classid = req.params.classid; | ||||
|     const assignmentNumber = Number(req.params.id); | ||||
|     const full = req.query.full === 'true'; | ||||
|     requireFields({ assignmentNumber, classid }); | ||||
| 
 | ||||
|     if (isNaN(assignmentNumber)) { | ||||
|         res.status(400).json({ error: 'Assignment id must be a number' }); | ||||
|         return; | ||||
|         throw new BadRequestException('Assignment id should be a number'); | ||||
|     } | ||||
| 
 | ||||
|     const submissions = await getAssignmentsSubmissions(classid, assignmentNumber, full); | ||||
| 
 | ||||
|     res.json({ | ||||
|         submissions: submissions, | ||||
|     }); | ||||
|     res.json({ submissions }); | ||||
| } | ||||
|  |  | |||
|  | @ -1,44 +1,62 @@ | |||
| import { Request, Response } from 'express'; | ||||
| import { createClass, getAllClasses, getClass, getClassStudents, getClassStudentsIds, getClassTeacherInvitations } from '../services/classes.js'; | ||||
| import { | ||||
|     addClassStudent, | ||||
|     addClassTeacher, | ||||
|     createClass, | ||||
|     deleteClass, | ||||
|     deleteClassStudent, | ||||
|     deleteClassTeacher, | ||||
|     getAllClasses, | ||||
|     getClass, | ||||
|     getClassStudents, | ||||
|     getClassTeacherInvitations, | ||||
|     getClassTeachers, | ||||
|     putClass, | ||||
| } from '../services/classes.js'; | ||||
| import { ClassDTO } from '@dwengo-1/common/interfaces/class'; | ||||
| import { requireFields } from './error-helper.js'; | ||||
| import { EntityDTO } from '@mikro-orm/core'; | ||||
| import { Class } from '../entities/classes/class.entity.js'; | ||||
| 
 | ||||
| export async function getAllClassesHandler(req: Request, res: Response): Promise<void> { | ||||
|     const full = req.query.full === 'true'; | ||||
|     const classes = await getAllClasses(full); | ||||
| 
 | ||||
|     res.json({ | ||||
|         classes: classes, | ||||
|     }); | ||||
|     res.json({ classes }); | ||||
| } | ||||
| 
 | ||||
| export async function createClassHandler(req: Request, res: Response): Promise<void> { | ||||
|     const displayName = req.body.displayName; | ||||
|     requireFields({ displayName }); | ||||
| 
 | ||||
|     const classData = req.body as ClassDTO; | ||||
| 
 | ||||
|     if (!classData.displayName) { | ||||
|         res.status(400).json({ | ||||
|             error: 'Missing one or more required fields: displayName', | ||||
|         }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     const cls = await createClass(classData); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         res.status(500).json({ error: 'Something went wrong while creating class' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.status(201).json({ class: cls }); | ||||
|     res.json({ class: cls }); | ||||
| } | ||||
| 
 | ||||
| export async function getClassHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     requireFields({ classId }); | ||||
| 
 | ||||
|     const cls = await getClass(classId); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         res.status(404).json({ error: 'Class not found' }); | ||||
|         return; | ||||
|     } | ||||
|     res.json({ class: cls }); | ||||
| } | ||||
| 
 | ||||
| export async function putClassHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     requireFields({ classId }); | ||||
| 
 | ||||
|     const newData = req.body as Partial<EntityDTO<Class>>; | ||||
|     const cls = await putClass(classId, newData); | ||||
| 
 | ||||
|     res.json({ class: cls }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteClassHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     const cls = await deleteClass(classId); | ||||
| 
 | ||||
|     res.json({ class: cls }); | ||||
| } | ||||
|  | @ -46,21 +64,69 @@ export async function getClassHandler(req: Request, res: Response): Promise<void | |||
| export async function getClassStudentsHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     const full = req.query.full === 'true'; | ||||
|     requireFields({ classId }); | ||||
| 
 | ||||
|     const students = full ? await getClassStudents(classId) : await getClassStudentsIds(classId); | ||||
|     const students = await getClassStudents(classId, full); | ||||
| 
 | ||||
|     res.json({ | ||||
|         students: students, | ||||
|     }); | ||||
|     res.json({ students }); | ||||
| } | ||||
| 
 | ||||
| export async function getClassTeachersHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     const full = req.query.full === 'true'; | ||||
|     requireFields({ classId }); | ||||
| 
 | ||||
|     const teachers = await getClassTeachers(classId, full); | ||||
| 
 | ||||
|     res.json({ teachers }); | ||||
| } | ||||
| 
 | ||||
| export async function getTeacherInvitationsHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     const full = req.query.full === 'true'; | ||||
|     requireFields({ classId }); | ||||
| 
 | ||||
|     const invitations = await getClassTeacherInvitations(classId, full); | ||||
| 
 | ||||
|     res.json({ | ||||
|         invitations: invitations, | ||||
|     }); | ||||
|     res.json({ invitations }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteClassStudentHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     const username = req.params.username; | ||||
|     requireFields({ classId, username }); | ||||
| 
 | ||||
|     const cls = await deleteClassStudent(classId, username); | ||||
| 
 | ||||
|     res.json({ class: cls }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteClassTeacherHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     const username = req.params.username; | ||||
|     requireFields({ classId, username }); | ||||
| 
 | ||||
|     const cls = await deleteClassTeacher(classId, username); | ||||
| 
 | ||||
|     res.json({ class: cls }); | ||||
| } | ||||
| 
 | ||||
| export async function addClassStudentHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     const username = req.body.username; | ||||
|     requireFields({ classId, username }); | ||||
| 
 | ||||
|     const cls = await addClassStudent(classId, username); | ||||
| 
 | ||||
|     res.json({ class: cls }); | ||||
| } | ||||
| 
 | ||||
| export async function addClassTeacherHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.id; | ||||
|     const username = req.body.username; | ||||
|     requireFields({ classId, username }); | ||||
| 
 | ||||
|     const cls = await addClassTeacher(classId, username); | ||||
| 
 | ||||
|     res.json({ class: cls }); | ||||
| } | ||||
|  |  | |||
|  | @ -1,100 +1,104 @@ | |||
| import { Request, Response } from 'express'; | ||||
| import { createGroup, getAllGroups, getGroup, getGroupSubmissions } from '../services/groups.js'; | ||||
| import { createGroup, deleteGroup, getAllGroups, getGroup, getGroupSubmissions, putGroup } from '../services/groups.js'; | ||||
| import { GroupDTO } from '@dwengo-1/common/interfaces/group'; | ||||
| import { requireFields } from './error-helper.js'; | ||||
| import { BadRequestException } from '../exceptions/bad-request-exception.js'; | ||||
| import { EntityDTO } from '@mikro-orm/core'; | ||||
| import { Group } from '../entities/assignments/group.entity.js'; | ||||
| 
 | ||||
| // Typescript is annoywith with parameter forwarding from class.ts
 | ||||
| interface GroupParams { | ||||
|     classid: string; | ||||
|     assignmentid: string; | ||||
|     groupid?: string; | ||||
| } | ||||
| 
 | ||||
| export async function getGroupHandler(req: Request<GroupParams>, res: Response): Promise<void> { | ||||
|     const classId = req.params.classid; | ||||
|     const full = req.query.full === 'true'; | ||||
|     const assignmentId = Number(req.params.assignmentid); | ||||
| function checkGroupFields(classId: string, assignmentId: number, groupId: number): void { | ||||
|     requireFields({ classId, assignmentId, groupId }); | ||||
| 
 | ||||
|     if (isNaN(assignmentId)) { | ||||
|         res.status(400).json({ error: 'Assignment id must be a number' }); | ||||
|         return; | ||||
|         throw new BadRequestException('Assignment id must be a number'); | ||||
|     } | ||||
| 
 | ||||
|     const groupId = Number(req.params.groupid!); // Can't be undefined
 | ||||
| 
 | ||||
|     if (isNaN(groupId)) { | ||||
|         res.status(400).json({ error: 'Group id must be a number' }); | ||||
|         return; | ||||
|         throw new BadRequestException('Group id must be a number'); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|     const group = await getGroup(classId, assignmentId, groupId, full); | ||||
| export async function getGroupHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.classid; | ||||
|     const assignmentId = parseInt(req.params.assignmentid); | ||||
|     const groupId = parseInt(req.params.groupid); | ||||
|     checkGroupFields(classId, assignmentId, groupId); | ||||
| 
 | ||||
|     if (!group) { | ||||
|         res.status(404).json({ error: 'Group not found' }); | ||||
|         return; | ||||
|     } | ||||
|     const group = await getGroup(classId, assignmentId, groupId); | ||||
| 
 | ||||
|     res.json(group); | ||||
|     res.json({ group }); | ||||
| } | ||||
| 
 | ||||
| export async function putGroupHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.classid; | ||||
|     const assignmentId = parseInt(req.params.assignmentid); | ||||
|     const groupId = parseInt(req.params.groupid); | ||||
|     checkGroupFields(classId, assignmentId, groupId); | ||||
| 
 | ||||
|     const group = await putGroup(classId, assignmentId, groupId, req.body as Partial<EntityDTO<Group>>); | ||||
| 
 | ||||
|     res.json({ group }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteGroupHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.classid; | ||||
|     const assignmentId = parseInt(req.params.assignmentid); | ||||
|     const groupId = parseInt(req.params.groupid); | ||||
|     checkGroupFields(classId, assignmentId, groupId); | ||||
| 
 | ||||
|     const group = await deleteGroup(classId, assignmentId, groupId); | ||||
| 
 | ||||
|     res.json({ group }); | ||||
| } | ||||
| 
 | ||||
| export async function getAllGroupsHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.classid; | ||||
|     const full = req.query.full === 'true'; | ||||
| 
 | ||||
|     const assignmentId = Number(req.params.assignmentid); | ||||
|     const full = req.query.full === 'true'; | ||||
|     requireFields({ classId, assignmentId }); | ||||
| 
 | ||||
|     if (isNaN(assignmentId)) { | ||||
|         res.status(400).json({ error: 'Assignment id must be a number' }); | ||||
|         return; | ||||
|         throw new BadRequestException('Assignment id must be a number'); | ||||
|     } | ||||
| 
 | ||||
|     const groups = await getAllGroups(classId, assignmentId, full); | ||||
| 
 | ||||
|     res.json({ | ||||
|         groups: groups, | ||||
|     }); | ||||
|     res.json({ groups }); | ||||
| } | ||||
| 
 | ||||
| export async function createGroupHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classid = req.params.classid; | ||||
|     const assignmentId = Number(req.params.assignmentid); | ||||
| 
 | ||||
|     requireFields({ classid, assignmentId }); | ||||
| 
 | ||||
|     if (isNaN(assignmentId)) { | ||||
|         res.status(400).json({ error: 'Assignment id must be a number' }); | ||||
|         return; | ||||
|         throw new BadRequestException('Assignment id must be a number'); | ||||
|     } | ||||
| 
 | ||||
|     const groupData = req.body as GroupDTO; | ||||
|     const group = await createGroup(groupData, classid, assignmentId); | ||||
| 
 | ||||
|     if (!group) { | ||||
|         res.status(500).json({ error: 'Something went wrong while creating group' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.status(201).json(group); | ||||
|     res.status(201).json({ group }); | ||||
| } | ||||
| 
 | ||||
| export async function getGroupSubmissionsHandler(req: Request, res: Response): Promise<void> { | ||||
|     const classId = req.params.classid; | ||||
|     const assignmentId = Number(req.params.assignmentid); | ||||
|     const groupId = Number(req.params.groupid); | ||||
|     const full = req.query.full === 'true'; | ||||
| 
 | ||||
|     const assignmentId = Number(req.params.assignmentid); | ||||
|     requireFields({ classId, assignmentId, groupId }); | ||||
| 
 | ||||
|     if (isNaN(assignmentId)) { | ||||
|         res.status(400).json({ error: 'Assignment id must be a number' }); | ||||
|         return; | ||||
|         throw new BadRequestException('Assignment id must be a number'); | ||||
|     } | ||||
| 
 | ||||
|     const groupId = Number(req.params.groupid); // Can't be undefined
 | ||||
| 
 | ||||
|     if (isNaN(groupId)) { | ||||
|         res.status(400).json({ error: 'Group id must be a number' }); | ||||
|         return; | ||||
|         throw new BadRequestException('Group id must be a number'); | ||||
|     } | ||||
| 
 | ||||
|     const submissions = await getGroupSubmissions(classId, assignmentId, groupId, full); | ||||
| 
 | ||||
|     res.json({ | ||||
|         submissions: submissions, | ||||
|     }); | ||||
|     res.json({ submissions }); | ||||
| } | ||||
|  |  | |||
|  | @ -1,61 +1,61 @@ | |||
| import { Request, Response } from 'express'; | ||||
| import { createSubmission, deleteSubmission, getSubmission } from '../services/submissions.js'; | ||||
| import { SubmissionDTO } from '@dwengo-1/common/interfaces/submission'; | ||||
| import { createSubmission, deleteSubmission, getAllSubmissions, getSubmission } from '../services/submissions.js'; | ||||
| import { BadRequestException } from '../exceptions/bad-request-exception.js'; | ||||
| import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | ||||
| import { Language, languageMap } from '@dwengo-1/common/util/language'; | ||||
| import { SubmissionDTO } from '@dwengo-1/common/interfaces/submission'; | ||||
| import { requireFields } from './error-helper.js'; | ||||
| 
 | ||||
| interface SubmissionParams { | ||||
|     hruid: string; | ||||
|     id: number; | ||||
| } | ||||
| 
 | ||||
| export async function getSubmissionHandler(req: Request<SubmissionParams>, res: Response): Promise<void> { | ||||
| export async function getSubmissionHandler(req: Request, res: Response): Promise<void> { | ||||
|     const lohruid = req.params.hruid; | ||||
|     const submissionNumber = Number(req.params.id); | ||||
| 
 | ||||
|     if (isNaN(submissionNumber)) { | ||||
|         res.status(400).json({ error: 'Submission number is not a number' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     const lang = languageMap[req.query.language as string] || Language.Dutch; | ||||
|     const version = (req.query.version || 1) as number; | ||||
|     const submissionNumber = Number(req.params.id); | ||||
|     requireFields({ lohruid, submissionNumber }); | ||||
| 
 | ||||
|     const submission = await getSubmission(lohruid, lang, version, submissionNumber); | ||||
| 
 | ||||
|     if (!submission) { | ||||
|         res.status(404).json({ error: 'Submission not found' }); | ||||
|         return; | ||||
|     if (isNaN(submissionNumber)) { | ||||
|         throw new BadRequestException('Submission number must be a number'); | ||||
|     } | ||||
| 
 | ||||
|     res.json(submission); | ||||
|     const loId = new LearningObjectIdentifier(lohruid, lang, version); | ||||
|     const submission = await getSubmission(loId, submissionNumber); | ||||
| 
 | ||||
|     res.json({ submission }); | ||||
| } | ||||
| 
 | ||||
| export async function getAllSubmissionsHandler(req: Request, res: Response): Promise<void> { | ||||
|     const lohruid = req.params.hruid; | ||||
|     const lang = languageMap[req.query.language as string] || Language.Dutch; | ||||
|     const version = (req.query.version || 1) as number; | ||||
|     requireFields({ lohruid }); | ||||
| 
 | ||||
|     const loId = new LearningObjectIdentifier(lohruid, lang, version); | ||||
|     const submissions = await getAllSubmissions(loId); | ||||
| 
 | ||||
|     res.json({ submissions }); | ||||
| } | ||||
| 
 | ||||
| // TODO: gerald moet nog dingen toevoegen aan de databank voor dat dit gefinaliseerd kan worden
 | ||||
| export async function createSubmissionHandler(req: Request, res: Response): Promise<void> { | ||||
|     const submissionDTO = req.body as SubmissionDTO; | ||||
| 
 | ||||
|     const submission = await createSubmission(submissionDTO); | ||||
| 
 | ||||
|     if (!submission) { | ||||
|         res.status(400).json({ error: 'Failed to create submission' }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.json(submission); | ||||
|     res.json({ submission }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteSubmissionHandler(req: Request, res: Response): Promise<void> { | ||||
|     const hruid = req.params.hruid; | ||||
|     const submissionNumber = Number(req.params.id); | ||||
| 
 | ||||
|     const lang = languageMap[req.query.language as string] || Language.Dutch; | ||||
|     const version = (req.query.version || 1) as number; | ||||
|     const submissionNumber = Number(req.params.id); | ||||
|     requireFields({ hruid, submissionNumber }); | ||||
| 
 | ||||
|     const submission = await deleteSubmission(hruid, lang, version, submissionNumber); | ||||
| 
 | ||||
|     if (!submission) { | ||||
|         res.status(404).json({ error: 'Submission not found' }); | ||||
|         return; | ||||
|     if (isNaN(submissionNumber)) { | ||||
|         throw new BadRequestException('Submission number must be a number'); | ||||
|     } | ||||
| 
 | ||||
|     res.json(submission); | ||||
|     const loId = new LearningObjectIdentifier(hruid, lang, version); | ||||
|     const submission = await deleteSubmission(loId, submissionNumber); | ||||
| 
 | ||||
|     res.json({ submission }); | ||||
| } | ||||
|  |  | |||
|  | @ -81,16 +81,15 @@ export async function getTeacherQuestionHandler(req: Request, res: Response): Pr | |||
| } | ||||
| 
 | ||||
| export async function getStudentJoinRequestHandler(req: Request, res: Response): Promise<void> { | ||||
|     const username = req.query.username as string; | ||||
|     const classId = req.params.classId; | ||||
|     requireFields({ username, classId }); | ||||
|     requireFields({ classId }); | ||||
| 
 | ||||
|     const joinRequests = await getJoinRequestsByClass(classId); | ||||
|     res.json({ joinRequests }); | ||||
| } | ||||
| 
 | ||||
| export async function updateStudentJoinRequestHandler(req: Request, res: Response): Promise<void> { | ||||
|     const studentUsername = req.query.studentUsername as string; | ||||
|     const studentUsername = req.params.studentUsername; | ||||
|     const classId = req.params.classId; | ||||
|     const accepted = req.body.accepted !== 'false'; // Default = true
 | ||||
|     requireFields({ studentUsername, classId }); | ||||
|  |  | |||
|  | @ -17,6 +17,14 @@ export class SubmissionRepository extends DwengoEntityRepository<Submission> { | |||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     public async findByLearningObject(loId: LearningObjectIdentifier): Promise<Submission[]> { | ||||
|         return this.find({ | ||||
|             learningObjectHruid: loId.hruid, | ||||
|             learningObjectLanguage: loId.language, | ||||
|             learningObjectVersion: loId.version, | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     public async findMostRecentSubmissionForStudent(loId: LearningObjectIdentifier, submitter: Student): Promise<Submission | null> { | ||||
|         return this.findOne( | ||||
|             { | ||||
|  |  | |||
|  | @ -3,6 +3,7 @@ import { Question } from '../../entities/questions/question.entity.js'; | |||
| import { LearningObjectIdentifier } from '../../entities/content/learning-object-identifier.js'; | ||||
| import { Student } from '../../entities/users/student.entity.js'; | ||||
| import { LearningObject } from '../../entities/content/learning-object.entity.js'; | ||||
| import { Assignment } from '../../entities/assignments/assignment.entity.js'; | ||||
| import { Loaded } from '@mikro-orm/core'; | ||||
| 
 | ||||
| export class QuestionRepository extends DwengoEntityRepository<Question> { | ||||
|  | @ -56,6 +57,14 @@ export class QuestionRepository extends DwengoEntityRepository<Question> { | |||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     public async findAllByAssignment(assignment: Assignment): Promise<Question[]> { | ||||
|         return this.find({ | ||||
|             author: assignment.groups.flatMap((group) => group.members), | ||||
|             learningObjectHruid: assignment.learningPathHruid, | ||||
|             learningObjectLanguage: assignment.learningPathLanguage, | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     public async findAllByAuthor(author: Student): Promise<Question[]> { | ||||
|         return this.findAll({ | ||||
|             where: { author }, | ||||
|  |  | |||
|  | @ -8,19 +8,18 @@ import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | |||
| export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { | ||||
|     return { | ||||
|         id: assignment.id!, | ||||
|         class: assignment.within.classId!, | ||||
|         within: assignment.within.classId!, | ||||
|         title: assignment.title, | ||||
|         description: assignment.description, | ||||
|         learningPath: assignment.learningPathHruid, | ||||
|         language: assignment.learningPathLanguage, | ||||
|         // Groups: assignment.groups.map(group => group.groupNumber),
 | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | ||||
|     return { | ||||
|         id: assignment.id!, | ||||
|         class: assignment.within.classId!, | ||||
|         within: assignment.within.classId!, | ||||
|         title: assignment.title, | ||||
|         description: assignment.description, | ||||
|         learningPath: assignment.learningPathHruid, | ||||
|  |  | |||
|  | @ -10,7 +10,6 @@ export function mapToClassDTO(cls: Class): ClassDTO { | |||
|         displayName: cls.displayName, | ||||
|         teachers: cls.teachers.map((teacher) => teacher.username), | ||||
|         students: cls.students.map((student) => student.username), | ||||
|         joinRequests: [], // TODO
 | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,11 +1,13 @@ | |||
| import { Group } from '../entities/assignments/group.entity.js'; | ||||
| import { mapToAssignmentDTO } from './assignment.js'; | ||||
| import { mapToClassDTO } from './class.js'; | ||||
| import { mapToStudentDTO } from './student.js'; | ||||
| import { GroupDTO } from '@dwengo-1/common/interfaces/group'; | ||||
| 
 | ||||
| export function mapToGroupDTO(group: Group): GroupDTO { | ||||
|     return { | ||||
|         assignment: mapToAssignmentDTO(group.assignment), // ERROR: , group.assignment.within),
 | ||||
|         class: mapToClassDTO(group.assignment.within), | ||||
|         assignment: mapToAssignmentDTO(group.assignment), | ||||
|         groupNumber: group.groupNumber!, | ||||
|         members: group.members.map(mapToStudentDTO), | ||||
|     }; | ||||
|  | @ -13,6 +15,7 @@ export function mapToGroupDTO(group: Group): GroupDTO { | |||
| 
 | ||||
| export function mapToGroupDTOId(group: Group): GroupDTO { | ||||
|     return { | ||||
|         class: group.assignment.within.classId!, | ||||
|         assignment: group.assignment.id!, | ||||
|         groupNumber: group.groupNumber!, | ||||
|         members: group.members.map((member) => member.username), | ||||
|  |  | |||
|  | @ -1,6 +1,9 @@ | |||
| import { getSubmissionRepository } from '../data/repositories.js'; | ||||
| import { Group } from '../entities/assignments/group.entity.js'; | ||||
| import { Submission } from '../entities/assignments/submission.entity.js'; | ||||
| import { Student } from '../entities/users/student.entity.js'; | ||||
| import { mapToGroupDTO } from './group.js'; | ||||
| import { mapToStudent, mapToStudentDTO } from './student.js'; | ||||
| import { mapToStudentDTO } from './student.js'; | ||||
| import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | ||||
| 
 | ||||
| export function mapToSubmissionDTO(submission: Submission): SubmissionDTO { | ||||
|  | @ -29,17 +32,14 @@ export function mapToSubmissionDTOId(submission: Submission): SubmissionDTOId { | |||
|     }; | ||||
| } | ||||
| 
 | ||||
| export function mapToSubmission(submissionDTO: SubmissionDTO): Submission { | ||||
|     const submission = new Submission(); | ||||
|     submission.learningObjectHruid = submissionDTO.learningObjectIdentifier.hruid; | ||||
|     submission.learningObjectLanguage = submissionDTO.learningObjectIdentifier.language; | ||||
|     submission.learningObjectVersion = submissionDTO.learningObjectIdentifier.version!; | ||||
|     // Submission.submissionNumber = submissionDTO.submissionNumber;
 | ||||
|     submission.submitter = mapToStudent(submissionDTO.submitter); | ||||
|     // Submission.submissionTime = submissionDTO.time;
 | ||||
|     // Submission.onBehalfOf =  submissionDTO.group!;
 | ||||
|     // TODO fix group
 | ||||
|     submission.content = submissionDTO.content; | ||||
| 
 | ||||
|     return submission; | ||||
| export function mapToSubmission(submissionDTO: SubmissionDTO, submitter: Student, onBehalfOf: Group | undefined): Submission { | ||||
|     return getSubmissionRepository().create({ | ||||
|         learningObjectHruid: submissionDTO.learningObjectIdentifier.hruid, | ||||
|         learningObjectLanguage: submissionDTO.learningObjectIdentifier.language, | ||||
|         learningObjectVersion: submissionDTO.learningObjectIdentifier.version || 1, | ||||
|         submitter: submitter, | ||||
|         submissionTime: new Date(), | ||||
|         content: submissionDTO.content, | ||||
|         onBehalfOf: onBehalfOf, | ||||
|     }); | ||||
| } | ||||
|  |  | |||
|  | @ -1,22 +1,26 @@ | |||
| import express from 'express'; | ||||
| import { | ||||
|     createAssignmentHandler, | ||||
|     deleteAssignmentHandler, | ||||
|     getAllAssignmentsHandler, | ||||
|     getAssignmentHandler, | ||||
|     getAssignmentsSubmissionsHandler, | ||||
|     putAssignmentHandler, | ||||
| } from '../controllers/assignments.js'; | ||||
| import groupRouter from './groups.js'; | ||||
| 
 | ||||
| const router = express.Router({ mergeParams: true }); | ||||
| 
 | ||||
| // Root endpoint used to search objects
 | ||||
| router.get('/', getAllAssignmentsHandler); | ||||
| 
 | ||||
| router.post('/', createAssignmentHandler); | ||||
| 
 | ||||
| // Information about an assignment with id 'id'
 | ||||
| router.get('/:id', getAssignmentHandler); | ||||
| 
 | ||||
| router.put('/:id', putAssignmentHandler); | ||||
| 
 | ||||
| router.delete('/:id', deleteAssignmentHandler); | ||||
| 
 | ||||
| router.get('/:id/submissions', getAssignmentsSubmissionsHandler); | ||||
| 
 | ||||
| router.get('/:id/questions', (_req, res) => { | ||||
|  |  | |||
|  | @ -1,10 +1,17 @@ | |||
| import express from 'express'; | ||||
| import { | ||||
|     addClassStudentHandler, | ||||
|     addClassTeacherHandler, | ||||
|     createClassHandler, | ||||
|     deleteClassHandler, | ||||
|     deleteClassStudentHandler, | ||||
|     deleteClassTeacherHandler, | ||||
|     getAllClassesHandler, | ||||
|     getClassHandler, | ||||
|     getClassStudentsHandler, | ||||
|     getClassTeachersHandler, | ||||
|     getTeacherInvitationsHandler, | ||||
|     putClassHandler, | ||||
| } from '../controllers/classes.js'; | ||||
| import assignmentRouter from './assignments.js'; | ||||
| 
 | ||||
|  | @ -15,13 +22,26 @@ router.get('/', getAllClassesHandler); | |||
| 
 | ||||
| router.post('/', createClassHandler); | ||||
| 
 | ||||
| // Information about an class with id 'id'
 | ||||
| router.get('/:id', getClassHandler); | ||||
| 
 | ||||
| router.put('/:id', putClassHandler); | ||||
| 
 | ||||
| router.delete('/:id', deleteClassHandler); | ||||
| 
 | ||||
| router.get('/:id/teacher-invitations', getTeacherInvitationsHandler); | ||||
| 
 | ||||
| router.get('/:id/students', getClassStudentsHandler); | ||||
| 
 | ||||
| router.post('/:id/students', addClassStudentHandler); | ||||
| 
 | ||||
| router.delete('/:id/students/:username', deleteClassStudentHandler); | ||||
| 
 | ||||
| router.get('/:id/teachers', getClassTeachersHandler); | ||||
| 
 | ||||
| router.post('/:id/teachers', addClassTeacherHandler); | ||||
| 
 | ||||
| router.delete('/:id/teachers/:username', deleteClassTeacherHandler); | ||||
| 
 | ||||
| router.use('/:classid/assignments', assignmentRouter); | ||||
| 
 | ||||
| export default router; | ||||
|  |  | |||
|  | @ -1,5 +1,12 @@ | |||
| import express from 'express'; | ||||
| import { createGroupHandler, getAllGroupsHandler, getGroupHandler, getGroupSubmissionsHandler } from '../controllers/groups.js'; | ||||
| import { | ||||
|     createGroupHandler, | ||||
|     deleteGroupHandler, | ||||
|     getAllGroupsHandler, | ||||
|     getGroupHandler, | ||||
|     getGroupSubmissionsHandler, | ||||
|     putGroupHandler, | ||||
| } from '../controllers/groups.js'; | ||||
| 
 | ||||
| const router = express.Router({ mergeParams: true }); | ||||
| 
 | ||||
|  | @ -8,16 +15,12 @@ router.get('/', getAllGroupsHandler); | |||
| 
 | ||||
| router.post('/', createGroupHandler); | ||||
| 
 | ||||
| // Information about a group (members, ... [TODO DOC])
 | ||||
| router.get('/:groupid', getGroupHandler); | ||||
| 
 | ||||
| router.put('/:groupid', putGroupHandler); | ||||
| 
 | ||||
| router.delete('/:groupid', deleteGroupHandler); | ||||
| 
 | ||||
| router.get('/:groupid/submissions', getGroupSubmissionsHandler); | ||||
| 
 | ||||
| // The list of questions a group has made
 | ||||
| router.get('/:id/questions', (_req, res) => { | ||||
|     res.json({ | ||||
|         questions: ['0'], | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
| export default router; | ||||
|  |  | |||
|  | @ -1,13 +1,9 @@ | |||
| import express from 'express'; | ||||
| import { createSubmissionHandler, deleteSubmissionHandler, getSubmissionHandler } from '../controllers/submissions.js'; | ||||
| import { createSubmissionHandler, deleteSubmissionHandler, getAllSubmissionsHandler, getSubmissionHandler } from '../controllers/submissions.js'; | ||||
| const router = express.Router({ mergeParams: true }); | ||||
| 
 | ||||
| // Root endpoint used to search objects
 | ||||
| router.get('/', (_req, res) => { | ||||
|     res.json({ | ||||
|         submissions: ['0', '1'], | ||||
|     }); | ||||
| }); | ||||
| router.get('/', getAllSubmissionsHandler); | ||||
| 
 | ||||
| router.post('/:id', createSubmissionHandler); | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,18 +1,43 @@ | |||
| import { getAssignmentRepository, getClassRepository, getGroupRepository, getSubmissionRepository } from '../data/repositories.js'; | ||||
| import { mapToAssignment, mapToAssignmentDTO, mapToAssignmentDTOId } from '../interfaces/assignment.js'; | ||||
| import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js'; | ||||
| import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | ||||
| import { | ||||
|     getAssignmentRepository, | ||||
|     getClassRepository, | ||||
|     getGroupRepository, | ||||
|     getQuestionRepository, | ||||
|     getSubmissionRepository, | ||||
| } from '../data/repositories.js'; | ||||
| import { Assignment } from '../entities/assignments/assignment.entity.js'; | ||||
| import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||
| import { mapToAssignment, mapToAssignmentDTO, mapToAssignmentDTOId } from '../interfaces/assignment.js'; | ||||
| import { mapToQuestionDTO } from '../interfaces/question.js'; | ||||
| import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js'; | ||||
| import { fetchClass } from './classes.js'; | ||||
| import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | ||||
| import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | ||||
| import { getLogger } from '../logging/initalize.js'; | ||||
| import { EntityDTO } from '@mikro-orm/core'; | ||||
| import { putObject } from './service-helper.js'; | ||||
| 
 | ||||
| export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> { | ||||
| export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classid); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return []; | ||||
|         throw new NotFoundException("Could not find assignment's class"); | ||||
|     } | ||||
| 
 | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         throw new NotFoundException('Could not find assignment'); | ||||
|     } | ||||
| 
 | ||||
|     return assignment; | ||||
| } | ||||
| 
 | ||||
| export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> { | ||||
|     const cls = await fetchClass(classid); | ||||
| 
 | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const assignments = await assignmentRepository.findAllAssignmentsInClass(cls); | ||||
| 
 | ||||
|  | @ -23,42 +48,37 @@ export async function getAllAssignments(classid: string, full: boolean): Promise | |||
|     return assignments.map(mapToAssignmentDTOId); | ||||
| } | ||||
| 
 | ||||
| export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO | null> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classid); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return null; | ||||
|     } | ||||
| export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO> { | ||||
|     const cls = await fetchClass(classid); | ||||
| 
 | ||||
|     const assignment = mapToAssignment(assignmentData, cls); | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
| 
 | ||||
|     try { | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const newAssignment = assignmentRepository.create(assignment); | ||||
|         await assignmentRepository.save(newAssignment); | ||||
|     await assignmentRepository.save(newAssignment, { preventOverwrite: true }); | ||||
| 
 | ||||
|     return mapToAssignmentDTO(newAssignment); | ||||
|     } catch (e) { | ||||
|         getLogger().error(e); | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO | null> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classid); | ||||
| export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO> { | ||||
|     const assignment = await fetchAssignment(classid, id); | ||||
|     return mapToAssignmentDTO(assignment); | ||||
| } | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return null; | ||||
|     } | ||||
| export async function putAssignment(classid: string, id: number, assignmentData: Partial<EntityDTO<Assignment>>): Promise<AssignmentDTO> { | ||||
|     const assignment = await fetchAssignment(classid, id); | ||||
| 
 | ||||
|     await putObject<Assignment>(assignment, assignmentData, getAssignmentRepository()); | ||||
| 
 | ||||
|     return mapToAssignmentDTO(assignment); | ||||
| } | ||||
| 
 | ||||
| export async function deleteAssignment(classid: string, id: number): Promise<AssignmentDTO> { | ||||
|     const assignment = await fetchAssignment(classid, id); | ||||
|     const cls = await fetchClass(classid); | ||||
| 
 | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const assignment = await assignmentRepository.findByClassAndId(cls, id); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         return null; | ||||
|     } | ||||
|     await assignmentRepository.deleteByClassAndId(cls, id); | ||||
| 
 | ||||
|     return mapToAssignmentDTO(assignment); | ||||
| } | ||||
|  | @ -68,19 +88,7 @@ export async function getAssignmentsSubmissions( | |||
|     assignmentNumber: number, | ||||
|     full: boolean | ||||
| ): Promise<SubmissionDTO[] | SubmissionDTOId[]> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classid); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         return []; | ||||
|     } | ||||
|     const assignment = await fetchAssignment(classid, assignmentNumber); | ||||
| 
 | ||||
|     const groupRepository = getGroupRepository(); | ||||
|     const groups = await groupRepository.findAllGroupsForAssignment(assignment); | ||||
|  | @ -94,3 +102,16 @@ export async function getAssignmentsSubmissions( | |||
| 
 | ||||
|     return submissions.map(mapToSubmissionDTOId); | ||||
| } | ||||
| 
 | ||||
| export async function getAssignmentsQuestions(classid: string, assignmentNumber: number, full: boolean): Promise<QuestionDTO[] | QuestionId[]> { | ||||
|     const assignment = await fetchAssignment(classid, assignmentNumber); | ||||
| 
 | ||||
|     const questionRepository = getQuestionRepository(); | ||||
|     const questions = await questionRepository.findAllByAssignment(assignment); | ||||
| 
 | ||||
|     if (full) { | ||||
|         return questions.map(mapToQuestionDTO); | ||||
|     } | ||||
| 
 | ||||
|     return questions.map(mapToQuestionDTO); | ||||
| } | ||||
|  |  | |||
|  | @ -1,22 +1,25 @@ | |||
| import { getClassRepository, getStudentRepository, getTeacherInvitationRepository, getTeacherRepository } from '../data/repositories.js'; | ||||
| import { getClassRepository, getTeacherInvitationRepository } from '../data/repositories.js'; | ||||
| import { mapToClassDTO } from '../interfaces/class.js'; | ||||
| import { mapToStudentDTO } from '../interfaces/student.js'; | ||||
| import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds } from '../interfaces/teacher-invitation.js'; | ||||
| import { getLogger } from '../logging/initalize.js'; | ||||
| import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||
| import { Class } from '../entities/classes/class.entity.js'; | ||||
| import { ClassDTO } from '@dwengo-1/common/interfaces/class'; | ||||
| import { TeacherInvitationDTO } from '@dwengo-1/common/interfaces/teacher-invitation'; | ||||
| import { StudentDTO } from '@dwengo-1/common/interfaces/student'; | ||||
| import { fetchTeacher } from './teachers.js'; | ||||
| import { fetchStudent } from './students.js'; | ||||
| import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; | ||||
| import { mapToTeacherDTO } from '../interfaces/teacher.js'; | ||||
| import { EntityDTO } from '@mikro-orm/core'; | ||||
| import { putObject } from './service-helper.js'; | ||||
| 
 | ||||
| const logger = getLogger(); | ||||
| 
 | ||||
| export async function fetchClass(classId: string): Promise<Class> { | ||||
| export async function fetchClass(classid: string): Promise<Class> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classId); | ||||
|     const cls = await classRepository.findById(classid); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         throw new NotFoundException('Class with id not found'); | ||||
|         throw new NotFoundException('Class not found'); | ||||
|     } | ||||
| 
 | ||||
|     return cls; | ||||
|  | @ -24,11 +27,7 @@ export async function fetchClass(classId: string): Promise<Class> { | |||
| 
 | ||||
| export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[]> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const classes = await classRepository.find({}, { populate: ['students', 'teachers'] }); | ||||
| 
 | ||||
|     if (!classes) { | ||||
|         return []; | ||||
|     } | ||||
|     const classes = await classRepository.findAll({ populate: ['students', 'teachers'] }); | ||||
| 
 | ||||
|     if (full) { | ||||
|         return classes.map(mapToClassDTO); | ||||
|  | @ -36,74 +35,71 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[ | |||
|     return classes.map((cls) => cls.classId!); | ||||
| } | ||||
| 
 | ||||
| export async function createClass(classData: ClassDTO): Promise<ClassDTO | null> { | ||||
|     const teacherRepository = getTeacherRepository(); | ||||
|     const teacherUsernames = classData.teachers || []; | ||||
|     const teachers = (await Promise.all(teacherUsernames.map(async (id) => teacherRepository.findByUsername(id)))).filter( | ||||
|         (teacher) => teacher !== null | ||||
|     ); | ||||
| export async function getClass(classId: string): Promise<ClassDTO> { | ||||
|     const cls = await fetchClass(classId); | ||||
|     return mapToClassDTO(cls); | ||||
| } | ||||
| 
 | ||||
| export async function createClass(classData: ClassDTO): Promise<ClassDTO> { | ||||
|     const teacherUsernames = classData.teachers || []; | ||||
|     const teachers = await Promise.all(teacherUsernames.map(async (id) => fetchTeacher(id))); | ||||
| 
 | ||||
|     const studentRepository = getStudentRepository(); | ||||
|     const studentUsernames = classData.students || []; | ||||
|     const students = (await Promise.all(studentUsernames.map(async (id) => studentRepository.findByUsername(id)))).filter( | ||||
|         (student) => student !== null | ||||
|     ); | ||||
|     const students = await Promise.all(studentUsernames.map(async (id) => fetchStudent(id))); | ||||
| 
 | ||||
|     const classRepository = getClassRepository(); | ||||
| 
 | ||||
|     try { | ||||
|     const newClass = classRepository.create({ | ||||
|         displayName: classData.displayName, | ||||
|         teachers: teachers, | ||||
|         students: students, | ||||
|     }); | ||||
|         await classRepository.save(newClass); | ||||
|     await classRepository.save(newClass, { preventOverwrite: true }); | ||||
| 
 | ||||
|     return mapToClassDTO(newClass); | ||||
|     } catch (e) { | ||||
|         logger.error(e); | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| export async function getClass(classId: string): Promise<ClassDTO | null> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classId); | ||||
| export async function putClass(classId: string, classData: Partial<EntityDTO<Class>>): Promise<ClassDTO> { | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return null; | ||||
|     } | ||||
|     await putObject<Class>(cls, classData, getClassRepository()); | ||||
| 
 | ||||
|     return mapToClassDTO(cls); | ||||
| } | ||||
| 
 | ||||
| async function fetchClassStudents(classId: string): Promise<StudentDTO[]> { | ||||
| export async function deleteClass(classId: string): Promise<ClassDTO> { | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classId); | ||||
|     await classRepository.deleteById(classId); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return []; | ||||
|     return mapToClassDTO(cls); | ||||
| } | ||||
| 
 | ||||
| export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> { | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     if (full) { | ||||
|         return cls.students.map(mapToStudentDTO); | ||||
|     } | ||||
|     return cls.students.map((student) => student.username); | ||||
| } | ||||
| 
 | ||||
| export async function getClassStudentsDTO(classId: string): Promise<StudentDTO[]> { | ||||
|     const cls = await fetchClass(classId); | ||||
|     return cls.students.map(mapToStudentDTO); | ||||
| } | ||||
| 
 | ||||
| export async function getClassStudents(classId: string): Promise<StudentDTO[]> { | ||||
|     return await fetchClassStudents(classId); | ||||
| } | ||||
| export async function getClassTeachers(classId: string, full: boolean): Promise<TeacherDTO[] | string[]> { | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
| export async function getClassStudentsIds(classId: string): Promise<string[]> { | ||||
|     const students: StudentDTO[] = await fetchClassStudents(classId); | ||||
|     return students.map((student) => student.username); | ||||
|     if (full) { | ||||
|         return cls.teachers.map(mapToTeacherDTO); | ||||
|     } | ||||
|     return cls.teachers.map((student) => student.username); | ||||
| } | ||||
| 
 | ||||
| export async function getClassTeacherInvitations(classId: string, full: boolean): Promise<TeacherInvitationDTO[]> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classId); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return []; | ||||
|     } | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     const teacherInvitationRepository = getTeacherInvitationRepository(); | ||||
|     const invitations = await teacherInvitationRepository.findAllInvitationsForClass(cls); | ||||
|  | @ -114,3 +110,41 @@ export async function getClassTeacherInvitations(classId: string, full: boolean) | |||
| 
 | ||||
|     return invitations.map(mapToTeacherInvitationDTOIds); | ||||
| } | ||||
| 
 | ||||
| export async function deleteClassStudent(classId: string, username: string): Promise<ClassDTO> { | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     const newStudents = { students: cls.students.filter((student) => student.username !== username) }; | ||||
|     await putObject<Class>(cls, newStudents, getClassRepository()); | ||||
| 
 | ||||
|     return mapToClassDTO(cls); | ||||
| } | ||||
| 
 | ||||
| export async function deleteClassTeacher(classId: string, username: string): Promise<ClassDTO> { | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     const newTeachers = { teachers: cls.teachers.filter((teacher) => teacher.username !== username) }; | ||||
|     await putObject<Class>(cls, newTeachers, getClassRepository()); | ||||
| 
 | ||||
|     return mapToClassDTO(cls); | ||||
| } | ||||
| 
 | ||||
| export async function addClassStudent(classId: string, username: string): Promise<ClassDTO> { | ||||
|     const cls = await fetchClass(classId); | ||||
|     const newStudent = await fetchStudent(username); | ||||
| 
 | ||||
|     const newStudents = { students: [...cls.students, newStudent] }; | ||||
|     await putObject<Class>(cls, newStudents, getClassRepository()); | ||||
| 
 | ||||
|     return mapToClassDTO(cls); | ||||
| } | ||||
| 
 | ||||
| export async function addClassTeacher(classId: string, username: string): Promise<ClassDTO> { | ||||
|     const cls = await fetchClass(classId); | ||||
|     const newTeacher = await fetchTeacher(username); | ||||
| 
 | ||||
|     const newTeachers = { teachers: [...cls.teachers, newTeacher] }; | ||||
|     await putObject<Class>(cls, newTeachers, getClassRepository()); | ||||
| 
 | ||||
|     return mapToClassDTO(cls); | ||||
| } | ||||
|  |  | |||
|  | @ -1,105 +1,90 @@ | |||
| import { | ||||
|     getAssignmentRepository, | ||||
|     getClassRepository, | ||||
|     getGroupRepository, | ||||
|     getStudentRepository, | ||||
|     getSubmissionRepository, | ||||
| } from '../data/repositories.js'; | ||||
| import { EntityDTO } from '@mikro-orm/core'; | ||||
| import { getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js'; | ||||
| import { Group } from '../entities/assignments/group.entity.js'; | ||||
| import { mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; | ||||
| import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js'; | ||||
| import { GroupDTO } from '@dwengo-1/common/interfaces/group'; | ||||
| import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | ||||
| import { getLogger } from '../logging/initalize.js'; | ||||
| import { fetchAssignment } from './assignments.js'; | ||||
| import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||
| import { putObject } from './service-helper.js'; | ||||
| 
 | ||||
| export async function getGroup(classId: string, assignmentNumber: number, groupNumber: number, full: boolean): Promise<GroupDTO | null> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classId); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         return null; | ||||
|     } | ||||
| export async function fetchGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<Group> { | ||||
|     const assignment = await fetchAssignment(classId, assignmentNumber); | ||||
| 
 | ||||
|     const groupRepository = getGroupRepository(); | ||||
|     const group = await groupRepository.findByAssignmentAndGroupNumber(assignment, groupNumber); | ||||
| 
 | ||||
|     if (!group) { | ||||
|         return null; | ||||
|         throw new NotFoundException('Could not find group'); | ||||
|     } | ||||
| 
 | ||||
|     if (full) { | ||||
|         return mapToGroupDTO(group); | ||||
|     } | ||||
| 
 | ||||
|     return mapToGroupDTOId(group); | ||||
|     return group; | ||||
| } | ||||
| 
 | ||||
| export async function createGroup(groupData: GroupDTO, classid: string, assignmentNumber: number): Promise<Group | null> { | ||||
| export async function getGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<GroupDTO> { | ||||
|     const group = await fetchGroup(classId, assignmentNumber, groupNumber); | ||||
|     return mapToGroupDTO(group); | ||||
| } | ||||
| 
 | ||||
| export async function putGroup( | ||||
|     classId: string, | ||||
|     assignmentNumber: number, | ||||
|     groupNumber: number, | ||||
|     groupData: Partial<EntityDTO<Group>> | ||||
| ): Promise<GroupDTO> { | ||||
|     const group = await fetchGroup(classId, assignmentNumber, groupNumber); | ||||
| 
 | ||||
|     await putObject<Group>(group, groupData, getGroupRepository()); | ||||
| 
 | ||||
|     return mapToGroupDTO(group); | ||||
| } | ||||
| 
 | ||||
| export async function deleteGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<GroupDTO> { | ||||
|     const group = await fetchGroup(classId, assignmentNumber, groupNumber); | ||||
|     const assignment = await fetchAssignment(classId, assignmentNumber); | ||||
| 
 | ||||
|     const groupRepository = getGroupRepository(); | ||||
|     await groupRepository.deleteByAssignmentAndGroupNumber(assignment, groupNumber); | ||||
| 
 | ||||
|     return mapToGroupDTO(group); | ||||
| } | ||||
| 
 | ||||
| export async function getExistingGroupFromGroupDTO(groupData: GroupDTO): Promise<Group> { | ||||
|     const classId = typeof groupData.class === 'string' ? groupData.class : groupData.class.id; | ||||
|     const assignmentNumber = typeof groupData.assignment === 'number' ? groupData.assignment : groupData.assignment.id; | ||||
|     const groupNumber = groupData.groupNumber; | ||||
| 
 | ||||
|     return await fetchGroup(classId, assignmentNumber, groupNumber); | ||||
| } | ||||
| 
 | ||||
| export async function createGroup(groupData: GroupDTO, classid: string, assignmentNumber: number): Promise<GroupDTO> { | ||||
|     const studentRepository = getStudentRepository(); | ||||
| 
 | ||||
|     const memberUsernames = (groupData.members as string[]) || []; // TODO check if groupdata.members is a list
 | ||||
|     const memberUsernames = (groupData.members as string[]) || []; | ||||
|     const members = (await Promise.all([...memberUsernames].map(async (id) => studentRepository.findByUsername(id)))).filter( | ||||
|         (student) => student !== null | ||||
|     ); | ||||
| 
 | ||||
|     getLogger().debug(members); | ||||
| 
 | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classid); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         return null; | ||||
|     } | ||||
|     const assignment = await fetchAssignment(classid, assignmentNumber); | ||||
| 
 | ||||
|     const groupRepository = getGroupRepository(); | ||||
|     try { | ||||
|     const newGroup = groupRepository.create({ | ||||
|         assignment: assignment, | ||||
|         members: members, | ||||
|     }); | ||||
|     await groupRepository.save(newGroup); | ||||
| 
 | ||||
|         return newGroup; | ||||
|     } catch (e) { | ||||
|         getLogger().error(e); | ||||
|         return null; | ||||
|     } | ||||
|     return mapToGroupDTO(newGroup); | ||||
| } | ||||
| 
 | ||||
| export async function getAllGroups(classId: string, assignmentNumber: number, full: boolean): Promise<GroupDTO[]> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classId); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         return []; | ||||
|     } | ||||
|     const assignment = await fetchAssignment(classId, assignmentNumber); | ||||
| 
 | ||||
|     const groupRepository = getGroupRepository(); | ||||
|     const groups = await groupRepository.findAllGroupsForAssignment(assignment); | ||||
| 
 | ||||
|     if (full) { | ||||
|         getLogger().debug({ full: full, groups: groups }); | ||||
|         return groups.map(mapToGroupDTO); | ||||
|     } | ||||
| 
 | ||||
|  | @ -112,26 +97,7 @@ export async function getGroupSubmissions( | |||
|     groupNumber: number, | ||||
|     full: boolean | ||||
| ): Promise<SubmissionDTO[] | SubmissionDTOId[]> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classId); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
|     const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     const groupRepository = getGroupRepository(); | ||||
|     const group = await groupRepository.findByAssignmentAndGroupNumber(assignment, groupNumber); | ||||
| 
 | ||||
|     if (!group) { | ||||
|         return []; | ||||
|     } | ||||
|     const group = await fetchGroup(classId, assignmentNumber, groupNumber); | ||||
| 
 | ||||
|     const submissionRepository = getSubmissionRepository(); | ||||
|     const submissions = await submissionRepository.findAllSubmissionsForGroup(group); | ||||
|  |  | |||
							
								
								
									
										20
									
								
								backend/src/services/service-helper.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								backend/src/services/service-helper.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | |||
| import { EntityDTO, FromEntityType } from '@mikro-orm/core'; | ||||
| import { DwengoEntityRepository } from '../data/dwengo-entity-repository'; | ||||
| 
 | ||||
| /** | ||||
|  * Utility function to perform an PUT on an object. | ||||
|  * | ||||
|  * @param object The object that needs to be changed | ||||
|  * @param data The datafields and their values that will be updated | ||||
|  * @param repo The repository on which this action needs to be performed | ||||
|  * | ||||
|  * @returns Nothing. | ||||
|  */ | ||||
| export async function putObject<T extends object>( | ||||
|     object: T, | ||||
|     data: Partial<EntityDTO<FromEntityType<T>>>, | ||||
|     repo: DwengoEntityRepository<T> | ||||
| ): Promise<void> { | ||||
|     repo.assign(object, data); | ||||
|     await repo.getEntityManager().flush(); | ||||
| } | ||||
|  | @ -23,6 +23,7 @@ import { GroupDTO } from '@dwengo-1/common/interfaces/group'; | |||
| import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | ||||
| import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | ||||
| import { ClassJoinRequestDTO } from '@dwengo-1/common/interfaces/class-join-request'; | ||||
| import { ConflictException } from '../exceptions/conflict-exception.js'; | ||||
| 
 | ||||
| export async function getAllStudents(full: boolean): Promise<StudentDTO[] | string[]> { | ||||
|     const studentRepository = getStudentRepository(); | ||||
|  | @ -135,6 +136,10 @@ export async function createClassJoinRequest(username: string, classId: string): | |||
|     const student = await fetchStudent(username); // Throws error if student not found
 | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     if (cls.students.contains(student)) { | ||||
|         throw new ConflictException('Student already in this class'); | ||||
|     } | ||||
| 
 | ||||
|     const request = mapToStudentRequest(student, cls); | ||||
|     await requestRepo.save(request, { preventOverwrite: true }); | ||||
|     return mapToStudentRequestDTO(request); | ||||
|  |  | |||
|  | @ -1,57 +1,51 @@ | |||
| import { getSubmissionRepository } from '../data/repositories.js'; | ||||
| import { LearningObjectIdentifier } from '../entities/content/learning-object-identifier.js'; | ||||
| import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||
| import { mapToSubmission, mapToSubmissionDTO } from '../interfaces/submission.js'; | ||||
| import { SubmissionDTO } from '@dwengo-1/common/interfaces/submission'; | ||||
| import { Language } from '@dwengo-1/common/util/language'; | ||||
| 
 | ||||
| export async function getSubmission( | ||||
|     learningObjectHruid: string, | ||||
|     language: Language, | ||||
|     version: number, | ||||
|     submissionNumber: number | ||||
| ): Promise<SubmissionDTO | null> { | ||||
|     const loId = new LearningObjectIdentifier(learningObjectHruid, language, version); | ||||
| import { fetchStudent } from './students.js'; | ||||
| import { getExistingGroupFromGroupDTO } from './groups.js'; | ||||
| import { Submission } from '../entities/assignments/submission.entity.js'; | ||||
| 
 | ||||
| export async function fetchSubmission(loId: LearningObjectIdentifier, submissionNumber: number): Promise<Submission> { | ||||
|     const submissionRepository = getSubmissionRepository(); | ||||
|     const submission = await submissionRepository.findSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber); | ||||
| 
 | ||||
|     if (!submission) { | ||||
|         return null; | ||||
|         throw new NotFoundException('Could not find submission'); | ||||
|     } | ||||
| 
 | ||||
|     return mapToSubmissionDTO(submission); | ||||
| } | ||||
| 
 | ||||
| export async function createSubmission(submissionDTO: SubmissionDTO): Promise<SubmissionDTO | null> { | ||||
|     const submissionRepository = getSubmissionRepository(); | ||||
|     const submission = mapToSubmission(submissionDTO); | ||||
| 
 | ||||
|     try { | ||||
|         const newSubmission = submissionRepository.create(submission); | ||||
|         await submissionRepository.save(newSubmission); | ||||
|     } catch (_) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     return mapToSubmissionDTO(submission); | ||||
| } | ||||
| 
 | ||||
| export async function deleteSubmission( | ||||
|     learningObjectHruid: string, | ||||
|     language: Language, | ||||
|     version: number, | ||||
|     submissionNumber: number | ||||
| ): Promise<SubmissionDTO | null> { | ||||
|     const submissionRepository = getSubmissionRepository(); | ||||
| 
 | ||||
|     const submission = getSubmission(learningObjectHruid, language, version, submissionNumber); | ||||
| 
 | ||||
|     if (!submission) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     const loId = new LearningObjectIdentifier(learningObjectHruid, language, version); | ||||
|     await submissionRepository.deleteSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber); | ||||
| 
 | ||||
|     return submission; | ||||
| } | ||||
| 
 | ||||
| export async function getSubmission(loId: LearningObjectIdentifier, submissionNumber: number): Promise<SubmissionDTO> { | ||||
|     const submission = await fetchSubmission(loId, submissionNumber); | ||||
|     return mapToSubmissionDTO(submission); | ||||
| } | ||||
| 
 | ||||
| export async function getAllSubmissions(loId: LearningObjectIdentifier): Promise<SubmissionDTO[]> { | ||||
|     const submissionRepository = getSubmissionRepository(); | ||||
|     const submissions = await submissionRepository.findByLearningObject(loId); | ||||
| 
 | ||||
|     return submissions.map(mapToSubmissionDTO); | ||||
| } | ||||
| 
 | ||||
| export async function createSubmission(submissionDTO: SubmissionDTO): Promise<SubmissionDTO> { | ||||
|     const submitter = await fetchStudent(submissionDTO.submitter.username); | ||||
|     const group = submissionDTO.group ? await getExistingGroupFromGroupDTO(submissionDTO.group) : undefined; | ||||
| 
 | ||||
|     const submissionRepository = getSubmissionRepository(); | ||||
|     const submission = mapToSubmission(submissionDTO, submitter, group); | ||||
|     await submissionRepository.save(submission); | ||||
| 
 | ||||
|     return mapToSubmissionDTO(submission); | ||||
| } | ||||
| 
 | ||||
| export async function deleteSubmission(loId: LearningObjectIdentifier, submissionNumber: number): Promise<SubmissionDTO> { | ||||
|     const submission = await fetchSubmission(loId, submissionNumber); | ||||
| 
 | ||||
|     const submissionRepository = getSubmissionRepository(); | ||||
|     await submissionRepository.deleteSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber); | ||||
| 
 | ||||
|     return mapToSubmissionDTO(submission); | ||||
| } | ||||
|  |  | |||
|  | @ -22,13 +22,14 @@ import { Question } from '../entities/questions/question.entity.js'; | |||
| import { ClassJoinRequestRepository } from '../data/classes/class-join-request-repository.js'; | ||||
| import { Student } from '../entities/users/student.entity.js'; | ||||
| import { NotFoundException } from '../exceptions/not-found-exception.js'; | ||||
| import { getClassStudents } from './classes.js'; | ||||
| import { addClassStudent, fetchClass, getClassStudentsDTO } from './classes.js'; | ||||
| import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; | ||||
| import { ClassDTO } from '@dwengo-1/common/interfaces/class'; | ||||
| import { StudentDTO } from '@dwengo-1/common/interfaces/student'; | ||||
| import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | ||||
| import { ClassJoinRequestDTO } from '@dwengo-1/common/interfaces/class-join-request'; | ||||
| import { ClassJoinRequestStatus } from '@dwengo-1/common/util/class-join-request'; | ||||
| import { ConflictException } from '../exceptions/conflict-exception.js'; | ||||
| 
 | ||||
| export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> { | ||||
|     const teacherRepository: TeacherRepository = getTeacherRepository(); | ||||
|  | @ -99,10 +100,12 @@ export async function getStudentsByTeacher(username: string, full: boolean): Pro | |||
| 
 | ||||
|     const classIds: string[] = classes.map((cls) => cls.id); | ||||
| 
 | ||||
|     const students: StudentDTO[] = (await Promise.all(classIds.map(async (id) => getClassStudents(id)))).flat(); | ||||
|     const students: StudentDTO[] = (await Promise.all(classIds.map(async (username) => await getClassStudentsDTO(username)))).flat(); | ||||
| 
 | ||||
|     if (full) { | ||||
|         return students; | ||||
|     } | ||||
| 
 | ||||
|     return students.map((student) => student.username); | ||||
| } | ||||
| 
 | ||||
|  | @ -143,13 +146,12 @@ export async function getJoinRequestsByClass(classId: string): Promise<ClassJoin | |||
| 
 | ||||
| export async function updateClassJoinRequestStatus(studentUsername: string, classId: string, accepted = true): Promise<ClassJoinRequestDTO> { | ||||
|     const requestRepo: ClassJoinRequestRepository = getClassJoinRequestRepository(); | ||||
|     const classRepo: ClassRepository = getClassRepository(); | ||||
| 
 | ||||
|     const student: Student = await fetchStudent(studentUsername); | ||||
|     const cls: Class | null = await classRepo.findById(classId); | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     if (!cls) { | ||||
|         throw new NotFoundException('Class not found'); | ||||
|     if (cls.students.contains(student)) { | ||||
|         throw new ConflictException('Student already in this class'); | ||||
|     } | ||||
| 
 | ||||
|     const request: ClassJoinRequest | null = await requestRepo.findByStudentAndClass(student, cls); | ||||
|  | @ -158,8 +160,14 @@ export async function updateClassJoinRequestStatus(studentUsername: string, clas | |||
|         throw new NotFoundException('Join request not found'); | ||||
|     } | ||||
| 
 | ||||
|     request.status = accepted ? ClassJoinRequestStatus.Accepted : ClassJoinRequestStatus.Declined; | ||||
|     request.status = ClassJoinRequestStatus.Declined; | ||||
| 
 | ||||
|     if (accepted) { | ||||
|         request.status = ClassJoinRequestStatus.Accepted; | ||||
|         await addClassStudent(classId, studentUsername); | ||||
|     } | ||||
| 
 | ||||
|     await requestRepo.save(request); | ||||
| 
 | ||||
|     return mapToStudentRequestDTO(request); | ||||
| } | ||||
|  |  | |||
|  | @ -198,15 +198,34 @@ describe('Student controllers', () => { | |||
|         ); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create join request', async () => { | ||||
|     it('Create and delete join request', async () => { | ||||
|         req = { | ||||
|             params: { username: 'Noordkaap' }, | ||||
|             params: { username: 'TheDoors' }, | ||||
|             body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, | ||||
|         }; | ||||
| 
 | ||||
|         await createStudentRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { username: 'TheDoors', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, | ||||
|         }; | ||||
| 
 | ||||
|         await deleteClassJoinRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); | ||||
| 
 | ||||
|         await expect(async () => deleteClassJoinRequestHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create join request student already in class error', async () => { | ||||
|         req = { | ||||
|             params: { username: 'Noordkaap' }, | ||||
|             body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, | ||||
|         }; | ||||
| 
 | ||||
|         await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Create join request duplicate', async () => { | ||||
|  | @ -217,16 +236,4 @@ describe('Student controllers', () => { | |||
| 
 | ||||
|         await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException); | ||||
|     }); | ||||
| 
 | ||||
|     it('Delete join request', async () => { | ||||
|         req = { | ||||
|             params: { username: 'Noordkaap', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, | ||||
|         }; | ||||
| 
 | ||||
|         await deleteClassJoinRequestHandler(req as Request, res as Response); | ||||
| 
 | ||||
|         expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() })); | ||||
| 
 | ||||
|         await expect(async () => deleteClassJoinRequestHandler(req as Request, res as Response)).rejects.toThrow(NotFoundException); | ||||
|     }); | ||||
| }); | ||||
|  |  | |||
|  | @ -16,6 +16,7 @@ import { BadRequestException } from '../../src/exceptions/bad-request-exception. | |||
| import { EntityAlreadyExistsException } from '../../src/exceptions/entity-already-exists-exception.js'; | ||||
| import { getStudentRequestsHandler } from '../../src/controllers/students.js'; | ||||
| import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher'; | ||||
| import { getClassHandler } from '../../src/controllers/classes'; | ||||
| 
 | ||||
| describe('Teacher controllers', () => { | ||||
|     let req: Partial<Request>; | ||||
|  | @ -168,7 +169,6 @@ describe('Teacher controllers', () => { | |||
| 
 | ||||
|     it('Get join requests by class', async () => { | ||||
|         req = { | ||||
|             query: { username: 'LimpBizkit' }, | ||||
|             params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, | ||||
|         }; | ||||
| 
 | ||||
|  | @ -183,8 +183,7 @@ describe('Teacher controllers', () => { | |||
| 
 | ||||
|     it('Update join request status', async () => { | ||||
|         req = { | ||||
|             query: { username: 'LimpBizkit', studentUsername: 'PinkFloyd' }, | ||||
|             params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, | ||||
|             params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', studentUsername: 'PinkFloyd' }, | ||||
|             body: { accepted: 'true' }, | ||||
|         }; | ||||
| 
 | ||||
|  | @ -200,5 +199,13 @@ describe('Teacher controllers', () => { | |||
| 
 | ||||
|         const status: boolean = jsonMock.mock.lastCall?.[0].requests[0].status; | ||||
|         expect(status).toBeTruthy(); | ||||
| 
 | ||||
|         req = { | ||||
|             params: { id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' }, | ||||
|         }; | ||||
| 
 | ||||
|         await getClassHandler(req as Request, res as Response); | ||||
|         const students: string[] = jsonMock.mock.lastCall?.[0].class.students; | ||||
|         expect(students).contains('PinkFloyd'); | ||||
|     }); | ||||
| }); | ||||
|  |  | |||
|  | @ -2,7 +2,7 @@ import { GroupDTO } from './group'; | |||
| 
 | ||||
| export interface AssignmentDTO { | ||||
|     id: number; | ||||
|     class: string; // Id of class 'within'
 | ||||
|     within: string; | ||||
|     title: string; | ||||
|     description: string; | ||||
|     learningPath: string; | ||||
|  |  | |||
|  | @ -3,5 +3,4 @@ export interface ClassDTO { | |||
|     displayName: string; | ||||
|     teachers: string[]; | ||||
|     students: string[]; | ||||
|     joinRequests: string[]; | ||||
| } | ||||
|  |  | |||
|  | @ -1,7 +1,9 @@ | |||
| import { AssignmentDTO } from './assignment'; | ||||
| import { ClassDTO } from './class'; | ||||
| import { StudentDTO } from './student'; | ||||
| 
 | ||||
| export interface GroupDTO { | ||||
|     class: string | ClassDTO; | ||||
|     assignment: number | AssignmentDTO; | ||||
|     groupNumber: number; | ||||
|     members: string[] | StudentDTO[]; | ||||
|  |  | |||
|  | @ -33,6 +33,10 @@ export class AssignmentController extends BaseController { | |||
|         return this.delete<AssignmentResponse>(`/${num}`); | ||||
|     } | ||||
| 
 | ||||
|     async updateAssignment(num: number, data: Partial<AssignmentDTO>): Promise<AssignmentResponse> { | ||||
|         return this.put<AssignmentResponse>(`/${num}`, data); | ||||
|     } | ||||
| 
 | ||||
|     async getSubmissions(assignmentNumber: number, full = true): Promise<SubmissionsResponse> { | ||||
|         return this.get<SubmissionsResponse>(`/${assignmentNumber}/submissions`, { full }); | ||||
|     } | ||||
|  |  | |||
|  | @ -3,6 +3,7 @@ import type { ClassDTO } from "@dwengo-1/common/interfaces/class"; | |||
| import type { StudentsResponse } from "./students"; | ||||
| import type { AssignmentsResponse } from "./assignments"; | ||||
| import type { TeacherInvitationDTO } from "@dwengo-1/common/interfaces/teacher-invitation"; | ||||
| import type { TeachersResponse } from "@/controllers/teachers.ts"; | ||||
| 
 | ||||
| export interface ClassesResponse { | ||||
|     classes: ClassDTO[] | string[]; | ||||
|  | @ -41,10 +42,34 @@ export class ClassController extends BaseController { | |||
|         return this.delete<ClassResponse>(`/${id}`); | ||||
|     } | ||||
| 
 | ||||
|     async updateClass(id: string, data: Partial<ClassDTO>): Promise<ClassResponse> { | ||||
|         return this.put<ClassResponse>(`/${id}`, data); | ||||
|     } | ||||
| 
 | ||||
|     async getStudents(id: string, full = true): Promise<StudentsResponse> { | ||||
|         return this.get<StudentsResponse>(`/${id}/students`, { full }); | ||||
|     } | ||||
| 
 | ||||
|     async addStudent(id: string, username: string): Promise<ClassResponse> { | ||||
|         return this.post<ClassResponse>(`/${id}/students`, { username }); | ||||
|     } | ||||
| 
 | ||||
|     async deleteStudent(id: string, username: string): Promise<ClassResponse> { | ||||
|         return this.delete<ClassResponse>(`/${id}/students/${username}`); | ||||
|     } | ||||
| 
 | ||||
|     async getTeachers(id: string, full = true): Promise<TeachersResponse> { | ||||
|         return this.get<TeachersResponse>(`/${id}/teachers`, { full }); | ||||
|     } | ||||
| 
 | ||||
|     async addTeacher(id: string, username: string): Promise<ClassResponse> { | ||||
|         return this.post<ClassResponse>(`/${id}/teachers`, { username }); | ||||
|     } | ||||
| 
 | ||||
|     async deleteTeacher(id: string, username: string): Promise<ClassResponse> { | ||||
|         return this.delete<ClassResponse>(`/${id}/teachers/${username}`); | ||||
|     } | ||||
| 
 | ||||
|     async getTeacherInvitations(id: string, full = true): Promise<TeacherInvitationsResponse> { | ||||
|         return this.get<TeacherInvitationsResponse>(`/${id}/teacher-invitations`, { full }); | ||||
|     } | ||||
|  |  | |||
|  | @ -32,6 +32,10 @@ export class GroupController extends BaseController { | |||
|         return this.delete<GroupResponse>(`/${num}`); | ||||
|     } | ||||
| 
 | ||||
|     async updateGroup(num: number, data: Partial<GroupDTO>): Promise<GroupResponse> { | ||||
|         return this.put<GroupResponse>(`/${num}`, data); | ||||
|     } | ||||
| 
 | ||||
|     async getSubmissions(groupNumber: number, full = true): Promise<SubmissionsResponse> { | ||||
|         return this.get<SubmissionsResponse>(`/${groupNumber}/submissions`, { full }); | ||||
|     } | ||||
|  |  | |||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl