fix: student join req by class route + teacher return post put delete + status
This commit is contained in:
		
							parent
							
								
									c0995d3933
								
							
						
					
					
						commit
						912369f87e
					
				
					 9 changed files with 106 additions and 47 deletions
				
			
		|  | @ -4,7 +4,7 @@ import { | |||
|     createStudent, | ||||
|     deleteClassJoinRequest, | ||||
|     deleteStudent, | ||||
|     getAllStudents, | ||||
|     getAllStudents, getJoinRequestByStudentClass, | ||||
|     getJoinRequestsByStudent, | ||||
|     getStudent, | ||||
|     getStudentAssignments, | ||||
|  | @ -116,7 +116,7 @@ export async function createStudentRequestHandler(req: Request, res: Response): | |||
|     res.json({ request }); | ||||
| } | ||||
| 
 | ||||
| export async function getStudentRequestHandler(req: Request, res: Response): Promise<void> { | ||||
| export async function getStudentRequestsHandler(req: Request, res: Response): Promise<void> { | ||||
|     const username = req.params.username; | ||||
|     requireFields({ username }); | ||||
| 
 | ||||
|  | @ -124,6 +124,15 @@ export async function getStudentRequestHandler(req: Request, res: Response): Pro | |||
|     res.json({ requests }); | ||||
| } | ||||
| 
 | ||||
| export async function getStudentRequestHandler(req: Request, res: Response): Promise<void> { | ||||
|     const username = req.params.username as string; | ||||
|     const classId = req.params.classId; | ||||
|     requireFields({ username, classId }); | ||||
| 
 | ||||
|     const request = await getJoinRequestByStudentClass(username, classId); | ||||
|     res.json({ request }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteClassJoinRequestHandler(req: Request, res: Response) { | ||||
|     const username = req.params.username as string; | ||||
|     const classId = req.params.classId; | ||||
|  |  | |||
|  | @ -41,16 +41,16 @@ export async function createTeacherHandler(req: Request, res: Response) { | |||
| 
 | ||||
|     const userData = req.body as TeacherDTO; | ||||
| 
 | ||||
|     await createTeacher(userData); | ||||
|     res.sendStatus(201); | ||||
|     const teacher = await createTeacher(userData); | ||||
|     res.json({ teacher }); | ||||
| } | ||||
| 
 | ||||
| export async function deleteTeacherHandler(req: Request, res: Response) { | ||||
|     const username = req.params.username; | ||||
|     requireFields({ username }); | ||||
| 
 | ||||
|     await deleteTeacher(username); | ||||
|     res.sendStatus(200); | ||||
|     const teacher = await deleteTeacher(username); | ||||
|     res.json({ teacher }); | ||||
| } | ||||
| 
 | ||||
| export async function getTeacherClassHandler(req: Request, res: Response): Promise<void> { | ||||
|  | @ -98,6 +98,6 @@ export async function updateStudentJoinRequestHandler(req: Request, res: Respons | |||
|     const accepted = req.body.accepted !== 'false'; // Default = true
 | ||||
|     requireFields({ studentUsername, classId }); | ||||
| 
 | ||||
|     await updateClassJoinRequestStatus(studentUsername, classId, accepted); | ||||
|     res.sendStatus(200); | ||||
|     const request = await updateClassJoinRequestStatus(studentUsername, classId, accepted); | ||||
|     res.json({ request }); | ||||
| } | ||||
|  |  | |||
|  | @ -1,12 +1,19 @@ | |||
| import express from 'express'; | ||||
| import { createStudentRequestHandler, deleteClassJoinRequestHandler, getStudentRequestHandler } from '../controllers/students.js'; | ||||
| import { | ||||
|     createStudentRequestHandler, | ||||
|     deleteClassJoinRequestHandler, | ||||
|     getStudentRequestHandler, | ||||
|     getStudentRequestsHandler | ||||
| } from '../controllers/students.js'; | ||||
| 
 | ||||
| const router = express.Router({ mergeParams: true }); | ||||
| 
 | ||||
| router.get('/', getStudentRequestHandler); | ||||
| router.get('/', getStudentRequestsHandler); | ||||
| 
 | ||||
| router.post('/', createStudentRequestHandler); | ||||
| 
 | ||||
| router.get('/:classId', getStudentRequestHandler); | ||||
| 
 | ||||
| router.delete('/:classId', deleteClassJoinRequestHandler); | ||||
| 
 | ||||
| export default router; | ||||
|  |  | |||
|  | @ -125,10 +125,10 @@ export async function getStudentQuestions(username: string, full: boolean): Prom | |||
|     return questionsDTO.map(mapToQuestionId); | ||||
| } | ||||
| 
 | ||||
| export async function createClassJoinRequest(studentUsername: string, classId: string) { | ||||
| export async function createClassJoinRequest(username: string, classId: string) { | ||||
|     const requestRepo = getClassJoinRequestRepository(); | ||||
| 
 | ||||
|     const student = await fetchStudent(studentUsername); // Throws error if student not found
 | ||||
|     const student = await fetchStudent(username); // Throws error if student not found
 | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     const request = mapToStudentRequest(student, cls); | ||||
|  | @ -136,19 +136,33 @@ export async function createClassJoinRequest(studentUsername: string, classId: s | |||
|     return mapToStudentRequestDTO(request); | ||||
| } | ||||
| 
 | ||||
| export async function getJoinRequestsByStudent(studentUsername: string) { | ||||
| export async function getJoinRequestsByStudent(username: string) { | ||||
|     const requestRepo = getClassJoinRequestRepository(); | ||||
| 
 | ||||
|     const student = await fetchStudent(studentUsername); | ||||
|     const student = await fetchStudent(username); | ||||
| 
 | ||||
|     const requests = await requestRepo.findAllRequestsBy(student); | ||||
|     return requests.map(mapToStudentRequestDTO); | ||||
| } | ||||
| 
 | ||||
| export async function deleteClassJoinRequest(studentUsername: string, classId: string) { | ||||
| export async function getJoinRequestByStudentClass(username: string, classId: string){ | ||||
|     const requestRepo = getClassJoinRequestRepository(); | ||||
| 
 | ||||
|     const student = await fetchStudent(studentUsername); | ||||
|     const student = await fetchStudent(username); | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     const request = await requestRepo.findByStudentAndClass(student, cls); | ||||
|     if (!request){ | ||||
|         throw new NotFoundException('Join request not found'); | ||||
|     } | ||||
| 
 | ||||
|     return mapToStudentRequestDTO(request); | ||||
| } | ||||
| 
 | ||||
| export async function deleteClassJoinRequest(username: string, classId: string) { | ||||
|     const requestRepo = getClassJoinRequestRepository(); | ||||
| 
 | ||||
|     const student = await fetchStudent(username); | ||||
|     const cls = await fetchClass(classId); | ||||
| 
 | ||||
|     const request = await requestRepo.findByStudentAndClass(student, cls); | ||||
|  |  | |||
|  | @ -51,19 +51,21 @@ export async function getTeacher(username: string): Promise<TeacherDTO> { | |||
|     return mapToTeacherDTO(user); | ||||
| } | ||||
| 
 | ||||
| export async function createTeacher(userData: TeacherDTO): Promise<void> { | ||||
| export async function createTeacher(userData: TeacherDTO): Promise<TeacherDTO> { | ||||
|     const teacherRepository: TeacherRepository = getTeacherRepository(); | ||||
| 
 | ||||
|     const newTeacher = mapToTeacher(userData); | ||||
|     await teacherRepository.save(newTeacher, { preventOverwrite: true }); | ||||
|     return mapToTeacherDTO(newTeacher); | ||||
| } | ||||
| 
 | ||||
| export async function deleteTeacher(username: string): Promise<void> { | ||||
| export async function deleteTeacher(username: string): Promise<TeacherDTO> { | ||||
|     const teacherRepository: TeacherRepository = getTeacherRepository(); | ||||
| 
 | ||||
|     await fetchTeacher(username); // Throws error if it does not exist
 | ||||
|     const teacher = await fetchTeacher(username); // Throws error if it does not exist
 | ||||
| 
 | ||||
|     await teacherRepository.deleteByUsername(username); | ||||
|     return mapToTeacherDTO(teacher); | ||||
| } | ||||
| 
 | ||||
| async function fetchClassesByTeacher(username: string): Promise<ClassDTO[]> { | ||||
|  | @ -106,9 +108,6 @@ export async function getTeacherQuestions(username: string, full: boolean): Prom | |||
|     const learningObjectRepository: LearningObjectRepository = getLearningObjectRepository(); | ||||
|     const learningObjects: LearningObject[] = await learningObjectRepository.findAllByTeacher(teacher); | ||||
| 
 | ||||
|     // Console.log(learningObjects)
 | ||||
|     // TODO returns empty
 | ||||
| 
 | ||||
|     if (!learningObjects || learningObjects.length === 0) { | ||||
|         return []; | ||||
|     } | ||||
|  | @ -138,7 +137,7 @@ export async function getJoinRequestsByClass(classId: string): Promise<StudentRe | |||
|     return requests.map(mapToStudentRequestDTO); | ||||
| } | ||||
| 
 | ||||
| export async function updateClassJoinRequestStatus(studentUsername: string, classId: string, accepted: boolean = true): Promise<void> { | ||||
| export async function updateClassJoinRequestStatus(studentUsername: string, classId: string, accepted: boolean = true): Promise<StudentRequestDTO> { | ||||
|     const requestRepo: ClassJoinRequestRepository = getClassJoinRequestRepository(); | ||||
|     const classRepo: ClassRepository = getClassRepository(); | ||||
| 
 | ||||
|  | @ -158,4 +157,5 @@ export async function updateClassJoinRequestStatus(studentUsername: string, clas | |||
|     request.status = accepted ? ClassJoinRequestStatus.Accepted : ClassJoinRequestStatus.Declined; | ||||
| 
 | ||||
|     await requestRepo.save(request); | ||||
|     return mapToStudentRequestDTO(request); | ||||
| } | ||||
|  |  | |||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl