diff --git a/backend/src/controllers/assignments.ts b/backend/src/controllers/assignments.ts index 16dbb310..b104c27c 100644 --- a/backend/src/controllers/assignments.ts +++ b/backend/src/controllers/assignments.ts @@ -2,13 +2,7 @@ import { Request, Response } from 'express'; import { createAssignment, getAllAssignments, getAssignment, getAssignmentsSubmissions } from '../services/assignments.js'; import { AssignmentDTO } from '../interfaces/assignment.js'; -// Typescript is annoy with with parameter forwarding from class.ts -interface AssignmentParams { - classid: string; - id: string; -} - -export async function getAllAssignmentsHandler(req: Request, res: Response): Promise { +export async function getAllAssignmentsHandler(req: Request, res: Response): Promise { const classid = req.params.classid; const full = req.query.full === 'true'; @@ -19,7 +13,7 @@ export async function getAllAssignmentsHandler(req: Request, r }); } -export async function createAssignmentHandler(req: Request, res: Response): Promise { +export async function createAssignmentHandler(req: Request, res: Response): Promise { const classid = req.params.classid; const assignmentData = req.body as AssignmentDTO; @@ -40,7 +34,7 @@ export async function createAssignmentHandler(req: Request, re res.status(201).json(assignment); } -export async function getAssignmentHandler(req: Request, res: Response): Promise { +export async function getAssignmentHandler(req: Request, res: Response): Promise { const id = +req.params.id; const classid = req.params.classid; @@ -59,7 +53,7 @@ export async function getAssignmentHandler(req: Request, res: res.json(assignment); } -export async function getAssignmentsSubmissionsHandler(req: Request, res: Response): Promise { +export async function getAssignmentsSubmissionsHandler(req: Request, res: Response): Promise { const classid = req.params.classid; const assignmentNumber = +req.params.id; const full = req.query.full === 'true'; diff --git a/backend/src/services/assignments.ts b/backend/src/services/assignments.ts index 05e8099a..f468b879 100644 --- a/backend/src/services/assignments.ts +++ b/backend/src/services/assignments.ts @@ -1,30 +1,31 @@ import { getAssignmentRepository, getClassRepository, getGroupRepository, getQuestionRepository, getSubmissionRepository } from '../data/repositories.js'; import { Assignment } from '../entities/assignments/assignment.entity.js'; +import { NotFoundException } from '../exceptions/not-found-exception.js'; import { AssignmentDTO, mapToAssignment, mapToAssignmentDTO, mapToAssignmentDTOId } from '../interfaces/assignment.js'; import { mapToQuestionDTO, mapToQuestionId, QuestionDTO, QuestionId } from '../interfaces/question.js'; import { mapToSubmissionDTO, mapToSubmissionDTOId, SubmissionDTO, SubmissionDTOId } from '../interfaces/submission.js'; +import { fetchClass } from './classes.js'; -export async function fetchAssignment(classid: string, assignmentNumber: number): Promise { +export async function fetchAssignment(classid: string, assignmentNumber: number): Promise { const classRepository = getClassRepository(); const cls = await classRepository.findById(classid); if (!cls) { - return null; + throw new NotFoundException('Could not find assignment\'s class'); } const assignmentRepository = getAssignmentRepository(); const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber); + if (!assignment) { + throw new NotFoundException('Could not find assignment'); + } + return assignment; } export async function getAllAssignments(classid: string, full: boolean): Promise { - const classRepository = getClassRepository(); - const cls = await classRepository.findById(classid); - - if (!cls) { - return []; - } + const cls = await fetchClass(classid); const assignmentRepository = getAssignmentRepository(); const assignments = await assignmentRepository.findAllAssignmentsInClass(cls); diff --git a/backend/src/services/classes.ts b/backend/src/services/classes.ts index dcf6a432..7735b08d 100644 --- a/backend/src/services/classes.ts +++ b/backend/src/services/classes.ts @@ -8,7 +8,7 @@ import { getLogger } from '../logging/initalize.js'; const logger = getLogger(); -async function fetchClass(classid: string): Promise { +export async function fetchClass(classid: string): Promise { const classRepository = getClassRepository(); const cls = await classRepository.findById(classid);