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 { BadRequestException } from '../exceptions/bad-request-exception.js'; | ||||||
| import { Assignment } from '../entities/assignments/assignment.entity.js'; | import { Assignment } from '../entities/assignments/assignment.entity.js'; | ||||||
| import { EntityDTO } from '@mikro-orm/core'; | 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> { | export async function getAllAssignmentsHandler(req: Request, res: Response): Promise<void> { | ||||||
|     const classId = req.params.classid; |     const classId = req.params.classid; | ||||||
|  | @ -32,8 +34,12 @@ 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 }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -4,10 +4,10 @@ import { Class } from '../../entities/classes/class.entity.js'; | ||||||
| 
 | 
 | ||||||
| export class AssignmentRepository extends DwengoEntityRepository<Assignment> { | export class AssignmentRepository extends DwengoEntityRepository<Assignment> { | ||||||
|     public async findByClassAndId(within: Class, id: number): Promise<Assignment | null> { |     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[]> { |     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> { |     public async deleteByClassAndId(within: Class, id: number): Promise<void> { | ||||||
|         return this.deleteWhere({ within: within, id: id }); |         return this.deleteWhere({ within: within, id: id }); | ||||||
|  |  | ||||||
|  | @ -14,7 +14,7 @@ export class Assignment { | ||||||
|     }) |     }) | ||||||
|     within!: Class; |     within!: Class; | ||||||
| 
 | 
 | ||||||
|     @PrimaryKey({ type: 'number', autoincrement: true }) |     @PrimaryKey({ type: 'integer', autoincrement: true }) | ||||||
|     id?: number; |     id?: number; | ||||||
| 
 | 
 | ||||||
|     @Property({ type: 'string' }) |     @Property({ type: 'string' }) | ||||||
|  |  | ||||||
|  | @ -4,6 +4,9 @@ import { Assignment } from '../entities/assignments/assignment.entity.js'; | ||||||
| import { Class } from '../entities/classes/class.entity.js'; | import { Class } from '../entities/classes/class.entity.js'; | ||||||
| import { getLogger } from '../logging/initalize.js'; | import { getLogger } from '../logging/initalize.js'; | ||||||
| import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment'; | 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 { | export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { | ||||||
|     return { |     return { | ||||||
|  | @ -13,6 +16,7 @@ export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { | ||||||
|         description: assignment.description, |         description: assignment.description, | ||||||
|         learningPath: assignment.learningPathHruid, |         learningPath: assignment.learningPathHruid, | ||||||
|         language: assignment.learningPathLanguage, |         language: assignment.learningPathLanguage, | ||||||
|  |         groups: assignment.groups.map(mapToGroupDTOId), | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -24,19 +28,17 @@ export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | ||||||
|         description: assignment.description, |         description: assignment.description, | ||||||
|         learningPath: assignment.learningPathHruid, |         learningPath: assignment.learningPathHruid, | ||||||
|         language: assignment.learningPathLanguage, |         language: assignment.learningPathLanguage, | ||||||
|         // Groups: assignment.groups.map(mapToGroupDTO),
 |         groups: assignment.groups.map(mapToGroupDTO), | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function mapToAssignment(assignmentData: AssignmentDTO, cls: Class): Assignment { | export function mapToAssignment(assignmentData: AssignmentDTO, cls: Class): Assignment { | ||||||
|     const assignment = new Assignment(); |     return getAssignmentRepository().create({ | ||||||
|     assignment.title = assignmentData.title; |         within: cls, | ||||||
|     assignment.description = assignmentData.description; |         title: assignmentData.title, | ||||||
|     assignment.learningPathHruid = assignmentData.learningPath; |         description: assignmentData.description, | ||||||
|     assignment.learningPathLanguage = languageMap[assignmentData.language] || FALLBACK_LANG; |         learningPathHruid: assignmentData.learningPath, | ||||||
|     assignment.within = cls; |         learningPathLanguage: languageMap[assignmentData.language], | ||||||
| 
 |         groups: [], | ||||||
|     getLogger().debug(assignment); |     }) | ||||||
| 
 |  | ||||||
|     return assignment; |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -14,8 +14,10 @@ import { mapToSubmissionDTO, mapToSubmissionDTOId } from '../interfaces/submissi | ||||||
| import { fetchClass } from './classes.js'; | import { fetchClass } from './classes.js'; | ||||||
| import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | import { QuestionDTO, QuestionId } from '@dwengo-1/common/interfaces/question'; | ||||||
| import { SubmissionDTO, SubmissionDTOId } from '@dwengo-1/common/interfaces/submission'; | 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 { 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> { | export async function fetchAssignment(classid: string, assignmentNumber: number): Promise<Assignment> { | ||||||
|     const classRepository = getClassRepository(); |     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> { | export async function createAssignment(classid: string, assignmentData: AssignmentDTO): Promise<AssignmentDTO> { | ||||||
|     const cls = await fetchClass(classid); |     const cls = await fetchClass(classid); | ||||||
| 
 | 
 | ||||||
|     const assignment = mapToAssignment(assignmentData, cls); |  | ||||||
| 
 |  | ||||||
|     const assignmentRepository = getAssignmentRepository(); |     const assignmentRepository = getAssignmentRepository(); | ||||||
|     const newAssignment = assignmentRepository.create(assignment); |     const assignment = assignmentRepository.create({ | ||||||
|     await assignmentRepository.save(newAssignment, { preventOverwrite: true }); |         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> { | export async function getAssignment(classid: string, id: number): Promise<AssignmentDTO> { | ||||||
|  |  | ||||||
|  | @ -7,5 +7,5 @@ export interface AssignmentDTO { | ||||||
|     description: string; |     description: string; | ||||||
|     learningPath: string; |     learningPath: string; | ||||||
|     language: string; |     language: string; | ||||||
|     groups?: GroupDTO[] | string[]; // TODO
 |     groups: GroupDTO[] | string[]; | ||||||
| } | } | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Adriaan Jacquet
						Adriaan Jacquet