fix: backend controllers gehomologeerd #130

This commit is contained in:
Adriaan Jacquet 2025-03-22 11:41:52 +01:00
parent 30ec73a88d
commit 32a3bb0dd6
10 changed files with 75 additions and 194 deletions

View file

@ -37,7 +37,7 @@ export async function createAssignmentHandler(req: Request<AssignmentParams>, re
return;
}
res.status(201).json({ assignment: assignment });
res.status(201).json(assignment);
}
export async function getAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {

View file

@ -28,7 +28,7 @@ export async function createClassHandler(req: Request, res: Response): Promise<v
return;
}
res.status(201).json({ class: cls });
res.status(201).json(cls);
}
export async function getClassHandler(req: Request, res: Response): Promise<void> {

View file

@ -66,7 +66,7 @@ export async function createGroupHandler(req: Request, res: Response): Promise<v
return;
}
res.status(201).json({ group: group });
res.status(201).json(group);
}
export async function getGroupSubmissionsHandler(req: Request, res: Response): Promise<void> {

View file

@ -40,7 +40,7 @@ export async function getAllLearningObjects(req: Request, res: Response): Promis
learningObjects = await learningObjectService.getLearningObjectIdsFromPath(learningPathId);
}
res.json(learningObjects);
res.json({ learningObjects: learningObjects });
}
export async function getLearningObject(req: Request, res: Response): Promise<void> {

View file

@ -48,7 +48,7 @@ export async function getAllQuestionsHandler(req: Request, res: Response): Promi
if (!questions) {
res.status(404).json({ error: `Questions not found.` });
} else {
res.json(questions);
res.json({ questions: questions });
}
}
@ -81,7 +81,7 @@ export async function getQuestionAnswersHandler(req: Request, res: Response): Pr
if (!answers) {
res.status(404).json({ error: `Questions not found.` });
} else {
res.json(answers);
res.json({ answers: answers });
}
}

View file

@ -9,13 +9,8 @@ import {
getStudentGroups,
getStudentSubmissions,
} from '../services/students.js';
import { ClassDTO } from '../interfaces/class.js';
import { getAllAssignments } from '../services/assignments.js';
import { getUserHandler } from './users.js';
import { Student } from '../entities/users/student.entity.js';
import { StudentDTO } from '../interfaces/student.js';
import { getStudentRepository } from '../data/repositories.js';
import { UserDTO } from '../interfaces/user.js';
// TODO: accept arguments (full, ...)
// TODO: endpoints
@ -31,7 +26,7 @@ export async function getAllStudentsHandler(req: Request, res: Response): Promis
return;
}
res.status(201).json(students);
res.json({ students: students });
}
export async function getStudentHandler(req: Request, res: Response): Promise<void> {
@ -51,7 +46,7 @@ export async function getStudentHandler(req: Request, res: Response): Promise<vo
return;
}
res.status(201).json(user);
res.json(user);
}
export async function createStudentHandler(req: Request, res: Response) {
@ -88,25 +83,14 @@ export async function deleteStudentHandler(req: Request, res: Response) {
}
export async function getStudentClassesHandler(req: Request, res: Response): Promise<void> {
try {
const full = req.query.full === 'true';
const username = req.params.id;
const full = req.query.full === 'true';
const username = req.params.id;
const classes = await getStudentClasses(username, full);
const classes = await getStudentClasses(username, full);
res.json({
classes: classes,
endpoints: {
self: `${req.baseUrl}/${req.params.id}`,
classes: `${req.baseUrl}/${req.params.id}/invitations`,
questions: `${req.baseUrl}/${req.params.id}/assignments`,
students: `${req.baseUrl}/${req.params.id}/students`,
},
});
} catch (error) {
console.error('Error fetching learning objects:', error);
res.status(500).json({ error: 'Internal server error' });
}
res.json({
classes: classes,
});
}
// TODO

View file

@ -36,10 +36,11 @@ export async function createSubmissionHandler(req: Request, res: Response) {
const submission = await createSubmission(submissionDTO);
if (!submission) {
res.status(404).json({ error: 'Submission not added' });
} else {
res.json(submission);
res.status(400).json({ error: 'Failed to create submission' });
return;
}
res.json(submission);
}
export async function deleteSubmissionHandler(req: Request, res: Response) {
@ -53,7 +54,8 @@ export async function deleteSubmissionHandler(req: Request, res: Response) {
if (!submission) {
res.status(404).json({ error: 'Submission not found' });
} else {
res.json(submission);
return;
}
res.json(submission);
}

View file

@ -4,17 +4,10 @@ import {
deleteTeacher,
getAllTeachers,
getClassesByTeacher,
getClassIdsByTeacher,
getQuestionIdsByTeacher,
getQuestionsByTeacher,
getStudentIdsByTeacher,
getStudentsByTeacher,
getTeacher,
} from '../services/teachers.js';
import { ClassDTO } from '../interfaces/class.js';
import { StudentDTO } from '../interfaces/student.js';
import { QuestionDTO, QuestionId } from '../interfaces/question.js';
import { Teacher } from '../entities/users/teacher.entity.js';
import { TeacherDTO } from '../interfaces/teacher.js';
import { getTeacherRepository } from '../data/repositories.js';
@ -30,7 +23,7 @@ export async function getAllTeachersHandler(req: Request, res: Response): Promis
return;
}
res.status(201).json(teachers);
res.status(201).json({ teachers: teachers });
}
export async function getTeacherHandler(req: Request, res: Response): Promise<void> {
@ -45,7 +38,7 @@ export async function getTeacherHandler(req: Request, res: Response): Promise<vo
if (!user) {
res.status(404).json({
error: `User with username '${username}' not found.`,
error: `User '${username}' not found.`,
});
return;
}
@ -64,6 +57,12 @@ export async function createTeacherHandler(req: Request, res: Response) {
}
const newUser = await createTeacher(userData);
if (!newUser) {
res.status(400).json({ error: "Failed to create teacher" });
return;
}
res.status(201).json(newUser);
}
@ -78,7 +77,7 @@ export async function deleteTeacherHandler(req: Request, res: Response) {
const deletedUser = await deleteTeacher(username);
if (!deletedUser) {
res.status(404).json({
error: `User with username '${username}' not found.`,
error: `User '${username}' not found.`,
});
return;
}
@ -87,58 +86,43 @@ export async function deleteTeacherHandler(req: Request, res: Response) {
}
export async function getTeacherClassHandler(req: Request, res: Response): Promise<void> {
try {
const username = req.params.username as string;
const full = req.query.full === 'true';
const username = req.params.username as string;
const full = req.query.full === 'true';
if (!username) {
res.status(400).json({ error: 'Missing required field: username' });
return;
}
const classes: ClassDTO[] | string[] = full ? await getClassesByTeacher(username) : await getClassIdsByTeacher(username);
res.status(201).json(classes);
} catch (error) {
console.error('Error fetching classes by teacher:', error);
res.status(500).json({ error: 'Internal server error' });
if (!username) {
res.status(400).json({ error: 'Missing required field: username' });
return;
}
const classes = await getClassesByTeacher(username, full);
res.status(201).json({ classes: classes });
}
export async function getTeacherStudentHandler(req: Request, res: Response): Promise<void> {
try {
const username = req.params.username as string;
const full = req.query.full === 'true';
const username = req.params.username as string;
const full = req.query.full === 'true';
if (!username) {
res.status(400).json({ error: 'Missing required field: username' });
return;
}
const students: StudentDTO[] | string[] = full ? await getStudentsByTeacher(username) : await getStudentIdsByTeacher(username);
res.status(201).json(students);
} catch (error) {
console.error('Error fetching students by teacher:', error);
res.status(500).json({ error: 'Internal server error' });
if (!username) {
res.status(400).json({ error: 'Missing required field: username' });
return;
}
const students = await getStudentsByTeacher(username, full);
res.json({ students: students });
}
export async function getTeacherQuestionHandler(req: Request, res: Response): Promise<void> {
try {
const username = req.params.username as string;
const full = req.query.full === 'true';
const username = req.params.username as string;
const full = req.query.full === 'true';
if (!username) {
res.status(400).json({ error: 'Missing required field: username' });
return;
}
const questions: QuestionDTO[] | QuestionId[] = full ? await getQuestionsByTeacher(username) : await getQuestionIdsByTeacher(username);
res.status(201).json(questions);
} catch (error) {
console.error('Error fetching questions by teacher:', error);
res.status(500).json({ error: 'Internal server error' });
if (!username) {
res.status(400).json({ error: 'Missing required field: username' });
return;
}
const questions = await getQuestionsByTeacher(username, full);
res.json({ questions: questions });
}

View file

@ -1,91 +0,0 @@
import { Request, Response } from 'express';
import { UserService } from '../services/users.js';
import { UserDTO } from '../interfaces/user.js';
import { User } from '../entities/users/user.entity.js';
export async function getAllUsersHandler<T extends User>(req: Request, res: Response, service: UserService<T>): Promise<void> {
try {
const full = req.query.full === 'true';
const users: UserDTO[] | string[] = full ? await service.getAllUsers() : await service.getAllUserIds();
if (!users) {
res.status(404).json({ error: `Users not found.` });
return;
}
res.status(201).json(users);
} catch (error) {
console.error('❌ Error fetching users:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export async function getUserHandler<T extends User>(req: Request, res: Response, service: UserService<T>): Promise<void> {
try {
const username = req.params.username as string;
if (!username) {
res.status(400).json({ error: 'Missing required field: username' });
return;
}
const user = await service.getUserByUsername(username);
if (!user) {
res.status(404).json({
error: `User with username '${username}' not found.`,
});
return;
}
res.status(201).json(user);
} catch (error) {
console.error('❌ Error fetching users:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export async function createUserHandler<T extends User>(req: Request, res: Response, service: UserService<T>, UserClass: new () => T) {
try {
console.log('req', req);
const userData = req.body as UserDTO;
if (!userData.username || !userData.firstName || !userData.lastName) {
res.status(400).json({
error: 'Missing required fields: username, firstName, lastName',
});
return;
}
const newUser = await service.createUser(userData, UserClass);
res.status(201).json(newUser);
} catch (error) {
console.error('❌ Error creating user:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
export async function deleteUserHandler<T extends User>(req: Request, res: Response, service: UserService<T>) {
try {
const username = req.params.username;
if (!username) {
res.status(400).json({ error: 'Missing required field: username' });
return;
}
const deletedUser = await service.deleteUser(username);
if (!deletedUser) {
res.status(404).json({
error: `User with username '${username}' not found.`,
});
return;
}
res.status(200).json(deletedUser);
} catch (error) {
console.error('❌ Error deleting user:', error);
res.status(500).json({ error: 'Internal server error' });
}
}

View file

@ -76,27 +76,29 @@ export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[
return classes.map(mapToClassDTO);
}
export async function getClassesByTeacher(username: string): Promise<ClassDTO[]> {
return await fetchClassesByTeacher(username);
}
export async function getClassIdsByTeacher(username: string): Promise<string[]> {
export async function getClassesByTeacher(username: string, full: boolean): Promise<ClassDTO[] | string[]> {
const classes = await fetchClassesByTeacher(username);
if (full) {
return classes;
}
return classes.map((cls) => cls.id);
}
export async function fetchStudentsByTeacher(username: string) {
const classes = await getClassIdsByTeacher(username);
const classes = await getClassesByTeacher(username, false) as string[];
return (await Promise.all(classes.map(async (id) => getClassStudents(id)))).flat();
}
export async function getStudentsByTeacher(username: string): Promise<StudentDTO[]> {
return await fetchStudentsByTeacher(username);
}
export async function getStudentIdsByTeacher(username: string): Promise<string[]> {
export async function getStudentsByTeacher(username: string, full: boolean): Promise<StudentDTO[] | string[]> {
const students = await fetchStudentsByTeacher(username);
if (full) {
return students;
}
return students.map((student) => student.username);
}
@ -118,12 +120,12 @@ export async function fetchTeacherQuestions(username: string): Promise<QuestionD
return questions.map(mapToQuestionDTO);
}
export async function getQuestionsByTeacher(username: string): Promise<QuestionDTO[]> {
return await fetchTeacherQuestions(username);
}
export async function getQuestionIdsByTeacher(username: string): Promise<QuestionId[]> {
export async function getQuestionsByTeacher(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
const questions = await fetchTeacherQuestions(username);
if (full) {
return questions;
}
return questions.map(mapToQuestionId);
}