fix: werkende aan 183 fix ([] vervangen door Collection<> in group en assignment)

This commit is contained in:
Adriaan Jacquet 2025-04-12 16:38:16 +02:00
parent 69659426de
commit c4729156ba
6 changed files with 38 additions and 24 deletions

View file

@ -21,6 +21,8 @@ export async function getAllAssignmentsHandler(req: Request, res: Response): Pro
const assignments = await getAllAssignments(classId, full);
console.log(JSON.stringify(assignments));
res.json({ assignments });
}
@ -34,12 +36,8 @@ export async function createAssignmentHandler(req: Request, res: Response): Prom
requireFields({ description, language, learningPath, title });
const assignmentData = req.body as AssignmentDTO;
Object.entries(assignmentData).forEach(getLogger().info);
const assignment = await createAssignment(classid, assignmentData);
// should probably use Promise.all
//assignmentData.groups.forEach(group => await createGroup({}, classid, assignment.id));
res.json({ assignment });
}

View file

@ -69,8 +69,8 @@ export async function getAllGroupsHandler(req: Request, res: Response): Promise<
export async function createGroupHandler(req: Request, res: Response): Promise<void> {
const classid = req.params.classid;
const assignmentId = Number(req.params.assignmentid);
requireFields({ classid, assignmentId });
const members = req.body.members;
requireFields({ classid, assignmentId, members });
if (isNaN(assignmentId)) {
throw new BadRequestException('Assignment id must be a number');

View file

@ -1,4 +1,4 @@
import { Entity, ManyToMany, ManyToOne, PrimaryKey } from '@mikro-orm/core';
import { Collection, Entity, ManyToMany, ManyToOne, PrimaryKey } from '@mikro-orm/core';
import { Assignment } from './assignment.entity.js';
import { Student } from '../users/student.entity.js';
import { GroupRepository } from '../../data/assignments/group-repository.js';
@ -19,5 +19,5 @@ export class Group {
@ManyToMany({
entity: () => Student,
})
members!: Student[];
members!: Collection<Student>;
}

View file

@ -4,6 +4,7 @@ import {
getClassRepository,
getGroupRepository,
getQuestionRepository,
getStudentRepository,
getSubmissionRepository,
} from '../data/repositories.js';
import { Assignment } from '../entities/assignments/assignment.entity.js';
@ -18,6 +19,8 @@ import { assign, EntityDTO } from '@mikro-orm/core';
import { putObject } from './service-helper.js';
import { getLogger } from '../logging/initalize.js';
import { languageMap } from '@dwengo-1/common/util/language';
import { createGroup } from './groups.js';
import { GroupDTO } from 'dwengo-1-common/interfaces/group';
export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> {
const classRepository = getClassRepository();
@ -54,24 +57,30 @@ export async function createAssignment(classid: string, assignmentData: Assignme
const cls = await fetchClass(classid);
const assignmentRepository = getAssignmentRepository();
const assignment = assignmentRepository.create({
within: cls,
title: assignmentData.title,
description: assignmentData.description,
learningPathHruid: assignmentData.learningPath,
learningPathLanguage: languageMap[assignmentData.language],
groups: [],
})
// const assignment = mapToAssignment(assignmentData, cls);
Object.entries(assignmentData).forEach(getLogger().info);
const assignment = mapToAssignment(assignmentData, cls);
await assignmentRepository.save(assignment);
try {
await assignmentRepository.save(assignment, { preventOverwrite: true });
} catch(e) {
getLogger().error(e);
/*
if (assignmentData.groups) {
const groupRepository = getGroupRepository();
const studentRepository = getStudentRepository();
(assignmentData.groups as string[][]).forEach(async (memberUsernames) => {
const members = (await Promise.all(memberUsernames.map(async (id) => studentRepository.findByUsername(id)))).filter(
(student) => student !== null
);
const newGroup = groupRepository.create({
assignment: assignment,
members: members,
});
await groupRepository.save(newGroup);
console.log('NEW GROUP');
console.log(newGroup);
});
}
*/
getLogger().info(`Saved assignment ${assignment.id}`);
return mapToAssignmentDTO(assignment);
}

View file

@ -66,15 +66,22 @@ export async function createGroup(groupData: GroupDTO, classid: string, assignme
(student) => student !== null
);
console.log(members);
const assignment = await fetchAssignment(classid, assignmentNumber);
console.log(assignment);
const groupRepository = getGroupRepository();
const newGroup = groupRepository.create({
assignment: assignment,
members: members,
});
console.log(newGroup.assignment);
await groupRepository.save(newGroup);
console.log(newGroup.assignment);
return mapToGroupDTO(newGroup);
}

View file

@ -7,5 +7,5 @@ export interface AssignmentDTO {
description: string;
learningPath: string;
language: string;
groups: GroupDTO[] | string[];
groups: GroupDTO[] | string[][];
}