import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Security, Put, Path, Delete, Request } from 'tsoa'; import { ValidationError } from '../middleware/errorHandler'; import { CoursesInstructorService } from '../services/CoursesInstructor.service'; import { createCourses, createCourseResponse, GetMyCourseResponse, ListMyCourseResponse, addinstructorCourse, addinstructorCourseResponse, removeinstructorCourse, removeinstructorCourseResponse, setprimaryCourseInstructor, setprimaryCourseInstructorResponse, UpdateMyCourse, UpdateMyCourseResponse, DeleteMyCourseResponse, submitCourseResponse, listinstructorCourseResponse } from '../types/CoursesInstructor.types'; import { CreateCourseValidator } from "../validators/CoursesInstructor.validator"; import jwt from 'jsonwebtoken'; import { config } from '../config'; @Route('api/instructors/courses') @Tags('CoursesInstructor') export class CoursesInstructorController { @Get('') @Security('jwt', ['instructor']) @SuccessResponse('200', 'Courses retrieved successfully') @Response('401', 'Invalid or expired token') @Response('404', 'Courses not found') public async listMyCourses(@Request() request: any): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); if (!token) { throw new ValidationError('No token provided'); } return await CoursesInstructorService.listMyCourses(token); } @Get('{courseId}') @Security('jwt', ['instructor']) @SuccessResponse('200', 'Course retrieved successfully') @Response('401', 'Invalid or expired token') @Response('404', 'Course not found') public async getMyCourse(@Request() request: any, @Path() courseId: number): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); if (!token) { throw new ValidationError('No token provided'); } return await CoursesInstructorService.getmyCourse({ token, course_id: courseId }); } @Put('{courseId}') @Security('jwt', ['instructor']) @SuccessResponse('200', 'Course updated successfully') @Response('401', 'Invalid or expired token') @Response('404', 'Course not found') public async updateCourse(@Request() request: any, @Path() courseId: number, @Body() body: UpdateMyCourse): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); if (!token) { throw new ValidationError('No token provided'); } return await CoursesInstructorService.updateCourse(token, courseId, body.data); } @Post('') @Security('jwt', ['instructor']) @SuccessResponse('201', 'Course created successfully') @Response('400', 'Validation error') @Response('500', 'Internal server error') public async createCourse(@Body() body: createCourses, @Request() request: any): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); const decoded = jwt.verify(token, config.jwt.secret) as { id: number }; const { error, value } = CreateCourseValidator.validate(body.data); if (error) throw new ValidationError(error.details[0].message); const course = await CoursesInstructorService.createCourse(value, decoded.id); return course; } @Delete('{courseId}') @Security('jwt', ['instructor']) @SuccessResponse('200', 'Course deleted successfully') @Response('401', 'Invalid or expired token') @Response('404', 'Course not found') public async deleteCourse(@Request() request: any, @Path() courseId: number): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); if (!token) { throw new ValidationError('No token provided'); } return await CoursesInstructorService.deleteCourse(token, courseId); } @Post('send-review/{courseId}') @Security('jwt', ['instructor']) @SuccessResponse('200', 'Course submitted successfully') @Response('401', 'Invalid or expired token') @Response('404', 'Course not found') public async submitCourse(@Request() request: any, @Path() courseId: number): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); if (!token) { throw new ValidationError('No token provided'); } return await CoursesInstructorService.sendCourseForReview({ token, course_id: courseId }); } @Get('listinstructor/{courseId}') @Security('jwt', ['instructor']) @SuccessResponse('200', 'Instructors retrieved successfully') @Response('401', 'Invalid or expired token') @Response('404', 'Instructors not found') public async listInstructorCourses(@Request() request: any, @Path() courseId: number): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); if (!token) { throw new ValidationError('No token provided'); } return await CoursesInstructorService.listInstructorsOfCourse({ token, course_id: courseId }); } @Post('add-instructor/{courseId}/{userId}') @Security('jwt', ['instructor']) @SuccessResponse('200', 'Instructor added successfully') @Response('401', 'Invalid or expired token') @Response('404', 'Instructor not found') public async addInstructor(@Request() request: any, @Path() courseId: number, @Path() userId: number): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); if (!token) { throw new ValidationError('No token provided'); } return await CoursesInstructorService.addInstructorToCourse({ token, course_id: courseId, user_id: userId }); } @Post('remove-instructor/{courseId}/{userId}') @Security('jwt', ['instructor']) @SuccessResponse('200', 'Instructor removed successfully') @Response('401', 'Invalid or expired token') @Response('404', 'Instructor not found') public async removeInstructor(@Request() request: any, @Path() courseId: number, @Path() userId: number): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); if (!token) { throw new ValidationError('No token provided'); } return await CoursesInstructorService.removeInstructorFromCourse({ token, course_id: courseId, user_id: userId }); } @Post('set-primary-instructor/{courseId}/{userId}') @Security('jwt', ['instructor']) @SuccessResponse('200', 'Primary instructor set successfully') @Response('401', 'Invalid or expired token') @Response('404', 'Primary instructor not found') public async setPrimaryInstructor(@Request() request: any, @Path() courseId: number, @Path() userId: number): Promise { const token = request.headers.authorization?.replace('Bearer ', ''); if (!token) { throw new ValidationError('No token provided'); } return await CoursesInstructorService.setPrimaryInstructor({ token, course_id: courseId, user_id: userId }); } }