fix: assignment errors en return json

This commit is contained in:
Gabriellvl 2025-04-06 10:40:34 +02:00
parent dbc1da741c
commit 2c4bc644fd
2 changed files with 43 additions and 75 deletions

View file

@ -1,6 +1,8 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { createAssignment, getAllAssignments, getAssignment, getAssignmentsSubmissions } from '../services/assignments.js'; import { createAssignment, getAllAssignments, getAssignment, getAssignmentsSubmissions } from '../services/assignments.js';
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
import {requireFields} from "./error-helper";
import {BadRequestException} from "../exceptions/bad-request-exception";
// Typescript is annoying with parameter forwarding from class.ts // Typescript is annoying with parameter forwarding from class.ts
interface AssignmentParams { interface AssignmentParams {
@ -9,69 +11,55 @@ interface AssignmentParams {
} }
export async function getAllAssignmentsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> { export async function getAllAssignmentsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
const classid = req.params.classid; const classId = req.params.classid;
const full = req.query.full === 'true'; const full = req.query.full === 'true';
requireFields({ classId });
const assignments = await getAllAssignments(classid, full); const assignments = await getAllAssignments(classId, full);
res.json({ res.json({ assignments });
assignments: assignments,
});
} }
export async function createAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> { export async function createAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
const classid = req.params.classid; const classid = req.params.classid;
const assignmentData = req.body as AssignmentDTO; const description = req.body.description;
const language = req.body.language;
const learningPath = req.body.learningPath;
const title = req.body.title;
if (!assignmentData.description || !assignmentData.language || !assignmentData.learningPath || !assignmentData.title) { requireFields({ description, language, learningPath, title });
res.status(400).json({ const assignmentData = req.body as AssignmentDTO;
error: 'Missing one or more required fields: title, description, learningPath, language',
});
return;
}
const assignment = await createAssignment(classid, assignmentData); const assignment = await createAssignment(classid, assignmentData);
if (!assignment) { res.json({ assignment });
res.status(500).json({ error: 'Could not create assignment ' });
return;
}
res.status(201).json(assignment);
} }
export async function getAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> { export async function getAssignmentHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
const id = Number(req.params.id); const id = Number(req.params.id);
const classid = req.params.classid; const classid = req.params.classid;
requireFields({ id, classid });
if (isNaN(id)) { if (isNaN(id)) {
res.status(400).json({ error: 'Assignment id must be a number' }); throw new BadRequestException("Assignment id should be a number")
return;
} }
const assignment = await getAssignment(classid, id); const assignment = await getAssignment(classid, id);
if (!assignment) { res.json({ assignment });
res.status(404).json({ error: 'Assignment not found' });
return;
}
res.json(assignment);
} }
export async function getAssignmentsSubmissionsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> { export async function getAssignmentsSubmissionsHandler(req: Request<AssignmentParams>, res: Response): Promise<void> {
const classid = req.params.classid; const classid = req.params.classid;
const assignmentNumber = Number(req.params.id); const assignmentNumber = Number(req.params.id);
const full = req.query.full === 'true'; const full = req.query.full === 'true';
requireFields({ assignmentNumber, classid });
if (isNaN(assignmentNumber)) { if (isNaN(assignmentNumber)) {
res.status(400).json({ error: 'Assignment id must be a number' }); throw new BadRequestException("Assignment id should be a number")
return;
} }
const submissions = await getAssignmentsSubmissions(classid, assignmentNumber, full); const submissions = await getAssignmentsSubmissions(classid, assignmentNumber, full);
res.json({ res.json({ submissions });
submissions: submissions,
});
} }

View file

@ -3,15 +3,12 @@ import { mapToAssignment, mapToAssignmentDTO, mapToAssignmentDTOId } from '../in
import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js'; import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submission.js';
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
import { getLogger } from '../logging/initalize.js'; import {fetchClass} from "./classes";
import {Assignment} from "../entities/assignments/assignment.entity";
import {NotFoundException} from "../exceptions/not-found-exception";
export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> { export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> {
const classRepository = getClassRepository(); const cls = await fetchClass(classid);
const cls = await classRepository.findById(classid);
if (!cls) {
return [];
}
const assignmentRepository = getAssignmentRepository(); const assignmentRepository = getAssignmentRepository();
const assignments = await assignmentRepository.findAllAssignmentsInClass(cls); const assignments = await assignmentRepository.findAllAssignmentsInClass(cls);
@ -23,43 +20,34 @@ export async function getAllAssignments(classid: string, full: boolean): Promise
return assignments.map(mapToAssignmentDTOId); return assignments.map(mapToAssignmentDTOId);
} }
export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO | null> { export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO> {
const classRepository = getClassRepository(); const cls = await fetchClass(classid);
const cls = await classRepository.findById(classid);
if (!cls) {
return null;
}
const assignment = mapToAssignment(assignmentData, cls); const assignment = mapToAssignment(assignmentData, cls);
const assignmentRepository = getAssignmentRepository(); const assignmentRepository = getAssignmentRepository();
try {
const newAssignment = assignmentRepository.create(assignment); const newAssignment = assignmentRepository.create(assignment);
await assignmentRepository.save(newAssignment); await assignmentRepository.save(newAssignment, {preventOverwrite: true});
return mapToAssignmentDTO(newAssignment); return mapToAssignmentDTO(newAssignment);
} catch (e) {
getLogger().error(e);
return null;
}
} }
export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO | null> { export async function fetchAssignment(classid: string, id: number): Promise<Assignment> {
const classRepository = getClassRepository(); const cls = await fetchClass(classid);
const cls = await classRepository.findById(classid);
if (!cls) {
return null;
}
const assignmentRepository = getAssignmentRepository(); const assignmentRepository = getAssignmentRepository();
const assignment = await assignmentRepository.findByClassAndId(cls, id); const assignment = await assignmentRepository.findByClassAndId(cls, id);
if (!assignment){ if (!assignment){
return null; throw new NotFoundException('Assignment with id not found');
} }
return assignment;
}
export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO> {
const assignment = await fetchAssignment(classid, id);
return mapToAssignmentDTO(assignment); return mapToAssignmentDTO(assignment);
} }
@ -68,23 +56,15 @@ export async function getAssignmentsSubmissions(
assignmentNumber: number, assignmentNumber: number,
full: boolean full: boolean
): Promise<SubmissionDTO[] | SubmissionDTOId[]> { ): Promise<SubmissionDTO[] | SubmissionDTOId[]> {
const classRepository = getClassRepository(); const assignment = await fetchAssignment(classid, assignmentNumber);
const cls = await classRepository.findById(classid);
if (!cls) {
return [];
}
const assignmentRepository = getAssignmentRepository();
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
if (!assignment) {
return [];
}
const groupRepository = getGroupRepository(); const groupRepository = getGroupRepository();
const groups = await groupRepository.findAllGroupsForAssignment(assignment); const groups = await groupRepository.findAllGroupsForAssignment(assignment);
if (groups.length === 0){
throw new NotFoundException('No groups for assignment found');
}
const submissionRepository = getSubmissionRepository(); const submissionRepository = getSubmissionRepository();
const submissions = (await Promise.all(groups.map(async (group) => submissionRepository.findAllSubmissionsForGroup(group)))).flat(); const submissions = (await Promise.all(groups.map(async (group) => submissionRepository.findAllSubmissionsForGroup(group)))).flat();