diff --git a/backend/src/controllers/classes.ts b/backend/src/controllers/classes.ts index ee932734..d7580d62 100644 --- a/backend/src/controllers/classes.ts +++ b/backend/src/controllers/classes.ts @@ -1,5 +1,5 @@ import { Request, Response } from 'express'; -import { getAllClasses, getClass, getClassStudents } from '../services/class'; +import { getAllClasses, getClass, getClassStudents, getClassTeacherInvitations } from '../services/class'; import { ClassDTO } from '../interfaces/classes'; export async function getAllClassesHandler( @@ -53,4 +53,18 @@ export async function getClassStudentsHandler( res.json({ students: students, }); +} + +export async function getTeacherInvitationsHandler( + req: Request, + res: Response, +): Promise { + const classId = req.params.id; + const full = req.query.full === "true"; // TODO: not implemented yet + + const invitations = await getClassTeacherInvitations(classId, full); + + res.json({ + invitations: invitations, + }); } \ No newline at end of file diff --git a/backend/src/data/repositories.ts b/backend/src/data/repositories.ts index 843eb1ac..35ad26cd 100644 --- a/backend/src/data/repositories.ts +++ b/backend/src/data/repositories.ts @@ -80,7 +80,7 @@ export const getClassJoinRequestRepository = repositoryGetter< export const getTeacherInvitationRepository = repositoryGetter< TeacherInvitation, TeacherInvitationRepository ->(TeacherInvitationRepository); +>(TeacherInvitation); /* Assignments */ export const getAssignmentRepository = repositoryGetter< diff --git a/backend/src/interfaces/teacher-invitation.ts b/backend/src/interfaces/teacher-invitation.ts new file mode 100644 index 00000000..c563968b --- /dev/null +++ b/backend/src/interfaces/teacher-invitation.ts @@ -0,0 +1,25 @@ +import { TeacherInvitation } from "../entities/classes/teacher-invitation.entity"; +import { ClassDTO, mapToClassDTO } from "./classes"; +import { mapToTeacherDTO, TeacherDTO } from "./teacher"; + +export interface TeacherInvitationDTO { + sender: string | TeacherDTO, + receiver: string | TeacherDTO, + class: string | ClassDTO, +} + +export function mapToTeacherInvitationDTO(invitation: TeacherInvitation): TeacherInvitationDTO { + return { + sender: mapToTeacherDTO(invitation.sender), + receiver: mapToTeacherDTO(invitation.receiver), + class: mapToClassDTO(invitation.class), + }; +} + +export function mapToTeacherInvitationDTOIds(invitation: TeacherInvitation): TeacherInvitationDTO { + return { + sender: invitation.sender.username, + receiver: invitation.receiver.username, + class: invitation.class.classId, + }; +} \ No newline at end of file diff --git a/backend/src/interfaces/teacher.ts b/backend/src/interfaces/teacher.ts new file mode 100644 index 00000000..5aee089c --- /dev/null +++ b/backend/src/interfaces/teacher.ts @@ -0,0 +1,23 @@ +import { Teacher } from "../entities/users/teacher.entity"; + +export interface TeacherDTO { + id: string; + username: string; + firstName: string; + lastName: string; + endpoints?: { + classes: string; + questions: string; + invitations: string; + groups: string; + }; +} + +export function mapToTeacherDTO(teacher: Teacher): TeacherDTO { + return { + id: teacher.username, + username: teacher.username, + firstName: teacher.firstName, + lastName: teacher.lastName, + }; +} \ No newline at end of file diff --git a/backend/src/routes/class.ts b/backend/src/routes/class.ts index bc502fb8..369e6ff4 100644 --- a/backend/src/routes/class.ts +++ b/backend/src/routes/class.ts @@ -1,5 +1,5 @@ import express from 'express' -import { getAllClassesHandler, getClassHandler, getClassStudentsHandler } from '../controllers/classes'; +import { getAllClassesHandler, getClassHandler, getClassStudentsHandler, getTeacherInvitationsHandler } from '../controllers/classes'; import assignmentRouter from './assignment.js'; const router = express.Router(); @@ -10,13 +10,7 @@ router.get('/', getAllClassesHandler); // information about an class with id 'id' router.get('/:id', getClassHandler); -router.get('/:id/invitations', (req, res) => { - res.json({ - invitations: [ - '0' - ], - }); -}) +router.get('/:id/teacher-invitations', getTeacherInvitationsHandler); router.get('/:id/students', getClassStudentsHandler); diff --git a/backend/src/services/class.ts b/backend/src/services/class.ts index 89628404..b009b66c 100644 --- a/backend/src/services/class.ts +++ b/backend/src/services/class.ts @@ -1,7 +1,8 @@ -import { getClassRepository } from "../data/repositories"; +import { getClassRepository, getTeacherInvitationRepository } from "../data/repositories"; import { Class } from "../entities/classes/class.entity"; import { ClassDTO, mapToClassDTO } from "../interfaces/classes"; import { mapToStudentDTO, StudentDTO } from "../interfaces/students"; +import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from "../interfaces/teacher-invitation"; export async function getAllClasses(full: boolean): Promise { const classRepository = getClassRepository(); @@ -41,4 +42,28 @@ export async function getClassStudents(classId: string, full: boolean): Promise< } else { return cls.students.map((student) => student.username); } +} + +export async function getClassTeacherInvitations(classId: string, full: boolean): Promise { + const classRepository = getClassRepository(); + const cls = await classRepository.findById(classId); + + if (!cls) { + return []; + } + + const teacherInvitationRepository = getTeacherInvitationRepository(); + const invitations = await teacherInvitationRepository.findAllInvitationsForClass(cls); + + console.log(invitations); + + if (!invitations) { + return []; + } + + if (full) { + return invitations.map(mapToTeacherInvitationDTO); + } + + return invitations.map(mapToTeacherInvitationDTOIds); } \ No newline at end of file