feat: teacher kan student request aanpassen en oprvagen

This commit is contained in:
Gabriellvl 2025-03-29 23:12:38 +01:00
parent 815858d02f
commit c8cff2e820
6 changed files with 136 additions and 139 deletions

View file

@ -9,13 +9,11 @@ import {
getStudentClasses, getStudentClasses,
getStudentGroups, getStudentGroups,
getStudentQuestions, getStudentQuestions,
getStudentSubmissions, updateClassJoinRequestStatus, getStudentSubmissions,
} from '../services/students.js'; } from '../services/students.js';
import { StudentDTO } from '../interfaces/student.js'; import { StudentDTO } from '../interfaces/student.js';
import {BadRequestException} from "../exceptions";
import {requireFields} from "./error-helper"; import {requireFields} from "./error-helper";
export async function getAllStudentsHandler(req: Request, res: Response): Promise<void> { export async function getAllStudentsHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true'; const full = req.query.full === 'true';
@ -41,16 +39,16 @@ export async function createStudentHandler(req: Request, res: Response) {
const userData = req.body as StudentDTO; const userData = req.body as StudentDTO;
const student = await createStudent(userData); await createStudent(userData);
res.status(201).json({ student }); res.status(201);
} }
export async function deleteStudentHandler(req: Request, res: Response) { export async function deleteStudentHandler(req: Request, res: Response) {
const username = req.params.username; const username = req.params.username;
requireFields({ username }); requireFields({ username });
const student = await deleteStudent(username); await deleteStudent(username);
res.status(200).json({ student }); res.status(200);
} }
export async function getStudentClassesHandler(req: Request, res: Response): Promise<void> { export async function getStudentClassesHandler(req: Request, res: Response): Promise<void> {
@ -133,16 +131,6 @@ export async function getStudentRequestHandler(req: Request, res: Response): Pro
res.status(201).json({ requests }) res.status(201).json({ requests })
} }
export async function updateClassJoinRequestHandler(req: Request, res: Response) {
const username = req.query.username as string;
const classId = req.params.classId;
const accepted = req.query.accepted !== 'false'; // default = true
requireFields({ username, classId });
const result = await updateClassJoinRequestStatus(username, classId, accepted);
res.status(200).json(result);
}
export async function deleteClassJoinRequestHandler(req: Request, res: Response) { export async function deleteClassJoinRequestHandler(req: Request, res: Response) {
const username = req.params.username as string; const username = req.params.username as string;
const classId = req.params.classId; const classId = req.params.classId;

View file

@ -3,10 +3,10 @@ import {
createTeacher, createTeacher,
deleteTeacher, deleteTeacher,
getAllTeachers, getAllTeachers,
getClassesByTeacher, getClassesByTeacher, getJoinRequestsByClass,
getStudentsByTeacher, getStudentsByTeacher,
getTeacher, getTeacher,
getTeacherQuestions getTeacherQuestions, updateClassJoinRequestStatus
} from '../services/teachers.js'; } from '../services/teachers.js';
import { ClassDTO } from '../interfaces/class.js'; import { ClassDTO } from '../interfaces/class.js';
import { StudentDTO } from '../interfaces/student.js'; import { StudentDTO } from '../interfaces/student.js';
@ -19,11 +19,6 @@ export async function getAllTeachersHandler(req: Request, res: Response): Promis
const teachers: TeacherDTO[] | string[] = await getAllTeachers(full); const teachers: TeacherDTO[] | string[] = await getAllTeachers(full);
if (!teachers) {
res.status(404).json({ error: `Teachers not found.` });
return;
}
res.json({ teachers }); res.json({ teachers });
} }
@ -31,59 +26,45 @@ export async function getTeacherHandler(req: Request, res: Response): Promise<vo
const username = req.params.username; const username = req.params.username;
requireFields({ username }); requireFields({ username });
const user = await getTeacher(username); const teacher = await getTeacher(username);
res.json({ teacher });
res.status(201).json(user);
} }
export async function createTeacherHandler(req: Request, res: Response) { export async function createTeacherHandler(req: Request, res: Response) {
const username = req.body.username;
const firstName = req.body.firstName;
const lastName = req.body.lastName;
requireFields({ username, firstName, lastName });
const userData = req.body as TeacherDTO; const userData = req.body as TeacherDTO;
if (!userData.username || !userData.firstName || !userData.lastName) { await createTeacher(userData);
return; res.status(201);
}
const newUser = await createTeacher(userData);
res.status(201).json(newUser);
} }
export async function deleteTeacherHandler(req: Request, res: Response) { export async function deleteTeacherHandler(req: Request, res: Response) {
const username = req.params.username; const username = req.params.username;
requireFields({ username });
if (!username) { await deleteTeacher(username);
return; res.status(200);
}
const deletedUser = await deleteTeacher(username);
if (!deletedUser) {
return;
}
res.status(200).json(deletedUser);
} }
export async function getTeacherClassHandler(req: Request, res: Response): Promise<void> { export async function getTeacherClassHandler(req: Request, res: Response): Promise<void> {
const username = req.params.username as string; const username = req.params.username as string;
const full = req.query.full === 'true'; const full = req.query.full === 'true';
requireFields({ username });
if (!username) {
return;
}
const classes: ClassDTO[] | string[] = await getClassesByTeacher(username, full); const classes: ClassDTO[] | string[] = await getClassesByTeacher(username, full);
res.status(201).json(classes); res.json(classes);
} }
export async function getTeacherStudentHandler(req: Request, res: Response): Promise<void> { export async function getTeacherStudentHandler(req: Request, res: Response): Promise<void> {
const username = req.params.username as string; const username = req.params.username as string;
const full = req.query.full === 'true'; const full = req.query.full === 'true';
requireFields({ username });
if (!username) {
return;
}
const students: StudentDTO[] | string[] = await getStudentsByTeacher(username, full); const students: StudentDTO[] | string[] = await getStudentsByTeacher(username, full);
@ -93,12 +74,28 @@ export async function getTeacherStudentHandler(req: Request, res: Response): Pro
export async function getTeacherQuestionHandler(req: Request, res: Response): Promise<void> { export async function getTeacherQuestionHandler(req: Request, res: Response): Promise<void> {
const username = req.params.username as string; const username = req.params.username as string;
const full = req.query.full === 'true'; const full = req.query.full === 'true';
requireFields({ username });
if (!username) {
return;
}
const questions: QuestionDTO[] | QuestionId[] = await getTeacherQuestions(username, full); const questions: QuestionDTO[] | QuestionId[] = await getTeacherQuestions(username, full);
res.json({ questions }); res.json({ questions });
} }
export async function getStudentJoinRequestHandler(req: Request, res: Response) {
const username = req.query.username as string;
const classId = req.params.classId;
requireFields({ username, classId });
const joinRequests = await getJoinRequestsByClass(classId);
res.json({ joinRequests });
}
export async function updateStudentJoinRequestHandler(req: Request, res: Response) {
const username = req.query.username as string;
const classId = req.params.classId;
const accepted = req.query.accepted !== 'false'; // default = true
requireFields({ username, classId });
await updateClassJoinRequestStatus(username, classId, accepted);
res.status(200);
}

View file

@ -1,6 +1,6 @@
import { DwengoEntityRepository } from '../dwengo-entity-repository.js'; import { DwengoEntityRepository } from '../dwengo-entity-repository.js';
import { Class } from '../../entities/classes/class.entity.js'; import { Class } from '../../entities/classes/class.entity.js';
import { ClassJoinRequest } from '../../entities/classes/class-join-request.entity.js'; import {ClassJoinRequest, ClassJoinRequestStatus} from '../../entities/classes/class-join-request.entity.js';
import { Student } from '../../entities/users/student.entity.js'; import { Student } from '../../entities/users/student.entity.js';
export class ClassJoinRequestRepository extends DwengoEntityRepository<ClassJoinRequest> { export class ClassJoinRequestRepository extends DwengoEntityRepository<ClassJoinRequest> {
@ -8,7 +8,7 @@ export class ClassJoinRequestRepository extends DwengoEntityRepository<ClassJoin
return this.findAll({ where: { requester: requester } }); return this.findAll({ where: { requester: requester } });
} }
public findAllOpenRequestsTo(clazz: Class): Promise<ClassJoinRequest[]> { public findAllOpenRequestsTo(clazz: Class): Promise<ClassJoinRequest[]> {
return this.findAll({ where: { class: clazz } }); return this.findAll({ where: { class: clazz, status: ClassJoinRequestStatus.Open, } }); // TODO check if works like this
} }
public findByStudentAndClass(requester: Student, clazz: Class): Promise<ClassJoinRequest | null> { public findByStudentAndClass(requester: Student, clazz: Class): Promise<ClassJoinRequest | null> {
return this.findOne({ requester, class: clazz }); return this.findOne({ requester, class: clazz });

View file

@ -2,11 +2,11 @@ import express from 'express';
import { import {
createTeacherHandler, createTeacherHandler,
deleteTeacherHandler, deleteTeacherHandler,
getAllTeachersHandler, getAllTeachersHandler, getStudentJoinRequestHandler,
getTeacherClassHandler, getTeacherClassHandler,
getTeacherHandler, getTeacherHandler,
getTeacherQuestionHandler, getTeacherQuestionHandler,
getTeacherStudentHandler, getTeacherStudentHandler, updateStudentJoinRequestHandler,
} from '../controllers/teachers.js'; } from '../controllers/teachers.js';
const router = express.Router(); const router = express.Router();
@ -25,6 +25,10 @@ router.get('/:username/students', getTeacherStudentHandler);
router.get('/:username/questions', getTeacherQuestionHandler); router.get('/:username/questions', getTeacherQuestionHandler);
router.get('/:username/joinRequests/:classId', getStudentJoinRequestHandler);
router.put('/:username/joinRequests/:classId/:studentUsername', updateStudentJoinRequestHandler);
// Invitations to other classes a teacher received // Invitations to other classes a teacher received
router.get('/:id/invitations', (req, res) => { router.get('/:id/invitations', (req, res) => {
res.json({ res.json({

View file

@ -44,31 +44,25 @@ export async function getStudent(username: string): Promise<StudentDTO> {
return mapToStudentDTO(user); return mapToStudentDTO(user);
} }
export async function createStudent(userData: StudentDTO): Promise<StudentDTO | null> { export async function createStudent(userData: StudentDTO): Promise<void> {
const studentRepository = getStudentRepository(); const studentRepository = getStudentRepository();
const user = await studentRepository.findByUsername(userData.username); const user = await studentRepository.findByUsername(userData.username);
if (user) { if (user) {
throw new ConflictException("Username already exists"); throw new ConflictException("Student with that sername already exists");
} }
const newStudent = studentRepository.create(mapToStudent(userData)); const newStudent = studentRepository.create(mapToStudent(userData));
await studentRepository.save(newStudent); await studentRepository.save(newStudent);
return mapToStudentDTO(newStudent);
} }
export async function deleteStudent(username: string): Promise<StudentDTO | null> { export async function deleteStudent(username: string): Promise<void> {
const studentRepository = getStudentRepository(); const studentRepository = getStudentRepository();
const user = await studentRepository.findByUsername(username); await fetchStudent(username); // throws error if it does not exist
if (!user) {
throw new NotFoundException("Student with username not found");
}
await studentRepository.deleteByUsername(username); await studentRepository.deleteByUsername(username);
return mapToStudentDTO(user);
} }
export async function getStudentClasses(username: string, full: boolean): Promise<ClassDTO[] | string[]> { export async function getStudentClasses(username: string, full: boolean): Promise<ClassDTO[] | string[]> {
@ -153,7 +147,6 @@ export async function createClassJoinRequest(studentUsername: string, classId: s
}); });
await requestRepo.save(request); await requestRepo.save(request);
return request;
} }
export async function getJoinRequestsByStudent(studentUsername: string) { export async function getJoinRequestsByStudent(studentUsername: string) {
@ -165,30 +158,6 @@ export async function getJoinRequestsByStudent(studentUsername: string) {
return requests.map(mapToStudentRequestDTO); return requests.map(mapToStudentRequestDTO);
} }
// TODO naar teacher
export async function updateClassJoinRequestStatus( studentUsername: string, classId: string, accepted: boolean = true) {
const requestRepo = getClassJoinRequestRepository();
const classRepo = getClassRepository();
const student = await fetchStudent(studentUsername);
const cls = await classRepo.findById(classId);
if (!cls) {
throw new NotFoundException('Class not found');
}
const request = await requestRepo.findOne({ requester: student, class: cls });
if (!request) {
throw new NotFoundException('Join request not found');
}
request.status = accepted ? ClassJoinRequestStatus.Accepted : ClassJoinRequestStatus.Declined;
await requestRepo.save(request);
return request;
}
export async function deleteClassJoinRequest(studentUsername: string, classId: string) { export async function deleteClassJoinRequest(studentUsername: string, classId: string) {
const requestRepo = getClassJoinRequestRepository(); const requestRepo = getClassJoinRequestRepository();
const classRepo = getClassRepository(); const classRepo = getClassRepository();
@ -207,7 +176,6 @@ export async function deleteClassJoinRequest(studentUsername: string, classId: s
} }
await requestRepo.deleteBy(student, cls); await requestRepo.deleteBy(student, cls);
return true;
} }

View file

@ -1,13 +1,19 @@
import { import {
getClassJoinRequestRepository,
getClassRepository, getClassRepository,
getLearningObjectRepository, getLearningObjectRepository,
getQuestionRepository, getQuestionRepository, getStudentRepository,
getTeacherRepository, getTeacherRepository,
} from '../data/repositories.js'; } from '../data/repositories.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { getClassStudents } from './class.js'; import { getClassStudents } from './class.js';
import {mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from '../interfaces/question.js'; import {mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from '../interfaces/question.js';
import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js'; import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js';
import {ConflictException, NotFoundException} from "../exceptions";
import {Teacher} from "../entities/users/teacher.entity";
import {fetchStudent} from "./students";
import {ClassJoinRequestStatus} from "../entities/classes/class-join-request.entity";
import {mapToStudentRequestDTO} from "../interfaces/student-request";
export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> { export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | string[]> {
const teacherRepository = getTeacherRepository(); const teacherRepository = getTeacherRepository();
@ -19,51 +25,45 @@ export async function getAllTeachers(full: boolean): Promise<TeacherDTO[] | stri
return users.map((user) => user.username); return users.map((user) => user.username);
} }
export async function getTeacher(username: string): Promise<TeacherDTO | null> { export async function fetchTeacher(username: string): Promise<Teacher> {
const teacherRepository = getTeacherRepository(); const studentRepository = getStudentRepository();
const user = await teacherRepository.findByUsername(username); const user = await studentRepository.findByUsername(username);
return user ? mapToTeacherDTO(user) : null;
}
export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO | null> {
const teacherRepository = getTeacherRepository();
try {
const newTeacher = teacherRepository.create(mapToTeacher(userData));
await teacherRepository.save(newTeacher);
return mapToTeacherDTO(newTeacher);
} catch (e) {
console.log(e);
return null;
}
}
export async function deleteTeacher(username: string): Promise<TeacherDTO | null> {
const teacherRepository = getTeacherRepository();
const user = await teacherRepository.findByUsername(username);
if (!user) { if (!user) {
return null; throw new NotFoundException("Teacher with username not found");
} }
try { return user;
await teacherRepository.deleteByUsername(username); }
export async function getTeacher(username: string): Promise<TeacherDTO | null> {
const user = await fetchTeacher(username);
return mapToTeacherDTO(user); return mapToTeacherDTO(user);
} catch (e) {
console.log(e);
return null;
} }
export async function createTeacher(userData: TeacherDTO): Promise<void> {
const teacherRepository = getTeacherRepository();
const user = await teacherRepository.findByUsername(userData.username);
if (user){
throw new ConflictException("Teacher with that username already exists");
}
const newTeacher = teacherRepository.create(mapToTeacher(userData));
await teacherRepository.save(newTeacher);
}
export async function deleteTeacher(username: string): Promise<void> {
const teacherRepository = getTeacherRepository();
await fetchTeacher(username); // throws error if it does not exist
await teacherRepository.deleteByUsername(username);
} }
async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> { async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> {
const teacherRepository = getTeacherRepository(); const teacher = await fetchTeacher(username);
const teacher = await teacherRepository.findByUsername(username);
if (!teacher) {
return [];
}
const classRepository = getClassRepository(); const classRepository = getClassRepository();
const classes = await classRepository.findByTeacher(teacher); const classes = await classRepository.findByTeacher(teacher);
@ -81,6 +81,11 @@ export async function getClassesByTeacher(username: string, full: boolean): Prom
export async function getStudentsByTeacher(username: string, full: boolean) { export async function getStudentsByTeacher(username: string, full: boolean) {
const classes = await fetchClassesByTeacher(username); const classes = await fetchClassesByTeacher(username);
if (!classes || classes.length === 0){
return [];
}
const classIds = classes.map((cls) => cls.id); const classIds = classes.map((cls) => cls.id);
const students = (await Promise.all(classIds.map(async (id) => getClassStudents(id)))).flat(); const students = (await Promise.all(classIds.map(async (id) => getClassStudents(id)))).flat();
@ -91,16 +96,16 @@ export async function getStudentsByTeacher(username: string, full: boolean) {
} }
export async function getTeacherQuestions(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> { export async function getTeacherQuestions(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
const teacherRepository = getTeacherRepository(); const teacher = await fetchTeacher(username);
const teacher = await teacherRepository.findByUsername(username);
if (!teacher) {
throw new Error(`Teacher with username '${username}' not found.`);
}
// Find all learning objects that this teacher manages // Find all learning objects that this teacher manages
const learningObjectRepository = getLearningObjectRepository(); const learningObjectRepository = getLearningObjectRepository();
const learningObjects = await learningObjectRepository.findAllByTeacher(teacher); const learningObjects = await learningObjectRepository.findAllByTeacher(teacher);
if (!learningObjects || learningObjects.length === 0){
return [];
}
// Fetch all questions related to these learning objects // Fetch all questions related to these learning objects
const questionRepository = getQuestionRepository(); const questionRepository = getQuestionRepository();
const questions = await questionRepository.findAllByLearningObjects(learningObjects); const questions = await questionRepository.findAllByLearningObjects(learningObjects);
@ -112,3 +117,38 @@ export async function getTeacherQuestions(username: string, full: boolean): Prom
return questionsDTO.map(mapToQuestionId); return questionsDTO.map(mapToQuestionId);
} }
export async function getJoinRequestsByClass( classId: string ){
const classRepository = getClassRepository();
const cls = await classRepository.findById(classId);
if (!cls) {
throw new NotFoundException("Class with id not found");
}
const requestRepo = getClassJoinRequestRepository();
const requests = await requestRepo.findAllOpenRequestsTo(cls);
return requests.map(mapToStudentRequestDTO);
}
export async function updateClassJoinRequestStatus( studentUsername: string, classId: string, accepted: boolean = true) {
const requestRepo = getClassJoinRequestRepository();
const classRepo = getClassRepository();
const student = await fetchStudent(studentUsername);
const cls = await classRepo.findById(classId);
if (!cls) {
throw new NotFoundException('Class not found');
}
const request = await requestRepo.findByStudentAndClass(student, cls);
if (!request) {
throw new NotFoundException('Join request not found');
}
request.status = accepted ? ClassJoinRequestStatus.Accepted : ClassJoinRequestStatus.Declined;
await requestRepo.save(request);
}