feat: submission full backend stack opgekuist
This commit is contained in:
parent
e562fad385
commit
dde672befd
7 changed files with 74 additions and 95 deletions
|
|
@ -26,7 +26,7 @@ export async function fetchClass(classid: string): Promise<Class> {
|
|||
|
||||
export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[]> {
|
||||
const classRepository = getClassRepository();
|
||||
const classes = await classRepository.find({}, { populate: ['students', 'teachers'] });
|
||||
const classes = await classRepository.findAll({ populate: ['students', 'teachers'] });
|
||||
|
||||
if (full) {
|
||||
return classes.map(mapToClassDTO);
|
||||
|
|
@ -34,13 +34,12 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[
|
|||
return classes.map((cls) => cls.classId!);
|
||||
}
|
||||
|
||||
export async function getClass(classId: string): Promise<ClassDTO | null> {
|
||||
export async function getClass(classId: string): Promise<ClassDTO> {
|
||||
const cls = await fetchClass(classId);
|
||||
|
||||
return mapToClassDTO(cls);
|
||||
}
|
||||
|
||||
export async function createClass(classData: ClassDTO): Promise<ClassDTO | null> {
|
||||
export async function createClass(classData: ClassDTO): Promise<ClassDTO> {
|
||||
const teacherUsernames = classData.teachers || [];
|
||||
const teachers = (await Promise.all(teacherUsernames.map(async (id) => fetchTeacher(id) )));
|
||||
|
||||
|
|
@ -48,7 +47,6 @@ export async function createClass(classData: ClassDTO): Promise<ClassDTO | null>
|
|||
const students = (await Promise.all(studentUsernames.map(async (id) => fetchStudent(id) )));
|
||||
|
||||
const classRepository = getClassRepository();
|
||||
|
||||
const newClass = classRepository.create({
|
||||
displayName: classData.displayName,
|
||||
teachers: teachers,
|
||||
|
|
|
|||
|
|
@ -14,80 +14,58 @@ import { GroupDTO } from '@dwengo-1/common/interfaces/group';
|
|||
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
|
||||
import { getLogger } from '../logging/initalize.js';
|
||||
import { fetchAssignment } from './assignments.js';
|
||||
import { NotFoundException } from '../exceptions/not-found-exception.js';
|
||||
import { fetchClass } from './classes.js';
|
||||
|
||||
async function fetchGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<Group | null> {
|
||||
export async function fetchGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<Group> {
|
||||
const assignment = await fetchAssignment(classId, assignmentNumber);
|
||||
|
||||
if (!assignment) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
const group = await groupRepository.findByAssignmentAndGroupNumber(assignment, groupNumber);
|
||||
|
||||
if (!group) {
|
||||
throw new NotFoundException('Could not find group');
|
||||
}
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
export async function getGroup(classId: string, assignmentNumber: number, groupNumber: number, full: boolean): Promise<GroupDTO | null> {
|
||||
export async function getGroup(classId: string, assignmentNumber: number, groupNumber: number): Promise<GroupDTO> {
|
||||
const group = await fetchGroup(classId, assignmentNumber, groupNumber);
|
||||
|
||||
if (!group) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (full) {
|
||||
return mapToGroupDTO(group);
|
||||
}
|
||||
|
||||
return mapToGroupDTOId(group);
|
||||
return mapToGroupDTO(group);
|
||||
}
|
||||
|
||||
export async function createGroup(groupData: GroupDTO, classid: string, assignmentNumber: number): Promise<Group | null> {
|
||||
export async function getExistingGroupFromGroupDTO(groupData: GroupDTO) {
|
||||
const classId = typeof(groupData.class) === 'string' ? groupData.class : groupData.class.id;
|
||||
const assignmentNumber = typeof(groupData.assignment) === 'number' ? groupData.assignment : groupData.assignment.id;
|
||||
const groupNumber = groupData.groupNumber;
|
||||
|
||||
return await fetchGroup(classId, assignmentNumber, groupNumber);
|
||||
}
|
||||
|
||||
export async function createGroup(groupData: GroupDTO, classid: string, assignmentNumber: number): Promise<GroupDTO> {
|
||||
const studentRepository = getStudentRepository();
|
||||
|
||||
const memberUsernames = (groupData.members as string[]) || []; // TODO check if groupdata.members is a list
|
||||
const memberUsernames = (groupData.members as string[]) || [];
|
||||
const members = (await Promise.all([...memberUsernames].map(async (id) => studentRepository.findByUsername(id)))).filter(
|
||||
(student) => student !== null
|
||||
);
|
||||
|
||||
getLogger().debug(members);
|
||||
|
||||
const classRepository = getClassRepository();
|
||||
const cls = await classRepository.findById(classid);
|
||||
|
||||
if (!cls) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, assignmentNumber);
|
||||
|
||||
if (!assignment) {
|
||||
return null;
|
||||
}
|
||||
const assignment = await fetchAssignment(classid, assignmentNumber);
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
try {
|
||||
const newGroup = groupRepository.create({
|
||||
assignment: assignment,
|
||||
members: members,
|
||||
});
|
||||
await groupRepository.save(newGroup);
|
||||
const newGroup = groupRepository.create({
|
||||
assignment: assignment,
|
||||
members: members,
|
||||
});
|
||||
await groupRepository.save(newGroup);
|
||||
|
||||
return newGroup;
|
||||
} catch (e) {
|
||||
getLogger().error(e);
|
||||
return null;
|
||||
}
|
||||
return mapToGroupDTO(newGroup);
|
||||
}
|
||||
|
||||
export async function getAllGroups(classId: string, assignmentNumber: number, full: boolean): Promise<GroupDTO[]> {
|
||||
const assignment = await fetchAssignment(classId, assignmentNumber);
|
||||
|
||||
if (!assignment) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const groupRepository = getGroupRepository();
|
||||
const groups = await groupRepository.findAllGroupsForAssignment(assignment);
|
||||
|
||||
|
|
@ -106,10 +84,6 @@ export async function getGroupSubmissions(
|
|||
): Promise<SubmissionDTO[] | SubmissionDTOId[]> {
|
||||
const group = await fetchGroup(classId, assignmentNumber, groupNumber);
|
||||
|
||||
if (!group) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submissions = await submissionRepository.findAllSubmissionsForGroup(group);
|
||||
|
||||
|
|
|
|||
|
|
@ -4,11 +4,14 @@ import { NotFoundException } from '../exceptions/not-found-exception.js';
|
|||
import { mapToSubmission, mapToSubmissionDTO } from '../interfaces/submission.js';
|
||||
import { SubmissionDTO } from '@dwengo-1/common/interfaces/submission';
|
||||
import { Language } from '@dwengo-1/common/util/language';
|
||||
import { fetchStudent } from './students.js';
|
||||
import { fetchGroup, getExistingGroupFromGroupDTO } from './groups.js';
|
||||
import { Submission } from '../entities/assignments/submission.entity.js';
|
||||
|
||||
export async function getSubmission(
|
||||
export async function fetchSubmission(
|
||||
loId: LearningObjectIdentifier,
|
||||
submissionNumber: number
|
||||
): Promise<SubmissionDTO> {
|
||||
submissionNumber: number,
|
||||
): Promise<Submission> {
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submission = await submissionRepository.findSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber);
|
||||
|
||||
|
|
@ -16,6 +19,14 @@ export async function getSubmission(
|
|||
throw new NotFoundException('Could not find submission');
|
||||
}
|
||||
|
||||
return submission;
|
||||
}
|
||||
|
||||
export async function getSubmission(
|
||||
loId: LearningObjectIdentifier,
|
||||
submissionNumber: number
|
||||
): Promise<SubmissionDTO> {
|
||||
const submission = await fetchSubmission(loId, submissionNumber);
|
||||
return mapToSubmissionDTO(submission);
|
||||
}
|
||||
|
||||
|
|
@ -28,30 +39,22 @@ export async function getAllSubmissions(
|
|||
return submissions.map(mapToSubmissionDTO);
|
||||
}
|
||||
|
||||
export async function createSubmission(submissionDTO: SubmissionDTO): Promise<SubmissionDTO | null> {
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submission = mapToSubmission(submissionDTO);
|
||||
export async function createSubmission(submissionDTO: SubmissionDTO): Promise<SubmissionDTO> {
|
||||
const submitter = await fetchStudent(submissionDTO.submitter.username);
|
||||
const group = submissionDTO.group ? await getExistingGroupFromGroupDTO(submissionDTO.group) : undefined;
|
||||
|
||||
try {
|
||||
const newSubmission = submissionRepository.create(submission);
|
||||
await submissionRepository.save(newSubmission);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
const submission = mapToSubmission(submissionDTO, submitter, group);
|
||||
await submissionRepository.save(submission);
|
||||
|
||||
return mapToSubmissionDTO(submission);
|
||||
}
|
||||
|
||||
export async function deleteSubmission(loId: LearningObjectIdentifier, submissionNumber: number): Promise<SubmissionDTO> {
|
||||
const submission = await fetchSubmission(loId, submissionNumber);
|
||||
|
||||
const submissionRepository = getSubmissionRepository();
|
||||
|
||||
const submission = await getSubmission(loId, submissionNumber);
|
||||
|
||||
if (!submission) {
|
||||
throw new NotFoundException('Could not delete submission because it does not exist');
|
||||
}
|
||||
|
||||
await submissionRepository.deleteSubmissionByLearningObjectAndSubmissionNumber(loId, submissionNumber);
|
||||
|
||||
return submission;
|
||||
return mapToSubmissionDTO(submission);
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue