feat: (broken) POST method voor groep

This commit is contained in:
Adriaan Jacquet 2025-03-13 15:49:13 +01:00
parent 6ad0e990c7
commit 4c76a82178
6 changed files with 81 additions and 7 deletions

View file

@ -1,9 +1,12 @@
import { GroupRepository } from '../data/assignments/group-repository.js';
import {
getAssignmentRepository,
getClassRepository,
getGroupRepository,
getStudentRepository,
getSubmissionRepository,
} from '../data/repositories.js';
import { Group } from '../entities/assignments/group.entity.js';
import {
GroupDTO,
mapToGroupDTO,
@ -51,6 +54,51 @@ export async function getGroup(
return mapToGroupDTOId(group);
}
export async function createGroup(
groupData: GroupDTO,
classid: string,
assignmentNumber: number,
): Promise<Group | null> {
const studentRepository = getStudentRepository();
const memberUsernames = groupData.members as string[] || []; // TODO check if groupdata.members is a list
const members = (await Promise.all([...memberUsernames].map(id => studentRepository.findByUsername(id))))
.filter(student => student != null);
console.log(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 groupRepository = getGroupRepository();
try {
console.log('EEEEE');
const newGroup = groupRepository.create({
assignment: assignment,
members: members,
});
console.log('OOOOOO');
await groupRepository.save(newGroup);
console.log('AAAAAA');
return newGroup;
} catch(e) {
console.log(e);
return null;
}
}
export async function getAllGroups(
classId: string,
assignmentNumber: number,