fix: werkende aan assignment fix
This commit is contained in:
parent
45ca433e09
commit
69659426de
6 changed files with 44 additions and 21 deletions
|
@ -12,6 +12,8 @@ import { requireFields } from './error-helper.js';
|
|||
import { BadRequestException } from '../exceptions/bad-request-exception.js';
|
||||
import { Assignment } from '../entities/assignments/assignment.entity.js';
|
||||
import { EntityDTO } from '@mikro-orm/core';
|
||||
import { createGroup } from '../services/groups.js';
|
||||
import { getLogger } from '../logging/initalize.js';
|
||||
|
||||
export async function getAllAssignmentsHandler(req: Request, res: Response): Promise<void> {
|
||||
const classId = req.params.classid;
|
||||
|
@ -32,8 +34,12 @@ 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 });
|
||||
}
|
||||
|
||||
|
|
|
@ -4,10 +4,10 @@ import { Class } from '../../entities/classes/class.entity.js';
|
|||
|
||||
export class AssignmentRepository extends DwengoEntityRepository<Assignment> {
|
||||
public async findByClassAndId(within: Class, id: number): Promise<Assignment | null> {
|
||||
return this.findOne({ within: within, id: id });
|
||||
return this.findOne({ within: within, id: id }, { populate: [ "groups" ]});
|
||||
}
|
||||
public async findAllAssignmentsInClass(within: Class): Promise<Assignment[]> {
|
||||
return this.findAll({ where: { within: within } });
|
||||
return this.findAll({ where: { within: within }, populate: [ "groups" ] });
|
||||
}
|
||||
public async deleteByClassAndId(within: Class, id: number): Promise<void> {
|
||||
return this.deleteWhere({ within: within, id: id });
|
||||
|
|
|
@ -14,7 +14,7 @@ export class Assignment {
|
|||
})
|
||||
within!: Class;
|
||||
|
||||
@PrimaryKey({ type: 'number', autoincrement: true })
|
||||
@PrimaryKey({ type: 'integer', autoincrement: true })
|
||||
id?: number;
|
||||
|
||||
@Property({ type: 'string' })
|
||||
|
|
|
@ -4,6 +4,9 @@ import { Assignment } from '../entities/assignments/assignment.entity.js';
|
|||
import { Class } from '../entities/classes/class.entity.js';
|
||||
import { getLogger } from '../logging/initalize.js';
|
||||
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
|
||||
import { mapToGroupDTO, mapToGroupDTOId } from './group.js';
|
||||
import { getAssignmentHandler } from '../controllers/assignments.js';
|
||||
import { getAssignmentRepository, getClassRepository } from '../data/repositories.js';
|
||||
|
||||
export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO {
|
||||
return {
|
||||
|
@ -13,6 +16,7 @@ export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO {
|
|||
description: assignment.description,
|
||||
learningPath: assignment.learningPathHruid,
|
||||
language: assignment.learningPathLanguage,
|
||||
groups: assignment.groups.map(mapToGroupDTOId),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -24,19 +28,17 @@ export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO {
|
|||
description: assignment.description,
|
||||
learningPath: assignment.learningPathHruid,
|
||||
language: assignment.learningPathLanguage,
|
||||
// Groups: assignment.groups.map(mapToGroupDTO),
|
||||
groups: assignment.groups.map(mapToGroupDTO),
|
||||
};
|
||||
}
|
||||
|
||||
export function mapToAssignment(assignmentData: AssignmentDTO, cls: Class): Assignment {
|
||||
const assignment = new Assignment();
|
||||
assignment.title = assignmentData.title;
|
||||
assignment.description = assignmentData.description;
|
||||
assignment.learningPathHruid = assignmentData.learningPath;
|
||||
assignment.learningPathLanguage = languageMap[assignmentData.language] || FALLBACK_LANG;
|
||||
assignment.within = cls;
|
||||
|
||||
getLogger().debug(assignment);
|
||||
|
||||
return assignment;
|
||||
return getAssignmentRepository().create({
|
||||
within: cls,
|
||||
title: assignmentData.title,
|
||||
description: assignmentData.description,
|
||||
learningPathHruid: assignmentData.learningPath,
|
||||
learningPathLanguage: languageMap[assignmentData.language],
|
||||
groups: [],
|
||||
})
|
||||
}
|
||||
|
|
|
@ -14,8 +14,10 @@ import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submissi
|
|||
import { fetchClass } from './classes.js';
|
||||
import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question';
|
||||
import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission';
|
||||
import { EntityDTO } from '@mikro-orm/core';
|
||||
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';
|
||||
|
||||
export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> {
|
||||
const classRepository = getClassRepository();
|
||||
|
@ -51,13 +53,26 @@ export async function getAllAssignments(classid: string, full: boolean): Promise
|
|||
export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO> {
|
||||
const cls = await fetchClass(classid);
|
||||
|
||||
const assignment = mapToAssignment(assignmentData, cls);
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const newAssignment = assignmentRepository.create(assignment);
|
||||
await assignmentRepository.save(newAssignment, { preventOverwrite: true });
|
||||
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);
|
||||
|
||||
return mapToAssignmentDTO(newAssignment);
|
||||
try {
|
||||
await assignmentRepository.save(assignment, { preventOverwrite: true });
|
||||
} catch(e) {
|
||||
getLogger().error(e);
|
||||
}
|
||||
|
||||
getLogger().info(`Saved assignment ${assignment.id}`);
|
||||
return mapToAssignmentDTO(assignment);
|
||||
}
|
||||
|
||||
export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO> {
|
||||
|
|
|
@ -7,5 +7,5 @@ export interface AssignmentDTO {
|
|||
description: string;
|
||||
learningPath: string;
|
||||
language: string;
|
||||
groups?: GroupDTO[] | string[]; // TODO
|
||||
groups: GroupDTO[] | string[];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue