feat: service en controller laag voor getStudent geimplementeerd

This commit is contained in:
Adriaan Jacquet 2025-03-04 12:31:12 +01:00
parent 8c22b72b22
commit f5b6a5a604
5 changed files with 459 additions and 2917 deletions

View file

@ -0,0 +1,29 @@
import { Request, Response } from 'express';
import { getStudentById } from "../services/students";
export async function getStudent(
req: Request,
res: Response,
): Promise<void> {
try {
const username = req.params.id;
const student = await getStudentById(username);
if (!student) {
res.status(404).json({ error: "Student not found" });
return;
} else {
student.endpoints = {
classes: `/student/${req.params.id}/classes`,
questions: `/student/${req.params.id}/submissions`,
invitations: `/student/${req.params.id}/assignments`,
groups: `/student/${req.params.id}/groups`,
}
res.json(student);
}
} catch (error) {
console.error('Error fetching learning objects:', error);
res.status(500).json({ error: 'Internal server error' });
}
}

View file

@ -0,0 +1,12 @@
export interface StudentDTO {
id: string;
username: string;
firstName: string;
lastName: string;
endpoints?: {
classes: string;
questions: string;
invitations: string;
groups: string;
};
}

View file

@ -1,4 +1,6 @@
import express from 'express'
import { getStudentById } from '../services/students';
import { getStudent } from '../controllers/students';
const router = express.Router();
// root endpoint used to search objects
@ -12,20 +14,7 @@ router.get('/', (req, res) => {
});
// information about a student's profile
router.get('/:id', (req, res) => {
res.json({
id: req.params.id,
firstName: 'Jimmy',
lastName: 'Faster',
username: 'JimmyFaster2',
endpoints: {
classes: `/student/${req.params.id}/classes`,
questions: `/student/${req.params.id}/submissions`,
invitations: `/student/${req.params.id}/assignments`,
groups: `/student/${req.params.id}/groups`,
},
});
});
router.get('/:id', getStudent);
// the list of classes a student is in
router.get('/:id/classes', (req, res) => {
@ -56,4 +45,11 @@ router.get('/:id/groups', (req, res) => {
});
})
// a list of questions a user has created
router.get('/:id/questions', (req, res) => {
res.json({
questions: [ '0' ],
});
})
export default router

View file

@ -0,0 +1,18 @@
import { getStudentRepository } from "../data/repositories";
import { StudentDTO } from "../interfaces/students";
export async function getStudentById(username: string): Promise<StudentDTO | null> {
const studentRepository = getStudentRepository();
const student = await studentRepository.findByUsername(username);
if (!student) return null;
else {
return {
id: student.username,
username: student.username,
firstName: student.firstName,
lastName: student.lastName,
}
}
}

3293
package-lock.json generated

File diff suppressed because it is too large Load diff