feat: endpoints voor /, /:id en /:id/students in routes/class.ts zijn geimplementeerd
This commit is contained in:
parent
123fdf0fa1
commit
241fe0103f
7 changed files with 95 additions and 34 deletions
|
@ -1,5 +1,18 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { getClass } from '../services/class';
|
||||
import { getAllClasses, getClass, getClassStudents } from '../services/class';
|
||||
import { ClassDTO } from '../interfaces/classes';
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
export async function getClassHandler(
|
||||
req: Request,
|
||||
|
@ -26,4 +39,18 @@ export async function getClassHandler(
|
|||
console.error('Error fetching learning objects:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
}
|
||||
|
||||
export async function getClassStudentsHandler(
|
||||
req: Request,
|
||||
res: Response,
|
||||
): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const full = req.query.full === "true";
|
||||
|
||||
const students = await getClassStudents(classId, full);
|
||||
|
||||
res.json({
|
||||
students: students,
|
||||
});
|
||||
}
|
|
@ -1,17 +1,18 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { getStudent, getStudentClasses, getStudentClassIds } from '../services/students';
|
||||
import { getAllStudents, getStudent, getStudentClasses, getStudentClassIds } from '../services/students';
|
||||
import { ClassDTO } from '../interfaces/classes';
|
||||
|
||||
// TODO: accept arguments (full, ...)
|
||||
// TODO: endpoints
|
||||
export async function getAllStudentsHandler (
|
||||
req: Request,
|
||||
res: Response,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const students = await getAllStudents();
|
||||
|
||||
res.json({
|
||||
students: [
|
||||
'0',
|
||||
'1',
|
||||
]
|
||||
students: students
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching learning objects:', error);
|
||||
|
|
|
@ -4,7 +4,10 @@ import { Student } from '../../entities/users/student.entity.js';
|
|||
|
||||
export class ClassRepository extends DwengoEntityRepository<Class> {
|
||||
public findById(id: string): Promise<Class | null> {
|
||||
return this.findOne({ classId: id });
|
||||
return this.findOne(
|
||||
{ classId: id },
|
||||
{ populate: ["students", "teachers"] },
|
||||
);
|
||||
}
|
||||
public deleteById(id: string): Promise<void> {
|
||||
return this.deleteWhere({ classId: id });
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { Student } from "../entities/users/student.entity";
|
||||
|
||||
export interface StudentDTO {
|
||||
id: string;
|
||||
username: string;
|
||||
|
@ -10,3 +12,12 @@ export interface StudentDTO {
|
|||
groups: string;
|
||||
};
|
||||
}
|
||||
|
||||
export function mapToStudentDTO(student: Student): StudentDTO {
|
||||
return {
|
||||
id: student.username,
|
||||
username: student.username,
|
||||
firstName: student.firstName,
|
||||
lastName: student.lastName,
|
||||
};
|
||||
}
|
|
@ -1,16 +1,9 @@
|
|||
import express from 'express'
|
||||
import { getClassHandler } from '../controllers/classes';
|
||||
import { getAllClassesHandler, getClassHandler, getClassStudentsHandler } from '../controllers/classes';
|
||||
const router = express.Router();
|
||||
|
||||
// root endpoint used to search objects
|
||||
router.get('/', (req, res) => {
|
||||
res.json({
|
||||
classes: [
|
||||
'0',
|
||||
'1',
|
||||
]
|
||||
});
|
||||
});
|
||||
router.get('/', getAllClassesHandler);
|
||||
|
||||
// information about an class with id 'id'
|
||||
router.get('/:id', getClassHandler);
|
||||
|
@ -31,12 +24,6 @@ router.get('/:id/assignments', (req, res) => {
|
|||
});
|
||||
})
|
||||
|
||||
router.get('/:id/students', (req, res) => {
|
||||
res.json({
|
||||
students: [
|
||||
'0'
|
||||
],
|
||||
});
|
||||
})
|
||||
router.get('/:id/students', getClassStudentsHandler);
|
||||
|
||||
export default router
|
|
@ -1,5 +1,22 @@
|
|||
import { getClassRepository } from "../data/repositories";
|
||||
import { Class } from "../entities/classes/class.entity";
|
||||
import { ClassDTO, mapToClassDTO } from "../interfaces/classes";
|
||||
import { mapToStudentDTO, StudentDTO } from "../interfaces/students";
|
||||
|
||||
export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const classes = await classRepository.find({}, { populate: ["students", "teachers"] });
|
||||
|
||||
if (!classes) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (full) {
|
||||
return classes.map(mapToClassDTO);
|
||||
} else {
|
||||
return classes.map((cls) => cls.classId);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getClass(classId: string): Promise<ClassDTO | null> {
|
||||
const classRepository = getClassRepository();
|
||||
|
@ -9,4 +26,19 @@ export async function getClass(classId: string): Promise<ClassDTO | null> {
|
|||
else {
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getClassStudents(classId: string, full: boolean): Promise<StudentDTO[] | string[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classId);
|
||||
|
||||
if (!cls) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (full) {
|
||||
return cls.students.map(mapToStudentDTO);
|
||||
} else {
|
||||
return cls.students.map((student) => student.username);
|
||||
}
|
||||
}
|
|
@ -1,26 +1,26 @@
|
|||
import { getClassRepository, getStudentRepository } from "../data/repositories";
|
||||
import { Class } from "../entities/classes/class.entity";
|
||||
import { Student } from "../entities/users/student.entity";
|
||||
import { ClassDTO, mapToClassDTO } from "../interfaces/classes";
|
||||
import { StudentDTO } from "../interfaces/students";
|
||||
import { StudentDTO, mapToStudentDTO } from "../interfaces/students";
|
||||
|
||||
|
||||
export async function getAllStudents(): Promise<StudentDTO[]> {
|
||||
// TODO
|
||||
return [];
|
||||
const studentRepository = getStudentRepository();
|
||||
const students = await studentRepository.find({});
|
||||
|
||||
return students.map(mapToStudentDTO);
|
||||
}
|
||||
|
||||
export async function getStudent(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,
|
||||
}
|
||||
if (!student) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return mapToStudentDTO(student);
|
||||
}
|
||||
|
||||
async function fetchStudentClasses(username: string): Promise<Class[]> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue