Merge remote-tracking branch 'origin/feat/service-layer' into feat/service-layer
This commit is contained in:
		
						commit
						70ba819e46
					
				
					 5 changed files with 81 additions and 5 deletions
				
			
		|  | @ -1,5 +1,7 @@ | |||
| import { Request, Response } from 'express'; | ||||
| import { getAllAssignments, getAssignment, getAssignmentsSubmissions } from '../services/assignments.js'; | ||||
| import { createAssignment, getAllAssignments, getAssignment, getAssignmentsSubmissions } from '../services/assignments.js'; | ||||
| import { AssignmentDTO, mapToAssignment, mapToAssignmentDTO } from '../interfaces/assignment.js'; | ||||
| import { getAssignmentRepository, getClassRepository } from '../data/repositories.js'; | ||||
| 
 | ||||
| // Typescript is annoy with with parameter forwarding from class.ts
 | ||||
| interface AssignmentParams { | ||||
|  | @ -21,6 +23,34 @@ export async function getAllAssignmentsHandler( | |||
|     }); | ||||
| } | ||||
| 
 | ||||
| export async function createAssignmentHandler( | ||||
|     req: Request<AssignmentParams>, | ||||
|     res: Response, | ||||
| ): Promise<void> { | ||||
|     const classid = req.params.classid; | ||||
|     const assignmentData = req.body as AssignmentDTO; | ||||
| 
 | ||||
|     if (!assignmentData.description  | ||||
|         || !assignmentData.language  | ||||
|         || !assignmentData.learningPath  | ||||
|         || !assignmentData.title | ||||
|     ) { | ||||
|         res.status(400).json({ | ||||
|             error: 'Missing one or more required fields: title, description, learningPath, title', | ||||
|         }); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     const assignment = createAssignment(classid, assignmentData); | ||||
| 
 | ||||
|     if (!assignment) { | ||||
|         res.status(500).json({ error: "Could not create assignment "}); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     res.status(201).json({ assignment: assignment }); | ||||
| } | ||||
| 
 | ||||
| export async function getAssignmentHandler( | ||||
|     req: Request<AssignmentParams>, | ||||
|     res: Response | ||||
|  |  | |||
|  | @ -25,8 +25,8 @@ export class Assignment { | |||
|     }) | ||||
|     within!: Class; | ||||
| 
 | ||||
|     @PrimaryKey({ type: 'number' }) | ||||
|     id!: number; | ||||
|     @PrimaryKey({ type: 'number', autoincrement: true }) | ||||
|     id?: number; | ||||
| 
 | ||||
|     @Property({ type: 'string' }) | ||||
|     title!: string; | ||||
|  |  | |||
|  | @ -1,4 +1,7 @@ | |||
| import { FALLBACK_LANG } from '../config.js'; | ||||
| import { Assignment } from '../entities/assignments/assignment.entity.js'; | ||||
| import { Class } from '../entities/classes/class.entity.js'; | ||||
| import { languageMap } from '../entities/content/language.js'; | ||||
| import { GroupDTO, mapToGroupDTO } from './group.js'; | ||||
| 
 | ||||
| export interface AssignmentDTO { | ||||
|  | @ -13,7 +16,7 @@ export interface AssignmentDTO { | |||
| 
 | ||||
| export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { | ||||
|     return { | ||||
|         id: assignment.id, | ||||
|         id: assignment.id!, | ||||
|         class: assignment.within.classId, | ||||
|         title: assignment.title, | ||||
|         description: assignment.description, | ||||
|  | @ -25,7 +28,7 @@ export function mapToAssignmentDTOId(assignment: Assignment): AssignmentDTO { | |||
| 
 | ||||
| export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | ||||
|     return { | ||||
|         id: assignment.id, | ||||
|         id: assignment.id!, | ||||
|         class: assignment.within.classId, | ||||
|         title: assignment.title, | ||||
|         description: assignment.description, | ||||
|  | @ -34,3 +37,16 @@ export function mapToAssignmentDTO(assignment: Assignment): AssignmentDTO { | |||
|         // 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; | ||||
| 
 | ||||
|     console.log(assignment); | ||||
| 
 | ||||
|     return assignment; | ||||
| } | ||||
|  |  | |||
|  | @ -1,5 +1,6 @@ | |||
| import express from 'express'; | ||||
| import { | ||||
|     createAssignmentHandler, | ||||
|     getAllAssignmentsHandler, | ||||
|     getAssignmentHandler, | ||||
|     getAssignmentsSubmissionsHandler, | ||||
|  | @ -11,6 +12,8 @@ const router = express.Router({ mergeParams: true }); | |||
| // Root endpoint used to search objects
 | ||||
| router.get('/', getAllAssignmentsHandler); | ||||
| 
 | ||||
| router.post('/', createAssignmentHandler); | ||||
| 
 | ||||
| // Information about an assignment with id 'id'
 | ||||
| router.get('/:id', getAssignmentHandler); | ||||
| 
 | ||||
|  |  | |||
|  | @ -4,8 +4,10 @@ import { | |||
|     getGroupRepository, | ||||
|     getSubmissionRepository, | ||||
| } from '../data/repositories.js'; | ||||
| import { Assignment } from '../entities/assignments/assignment.entity.js'; | ||||
| import { | ||||
|     AssignmentDTO, | ||||
|     mapToAssignment, | ||||
|     mapToAssignmentDTO, | ||||
|     mapToAssignmentDTOId, | ||||
| } from '../interfaces/assignment.js'; | ||||
|  | @ -33,6 +35,31 @@ export async function getAllAssignments( | |||
|     return assignments.map(mapToAssignmentDTOId); | ||||
| } | ||||
| 
 | ||||
| export async function createAssignment( | ||||
|     classid: string, | ||||
|     assignmentData: AssignmentDTO, | ||||
| ): Promise<Assignment | null> { | ||||
|     const classRepository = getClassRepository(); | ||||
|     const cls = await classRepository.findById(classid); | ||||
| 
 | ||||
|     if  (!cls) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
|     const assignment = mapToAssignment(assignmentData, cls); | ||||
|     const assignmentRepository = getAssignmentRepository(); | ||||
| 
 | ||||
|     try { | ||||
|         const newAssignment = assignmentRepository.create(assignment); | ||||
|         await assignmentRepository.save(newAssignment); | ||||
| 
 | ||||
|         return newAssignment; | ||||
|     } catch(e) { | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| export async function getAssignment( | ||||
|     classid: string, | ||||
|     id: number | ||||
|  |  | |||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl