114 lines
No EOL
3.7 KiB
TypeScript
114 lines
No EOL
3.7 KiB
TypeScript
import { getAssignmentRepository, getClassRepository, getGroupRepository, getQuestionRepository, getSubmissionRepository } from '../data/repositories.js';
|
|
import { Assignment } from '../entities/assignments/assignment.entity.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';
|
|
|
|
export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment | null> {
|
|
const classRepository = getClassRepository();
|
|
const cls = await classRepository.findById(classid);
|
|
|
|
if (!cls) {
|
|
return null;
|
|
}
|
|
|
|
const assignmentRepository = getAssignmentRepository();
|
|
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
|
|
|
return assignment;
|
|
}
|
|
|
|
export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> {
|
|
const classRepository = getClassRepository();
|
|
const cls = await classRepository.findById(classid);
|
|
|
|
if (!cls) {
|
|
return [];
|
|
}
|
|
|
|
const assignmentRepository = getAssignmentRepository();
|
|
const assignments = await assignmentRepository.findAllAssignmentsInClass(cls);
|
|
|
|
if (full) {
|
|
return assignments.map(mapToAssignmentDTO);
|
|
}
|
|
|
|
return assignments.map(mapToAssignmentDTOId);
|
|
}
|
|
|
|
export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO | null> {
|
|
const classRepository = getClassRepository();
|
|
const cls = await classRepository.findById(classid);
|
|
|
|
if (!cls) {
|
|
return null;
|
|
}
|
|
|
|
const assignment = mapToAssignment(assignmentData, cls);
|
|
const assignmentRepository = getAssignmentRepository();
|
|
|
|
try {
|
|
const newAssignment = assignmentRepository.create(assignment);
|
|
await assignmentRepository.save(newAssignment);
|
|
|
|
return mapToAssignmentDTO(newAssignment);
|
|
} catch (e) {
|
|
console.error(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO | null> {
|
|
const assignment = await fetchAssignment(classid, id);
|
|
|
|
if (!assignment) {
|
|
return null;
|
|
}
|
|
|
|
return mapToAssignmentDTO(assignment);
|
|
}
|
|
|
|
export async function getAssignmentsSubmissions(
|
|
classid: string,
|
|
assignmentNumber: number,
|
|
full: boolean
|
|
): Promise<SubmissionDTO[] | SubmissionDTOId[]> {
|
|
const assignment = await fetchAssignment(classid, assignmentNumber);
|
|
|
|
if (!assignment) {
|
|
return [];
|
|
}
|
|
|
|
const groupRepository = getGroupRepository();
|
|
const groups = await groupRepository.findAllGroupsForAssignment(assignment);
|
|
|
|
const submissionRepository = getSubmissionRepository();
|
|
const submissions = (await Promise.all(groups.map((group) => submissionRepository.findAllSubmissionsForGroup(group)))).flat();
|
|
|
|
if (full) {
|
|
return submissions.map(mapToSubmissionDTO);
|
|
}
|
|
|
|
return submissions.map(mapToSubmissionDTOId);
|
|
}
|
|
|
|
export async function getAssignmentsQuestions(
|
|
classid: string,
|
|
assignmentNumber: number,
|
|
full: boolean
|
|
): Promise<QuestionDTO[] | QuestionId[]> {
|
|
const assignment = await fetchAssignment(classid, assignmentNumber);
|
|
|
|
if (!assignment) {
|
|
return [];
|
|
}
|
|
|
|
const questionRepository = getQuestionRepository();
|
|
const questions = await questionRepository.findAllByAssignment(assignment);
|
|
|
|
if (full) {
|
|
return questions.map(mapToQuestionDTO);
|
|
}
|
|
|
|
return questions.map(mapToQuestionDTO).map(mapToQuestionId); // mapToQuestionId should be updated
|
|
} |