feat: questions via student

This commit is contained in:
Gabriellvl 2025-03-21 22:51:42 +01:00
parent 30ec73a88d
commit c31b471371
7 changed files with 153 additions and 171 deletions

View file

@ -1,53 +1,43 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { import {
createStudent, createStudent,
deleteStudent, deleteStudent, getAllStudentIds,
getAllStudents, getAllStudents,
getStudent, getStudent,
getStudentAssignments, getStudentAssignments,
getStudentClasses, getStudentClasses,
getStudentGroups, getStudentGroups, getStudentQuestions,
getStudentSubmissions, getStudentSubmissions,
} from '../services/students.js'; } from '../services/students.js';
import { ClassDTO } from '../interfaces/class.js'; import {MISSING_FIELDS_ERROR, MISSING_USERNAME_ERROR, NAME_NOT_FOUND_ERROR} from './users.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 { StudentDTO } from '../interfaces/student.js';
import { getStudentRepository } from '../data/repositories.js';
import { UserDTO } from '../interfaces/user.js';
// TODO: accept arguments (full, ...)
// TODO: endpoints
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';
const studentRepository = getStudentRepository(); const students: StudentDTO[] | string[] = full ? await getAllStudents() : await getAllStudentIds();
const students: StudentDTO[] | string[] = full ? await getAllStudents() : await getAllStudents();
if (!students) { if (!students) {
res.status(404).json({ error: `Student not found.` }); res.status(404).json({ error: `Students not found.` });
return; return;
} }
res.status(201).json(students); res.json({students});
} }
export async function getStudentHandler(req: Request, res: Response): Promise<void> { export async function getStudentHandler(req: Request, res: Response): Promise<void> {
const username = req.params.username; const username = req.params.username;
if (!username) { if (!username) {
res.status(400).json({ error: 'Missing required field: username' }); res.status(400).json(MISSING_USERNAME_ERROR);
return; return;
} }
const user = await getStudent(username); const user = await getStudent(username);
if (!user) { if (!user) {
res.status(404).json({ res.status(404).json(NAME_NOT_FOUND_ERROR(username));
error: `User with username '${username}' not found.`,
});
return; return;
} }
@ -58,9 +48,7 @@ export async function createStudentHandler(req: Request, res: Response) {
const userData = req.body as StudentDTO; const userData = req.body as StudentDTO;
if (!userData.username || !userData.firstName || !userData.lastName) { if (!userData.username || !userData.firstName || !userData.lastName) {
res.status(400).json({ res.status(400).json(MISSING_FIELDS_ERROR);
error: 'Missing required fields: username, firstName, lastName',
});
return; return;
} }
@ -72,15 +60,13 @@ export async function deleteStudentHandler(req: Request, res: Response) {
const username = req.params.username; const username = req.params.username;
if (!username) { if (!username) {
res.status(400).json({ error: 'Missing required field: username' }); res.status(400).json(MISSING_USERNAME_ERROR);
return; return;
} }
const deletedUser = await deleteStudent(username); const deletedUser = await deleteStudent(username);
if (!deletedUser) { if (!deletedUser) {
res.status(404).json({ res.status(404).json(NAME_NOT_FOUND_ERROR(username));
error: `User with username '${username}' not found.`,
});
return; return;
} }
@ -88,25 +74,19 @@ export async function deleteStudentHandler(req: Request, res: Response) {
} }
export async function getStudentClassesHandler(req: Request, res: Response): Promise<void> { export async function getStudentClassesHandler(req: Request, res: Response): Promise<void> {
try {
const full = req.query.full === 'true'; const full = req.query.full === 'true';
const username = req.params.id; const username = req.params.username;
if (!username) {
res.status(400).json(MISSING_USERNAME_ERROR);
return;
}
const classes = await getStudentClasses(username, full); const classes = await getStudentClasses(username, full);
res.json({ res.json({
classes: 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' });
}
} }
// TODO // TODO
@ -115,32 +95,62 @@ export async function getStudentClassesHandler(req: Request, res: Response): Pro
// Have this assignment. // Have this assignment.
export async function getStudentAssignmentsHandler(req: Request, res: Response): Promise<void> { export async function getStudentAssignmentsHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true'; const full = req.query.full === 'true';
const username = req.params.id; const username = req.params.username;
if (!username) {
res.status(400).json(MISSING_USERNAME_ERROR);
return;
}
const assignments = getStudentAssignments(username, full); const assignments = getStudentAssignments(username, full);
res.json({ res.json({
assignments: assignments, assignments,
}); });
} }
export async function getStudentGroupsHandler(req: Request, res: Response): Promise<void> { export async function getStudentGroupsHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true'; const full = req.query.full === 'true';
const username = req.params.id; const username = req.params.username;
if (!username) {
res.status(400).json(MISSING_USERNAME_ERROR);
return;
}
const groups = await getStudentGroups(username, full); const groups = await getStudentGroups(username, full);
res.json({ res.json({
groups: groups, groups,
}); });
} }
export async function getStudentSubmissionsHandler(req: Request, res: Response): Promise<void> { export async function getStudentSubmissionsHandler(req: Request, res: Response): Promise<void> {
const username = req.params.id; const username = req.params.username;
if (!username) {
res.status(400).json(MISSING_USERNAME_ERROR);
return;
}
const submissions = await getStudentSubmissions(username); const submissions = await getStudentSubmissions(username);
res.json({ res.json({
submissions: submissions, submissions,
}); });
} }
export async function getStudentQuestionsHandler(req: Request, res: Response): Promise<void> {
const username = req.params.username;
if (!username) {
res.status(400).json(MISSING_USERNAME_ERROR);
return;
}
const questions = await getStudentQuestions(username, full);
res.json({
questions,
})
}

View file

@ -4,49 +4,40 @@ import {
deleteTeacher, deleteTeacher,
getAllTeachers, getAllTeachers,
getClassesByTeacher, getClassesByTeacher,
getClassIdsByTeacher,
getQuestionIdsByTeacher,
getQuestionsByTeacher,
getStudentIdsByTeacher,
getStudentsByTeacher, getStudentsByTeacher,
getTeacher, getTeacher,
} 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';
import { QuestionDTO, QuestionId } from '../interfaces/question.js'; import { QuestionDTO, QuestionId } from '../interfaces/question.js';
import { Teacher } from '../entities/users/teacher.entity.js';
import { TeacherDTO } from '../interfaces/teacher.js'; import { TeacherDTO } from '../interfaces/teacher.js';
import { getTeacherRepository } from '../data/repositories.js'; import {MISSING_FIELDS_ERROR, MISSING_USERNAME_ERROR, NAME_NOT_FOUND_ERROR} from "./users";
export async function getAllTeachersHandler(req: Request, res: Response): Promise<void> { export async function getAllTeachersHandler(req: Request, res: Response): Promise<void> {
const full = req.query.full === 'true'; const full = req.query.full === 'true';
const teacherRepository = getTeacherRepository(); const teachers: TeacherDTO[] | string[] = await getAllTeachers(full);
const teachers: TeacherDTO[] | string[] = full ? await getAllTeachers() : await getAllTeachers();
if (!teachers) { if (!teachers) {
res.status(404).json({ error: `Teacher not found.` }); res.status(404).json({ error: `Teachers not found.` });
return; return;
} }
res.status(201).json(teachers); res.json({teachers});
} }
export async function getTeacherHandler(req: Request, res: Response): Promise<void> { export async function getTeacherHandler(req: Request, res: Response): Promise<void> {
const username = req.params.username; const username = req.params.username;
if (!username) { if (!username) {
res.status(400).json({ error: 'Missing required field: username' }); res.status(400).json(MISSING_USERNAME_ERROR);
return; return;
} }
const user = await getTeacher(username); const user = await getTeacher(username);
if (!user) { if (!user) {
res.status(404).json({ res.status(404).json(NAME_NOT_FOUND_ERROR(username));
error: `User with username '${username}' not found.`,
});
return; return;
} }
@ -57,9 +48,7 @@ export async function createTeacherHandler(req: Request, res: Response) {
const userData = req.body as TeacherDTO; const userData = req.body as TeacherDTO;
if (!userData.username || !userData.firstName || !userData.lastName) { if (!userData.username || !userData.firstName || !userData.lastName) {
res.status(400).json({ res.status(400).json(MISSING_FIELDS_ERROR);
error: 'Missing required fields: username, firstName, lastName',
});
return; return;
} }
@ -71,15 +60,13 @@ export async function deleteTeacherHandler(req: Request, res: Response) {
const username = req.params.username; const username = req.params.username;
if (!username) { if (!username) {
res.status(400).json({ error: 'Missing required field: username' }); res.status(400).json(MISSING_USERNAME_ERROR);
return; return;
} }
const deletedUser = await deleteTeacher(username); const deletedUser = await deleteTeacher(username);
if (!deletedUser) { if (!deletedUser) {
res.status(404).json({ res.status(404).json(NAME_NOT_FOUND_ERROR(username));
error: `User with username '${username}' not found.`,
});
return; return;
} }
@ -87,58 +74,43 @@ export async function deleteTeacherHandler(req: Request, res: Response) {
} }
export async function getTeacherClassHandler(req: Request, res: Response): Promise<void> { export async function getTeacherClassHandler(req: Request, res: Response): Promise<void> {
try {
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';
if (!username) { if (!username) {
res.status(400).json({ error: 'Missing required field: username' }); res.status(400).json(MISSING_USERNAME_ERROR);
return; return;
} }
const classes: ClassDTO[] | string[] = full ? await getClassesByTeacher(username) : await getClassIdsByTeacher(username); const classes: ClassDTO[] | string[] = await getClassesByTeacher(username, full);
res.status(201).json(classes); res.status(201).json(classes);
} catch (error) {
console.error('Error fetching classes by teacher:', error);
res.status(500).json({ error: 'Internal server error' });
}
} }
export async function getTeacherStudentHandler(req: Request, res: Response): Promise<void> { export async function getTeacherStudentHandler(req: Request, res: Response): Promise<void> {
try {
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';
if (!username) { if (!username) {
res.status(400).json({ error: 'Missing required field: username' }); res.status(400).json(MISSING_USERNAME_ERROR);
return; return;
} }
const students: StudentDTO[] | string[] = full ? await getStudentsByTeacher(username) : await getStudentIdsByTeacher(username); const students: StudentDTO[] | string[] = await getStudentsByTeacher(username, full);
res.status(201).json(students); res.json({students});
} catch (error) {
console.error('Error fetching students by teacher:', error);
res.status(500).json({ error: 'Internal server error' });
}
} }
export async function getTeacherQuestionHandler(req: Request, res: Response): Promise<void> { export async function getTeacherQuestionHandler(req: Request, res: Response): Promise<void> {
try {
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';
if (!username) { if (!username) {
res.status(400).json({ error: 'Missing required field: username' }); res.status(400).json(MISSING_USERNAME_ERROR);
return; return;
} }
const questions: QuestionDTO[] | QuestionId[] = full ? await getQuestionsByTeacher(username) : await getQuestionIdsByTeacher(username); const questions: QuestionDTO[] | QuestionId[] = await getQuestionsByTeacher(username, full);
res.status(201).json(questions); res.json({questions});
} catch (error) {
console.error('Error fetching questions by teacher:', error);
res.status(500).json({ error: 'Internal server error' });
}
} }

View file

@ -54,4 +54,12 @@ export class QuestionRepository extends DwengoEntityRepository<Question> {
orderBy: { timestamp: 'ASC' }, orderBy: { timestamp: 'ASC' },
}); });
} }
public findAllByAuthor(author: Student): Promise<Question[]> {
return this.findAll({
where: { author },
orderBy: { timestamp: 'DESC' }, // new to old
});
}
} }

View file

@ -6,7 +6,7 @@ import {
getStudentAssignmentsHandler, getStudentAssignmentsHandler,
getStudentClassesHandler, getStudentClassesHandler,
getStudentGroupsHandler, getStudentGroupsHandler,
getStudentHandler, getStudentHandler, getStudentQuestionsHandler,
getStudentSubmissionsHandler, getStudentSubmissionsHandler,
} from '../controllers/students.js'; } from '../controllers/students.js';
import { getStudentGroups } from '../services/students.js'; import { getStudentGroups } from '../services/students.js';
@ -17,30 +17,24 @@ router.get('/', getAllStudentsHandler);
router.post('/', createStudentHandler); router.post('/', createStudentHandler);
router.delete('/', deleteStudentHandler);
router.delete('/:username', deleteStudentHandler); router.delete('/:username', deleteStudentHandler);
// Information about a student's profile // Information about a student's profile
router.get('/:username', getStudentHandler); router.get('/:username', getStudentHandler);
// The list of classes a student is in // The list of classes a student is in
router.get('/:id/classes', getStudentClassesHandler); router.get('/:username/classes', getStudentClassesHandler);
// The list of submissions a student has made // The list of submissions a student has made
router.get('/:id/submissions', getStudentSubmissionsHandler); router.get('/:username/submissions', getStudentSubmissionsHandler);
// The list of assignments a student has // The list of assignments a student has
router.get('/:id/assignments', getStudentAssignmentsHandler); router.get('/:username/assignments', getStudentAssignmentsHandler);
// The list of groups a student is in // The list of groups a student is in
router.get('/:id/groups', getStudentGroupsHandler); router.get('/:username/groups', getStudentGroupsHandler);
// A list of questions a user has created // A list of questions a user has created
router.get('/:id/questions', (req, res) => { router.get('/:username/questions', getStudentQuestionsHandler);
res.json({
questions: ['0'],
});
});
export default router; export default router;

View file

@ -15,8 +15,6 @@ router.get('/', getAllTeachersHandler);
router.post('/', createTeacherHandler); router.post('/', createTeacherHandler);
router.delete('/', deleteTeacherHandler);
router.get('/:username', getTeacherHandler); router.get('/:username', getTeacherHandler);
router.delete('/:username', deleteTeacherHandler); router.delete('/:username', deleteTeacherHandler);

View file

@ -1,13 +1,17 @@
import { getClassRepository, getGroupRepository, getStudentRepository, getSubmissionRepository } from '../data/repositories.js'; import {
import { Class } from '../entities/classes/class.entity.js'; getClassRepository,
import { Student } from '../entities/users/student.entity.js'; getGroupRepository,
getQuestionRepository,
getStudentRepository,
getSubmissionRepository
} from '../data/repositories.js';
import { AssignmentDTO } from '../interfaces/assignment.js'; import { AssignmentDTO } from '../interfaces/assignment.js';
import { ClassDTO, mapToClassDTO } from '../interfaces/class.js'; import { ClassDTO, mapToClassDTO } from '../interfaces/class.js';
import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js'; import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from '../interfaces/group.js';
import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student.js'; import { mapToStudent, mapToStudentDTO, StudentDTO } from '../interfaces/student.js';
import { mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js'; import { mapToSubmissionDTO, SubmissionDTO } from '../interfaces/submission.js';
import { getAllAssignments } from './assignments.js'; import { getAllAssignments } from './assignments.js';
import { UserService } from './users.js'; import {mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId} from "../interfaces/question";
export async function getAllStudents(): Promise<StudentDTO[]> { export async function getAllStudents(): Promise<StudentDTO[]> {
const studentRepository = getStudentRepository(); const studentRepository = getStudentRepository();
@ -88,9 +92,7 @@ export async function getStudentAssignments(username: string, full: boolean): Pr
const classRepository = getClassRepository(); const classRepository = getClassRepository();
const classes = await classRepository.findByStudent(student); const classes = await classRepository.findByStudent(student);
const assignments = (await Promise.all(classes.map(async (cls) => await getAllAssignments(cls.classId!, full)))).flat(); return (await Promise.all(classes.map(async (cls) => await getAllAssignments(cls.classId!, full)))).flat();
return assignments;
} }
export async function getStudentGroups(username: string, full: boolean): Promise<GroupDTO[]> { export async function getStudentGroups(username: string, full: boolean): Promise<GroupDTO[]> {
@ -124,3 +126,20 @@ export async function getStudentSubmissions(username: string): Promise<Submissio
return submissions.map(mapToSubmissionDTO); return submissions.map(mapToSubmissionDTO);
} }
export async function getStudentQuestions(username: string, full: boolean): Promise<QuestionDTO[] | QuestionId[]> {
const studentRepository = getStudentRepository();
const student = await studentRepository.findByUsername(username);
if (!student) {
return [];
}
const questionRepository = getQuestionRepository();
const questions = await questionRepository.findAllByAuthor(student);
if (full)
return questions.map(mapToQuestionDTO)
return questions.map(mapToQuestionId);
}

View file

@ -5,23 +5,17 @@ import {
getStudentRepository, getStudentRepository,
getTeacherRepository, getTeacherRepository,
} from '../data/repositories.js'; } from '../data/repositories.js';
import { Teacher } from '../entities/users/teacher.entity.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 { StudentDTO } from '../interfaces/student.js'; import { mapToQuestionDTO, mapToQuestionId, QuestionDTO } from '../interfaces/question.js';
import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js';
import { UserService } from './users.js';
import { mapToUser } from '../interfaces/user.js';
import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js'; import { mapToTeacher, mapToTeacherDTO, TeacherDTO } from '../interfaces/teacher.js';
export async function getAllTeachers(): Promise<TeacherDTO[]> { export async function getAllTeachers(full: boolean): Promise<TeacherDTO[]> {
const teacherRepository = getTeacherRepository(); const teacherRepository = getTeacherRepository();
const users = await teacherRepository.findAll(); const users = await teacherRepository.findAll();
return users.map(mapToTeacherDTO);
}
export async function getAllTeacherIds(): Promise<string[]> { if (full)
const users = await getAllTeachers(); return users.map(mapToTeacherDTO);
return users.map((user) => user.username); return users.map((user) => user.username);
} }
@ -64,7 +58,7 @@ export async function deleteTeacher(username: string): Promise<TeacherDTO | null
} }
} }
export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> { async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> {
const teacherRepository = getTeacherRepository(); const teacherRepository = getTeacherRepository();
const teacher = await teacherRepository.findByUsername(username); const teacher = await teacherRepository.findByUsername(username);
if (!teacher) { if (!teacher) {
@ -76,31 +70,24 @@ export async function fetchClassesByTeacher(username: string): Promise<ClassDTO[
return classes.map(mapToClassDTO); return classes.map(mapToClassDTO);
} }
export async function getClassesByTeacher(username: string): Promise<ClassDTO[]> { export async function getClassesByTeacher(username: string, full: boolean): Promise<ClassDTO[] | string[]> {
return await fetchClassesByTeacher(username);
}
export async function getClassIdsByTeacher(username: string): Promise<string[]> {
const classes = await fetchClassesByTeacher(username); const classes = await fetchClassesByTeacher(username);
if (full)
return classes;
return classes.map((cls) => cls.id); return classes.map((cls) => cls.id);
} }
export async function fetchStudentsByTeacher(username: string) { export async function getStudentsByTeacher(username: string, full: boolean) {
const classes = await getClassIdsByTeacher(username); const classes = await getClassesByTeacher(username, false);
return (await Promise.all(classes.map(async (id) => getClassStudents(id)))).flat(); const students = (await Promise.all(classes.map(async (id) => getClassStudents(id)))).flat();
} if (full)
return students
export async function getStudentsByTeacher(username: string): Promise<StudentDTO[]> {
return await fetchStudentsByTeacher(username);
}
export async function getStudentIdsByTeacher(username: string): Promise<string[]> {
const students = await fetchStudentsByTeacher(username);
return students.map((student) => student.username); return students.map((student) => student.username);
} }
export async function fetchTeacherQuestions(username: string): Promise<QuestionDTO[]> { export async function getTeacherQuestions(username: string, full: boolean): Promise<QuestionDTO[]> {
const teacherRepository = getTeacherRepository(); const teacherRepository = getTeacherRepository();
const teacher = await teacherRepository.findByUsername(username); const teacher = await teacherRepository.findByUsername(username);
if (!teacher) { if (!teacher) {
@ -115,15 +102,9 @@ export async function fetchTeacherQuestions(username: string): Promise<QuestionD
const questionRepository = getQuestionRepository(); const questionRepository = getQuestionRepository();
const questions = await questionRepository.findAllByLearningObjects(learningObjects); const questions = await questionRepository.findAllByLearningObjects(learningObjects);
if (full)
return questions.map(mapToQuestionDTO); return questions.map(mapToQuestionDTO);
}
export async function getQuestionsByTeacher(username: string): Promise<QuestionDTO[]> {
return await fetchTeacherQuestions(username);
}
export async function getQuestionIdsByTeacher(username: string): Promise<QuestionId[]> {
const questions = await fetchTeacherQuestions(username);
return questions.map(mapToQuestionId); return questions.map(mapToQuestionId);
} }