feat: service en controller laag voor getStudent geimplementeerd
This commit is contained in:
parent
8c22b72b22
commit
f5b6a5a604
5 changed files with 459 additions and 2917 deletions
29
backend/src/controllers/students.ts
Normal file
29
backend/src/controllers/students.ts
Normal 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' });
|
||||||
|
}
|
||||||
|
}
|
12
backend/src/interfaces/students.ts
Normal file
12
backend/src/interfaces/students.ts
Normal 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;
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,4 +1,6 @@
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
|
import { getStudentById } from '../services/students';
|
||||||
|
import { getStudent } from '../controllers/students';
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// root endpoint used to search objects
|
// root endpoint used to search objects
|
||||||
|
@ -12,20 +14,7 @@ router.get('/', (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// information about a student's profile
|
// information about a student's profile
|
||||||
router.get('/:id', (req, res) => {
|
router.get('/:id', getStudent);
|
||||||
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`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// the list of classes a student is in
|
// the list of classes a student is in
|
||||||
router.get('/:id/classes', (req, res) => {
|
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
|
export default router
|
18
backend/src/services/students.ts
Normal file
18
backend/src/services/students.ts
Normal 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
3293
package-lock.json
generated
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue