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); const assignments = await getAllAssignments(classId, full);
console.log(JSON.stringify(assignments));
res.json({ assignments }); res.json({ assignments });
} }
@ -34,12 +36,8 @@ export async function createAssignmentHandler(req: Request, res: Response): Prom
requireFields({ description, language, learningPath, title }); requireFields({ description, language, learningPath, title });
const assignmentData = req.body as AssignmentDTO; const assignmentData = req.body as AssignmentDTO;
Object.entries(assignmentData).forEach(getLogger().info);
const assignment = await createAssignment(classid, assignmentData); const assignment = await createAssignment(classid, assignmentData);
// should probably use Promise.all
//assignmentData.groups.forEach(group => await createGroup({}, classid, assignment.id));
res.json({ assignment }); 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> { export async function createGroupHandler(req: Request, res: Response): Promise<void> {
const classid = req.params.classid; const classid = req.params.classid;
const assignmentId = Number(req.params.assignmentid); const assignmentId = Number(req.params.assignmentid);
const members = req.body.members;
requireFields({ classid, assignmentId }); requireFields({ classid, assignmentId, members });
if (isNaN(assignmentId)) { if (isNaN(assignmentId)) {
throw new BadRequestException('Assignment id must be a number'); 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 { Assignment } from './assignment.entity.js';
import { Student } from '../users/student.entity.js'; import { Student } from '../users/student.entity.js';
import { GroupRepository } from '../../data/assignments/group-repository.js'; import { GroupRepository } from '../../data/assignments/group-repository.js';
@ -19,5 +19,5 @@ export class Group {
@ManyToMany({ @ManyToMany({
entity: () => Student, entity: () => Student,
}) })
members!: Student[]; members!: Collection<Student>;
} }

View file

@ -4,6 +4,7 @@ import {
getClassRepository, getClassRepository,
getGroupRepository, getGroupRepository,
getQuestionRepository, getQuestionRepository,
getStudentRepository,
getSubmissionRepository, getSubmissionRepository,
} from '../data/repositories.js'; } from '../data/repositories.js';
import { Assignment } from '../entities/assignments/assignment.entity.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 { putObject } from './service-helper.js';
import { getLogger } from '../logging/initalize.js'; import { getLogger } from '../logging/initalize.js';
import { languageMap } from '@dwengo-1/common/util/language'; 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> { export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> {
const classRepository = getClassRepository(); const classRepository = getClassRepository();
@ -54,24 +57,30 @@ export async function createAssignment(classid: string, assignmentData: Assignme
const cls = await fetchClass(classid); const cls = await fetchClass(classid);
const assignmentRepository = getAssignmentRepository(); const assignmentRepository = getAssignmentRepository();
const assignment = assignmentRepository.create({ const assignment = mapToAssignment(assignmentData, cls);
within: cls, await assignmentRepository.save(assignment);
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);
try { /*
await assignmentRepository.save(assignment, { preventOverwrite: true }); if (assignmentData.groups) {
} catch(e) { const groupRepository = getGroupRepository();
getLogger().error(e); 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); return mapToAssignmentDTO(assignment);
} }

View file

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

View file

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