diff --git a/backend/src/app.ts b/backend/src/app.ts index b21bb9f1..e4da44e7 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -8,7 +8,6 @@ import learningObjectRoutes from './routes/learningObjects.js'; import studentRouter from './routes/student.js'; import groupRouter from './routes/group.js'; -import assignmentRouter from './routes/assignment.js'; import submissionRouter from './routes/submission.js'; import classRouter from './routes/class.js'; import questionRouter from './routes/question.js'; @@ -27,7 +26,6 @@ app.get('/', (_, res: Response) => { app.use('/student', studentRouter); app.use('/group', groupRouter); -app.use('/assignment', assignmentRouter); app.use('/submission', submissionRouter); app.use('/class', classRouter); app.use('/question', questionRouter); diff --git a/backend/src/controllers/assignments.ts b/backend/src/controllers/assignments.ts new file mode 100644 index 00000000..775a60df --- /dev/null +++ b/backend/src/controllers/assignments.ts @@ -0,0 +1,30 @@ +import { Request, Response } from 'express' +import { getAssignment } from '../services/assignments'; + +// typescript is annoywith with parameter forwarding from class.ts +interface AssignmentParams { + classid: string; + id: string; +} + +export async function getAssignmentHandler( + req: Request, + res: Response, +): Promise { + const id = +req.params.id; + const classid = req.params.classid; + + if (isNaN(id)) { + res.status(400).json({ error: "Assignment id must be a number" }); + return; + } + + const assignment = await getAssignment(classid, id); + + if (!assignment) { + res.status(404).json({ error: "Assignment not found" }); + return; + } + + res.json(assignment); +} \ No newline at end of file diff --git a/backend/src/interfaces/assignments.ts b/backend/src/interfaces/assignments.ts new file mode 100644 index 00000000..a3c3c4cc --- /dev/null +++ b/backend/src/interfaces/assignments.ts @@ -0,0 +1,25 @@ +import { Assignment } from "../entities/assignments/assignment.entity"; +import { Class } from "../entities/classes/class.entity"; +import { GroupDTO } from "./groups"; + +export interface AssignmentDTO { + id: number, + class: string, // id of class 'within' + title: string, + description: string, + learningPath: string, + language: string, + groups?: GroupDTO[], // TODO +} + +export function mapToAssignmentDTO(assignment: Assignment, cls: Class): AssignmentDTO { + return { + id: assignment.id, + class: cls.classId, + title: assignment.title, + description: assignment.description, + learningPath: assignment.learningPathHruid, + language: assignment.learningPathLanguage, + //groups: assignment.groups.map(mapToGroupDTO), + }; +} \ No newline at end of file diff --git a/backend/src/interfaces/groups.ts b/backend/src/interfaces/groups.ts new file mode 100644 index 00000000..6056a906 --- /dev/null +++ b/backend/src/interfaces/groups.ts @@ -0,0 +1,3 @@ +export interface GroupDTO { + groupNumber: number, +} \ No newline at end of file diff --git a/backend/src/interfaces/list.ts b/backend/src/interfaces/list.ts new file mode 100644 index 00000000..0037403d --- /dev/null +++ b/backend/src/interfaces/list.ts @@ -0,0 +1,5 @@ +// TODO: implement something like this but with named endpoints +export interface List { + items: T[], + endpoints?: string[], +}; \ No newline at end of file diff --git a/backend/src/routes/assignment.ts b/backend/src/routes/assignment.ts index fcb6e9da..e9c91160 100644 --- a/backend/src/routes/assignment.ts +++ b/backend/src/routes/assignment.ts @@ -1,5 +1,8 @@ import express from 'express' -const router = express.Router(); +import { getAssignmentHandler } from '../controllers/assignments'; +const router = express.Router({ mergeParams: true }); + + // root endpoint used to search objects router.get('/', (req, res) => { @@ -12,20 +15,7 @@ router.get('/', (req, res) => { }); // information about an assignment with id 'id' -router.get('/:id', (req, res) => { - res.json({ - id: req.params.id, - title: 'Dit is een test assignment', - description: 'Een korte beschrijving', - groups: [ '0' ], - learningPath: '0', - class: '0', - links: { - self: `${req.baseUrl}/${req.params.id}`, - submissions: `${req.baseUrl}/${req.params.id}`, - }, - }); -}) +router.get('/:id', getAssignmentHandler); router.get('/:id/submissions', (req, res) => { res.json({ diff --git a/backend/src/routes/class.ts b/backend/src/routes/class.ts index e58547b2..bc502fb8 100644 --- a/backend/src/routes/class.ts +++ b/backend/src/routes/class.ts @@ -1,5 +1,7 @@ import express from 'express' import { getAllClassesHandler, getClassHandler, getClassStudentsHandler } from '../controllers/classes'; +import assignmentRouter from './assignment.js'; + const router = express.Router(); // root endpoint used to search objects @@ -16,14 +18,8 @@ router.get('/:id/invitations', (req, res) => { }); }) -router.get('/:id/assignments', (req, res) => { - res.json({ - assignments: [ - '0' - ], - }); -}) - router.get('/:id/students', getClassStudentsHandler); +router.use('/:classid/assignments', assignmentRouter); + export default router \ No newline at end of file diff --git a/backend/src/services/assignments.ts b/backend/src/services/assignments.ts new file mode 100644 index 00000000..ab76f6a8 --- /dev/null +++ b/backend/src/services/assignments.ts @@ -0,0 +1,20 @@ +import { getAssignmentRepository, getClassRepository } from "../data/repositories"; +import { AssignmentDTO, mapToAssignmentDTO } from "../interfaces/assignments"; + +export async function getAssignment(classid: string, id: number): Promise { + const classRepository = getClassRepository(); + const cls = await classRepository.findById(classid); + + if (!cls) { + return null; + } + + const assignmentRepository = getAssignmentRepository(); + const assignment = await assignmentRepository.findByClassAndId(cls, id); + + if (!assignment) { + return null; + } + + return mapToAssignmentDTO(assignment, cls); +} \ No newline at end of file