elearning/Backend/src/controllers/CoursesInstructorController.ts

229 lines
11 KiB
TypeScript
Raw Normal View History

import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Security, Put, Path, Delete, Request, Example, FormField, UploadedFile } 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,
2026-01-23 13:16:41 +07:00
listinstructorCourseResponse,
GetCourseApprovalsResponse
} 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 all courses where the authenticated user is an instructor
*/
@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<ListMyCourseResponse> {
const token = request.headers.authorization?.replace('Bearer ', '');
if (!token) {
throw new ValidationError('No token provided');
}
return await CoursesInstructorService.listMyCourses(token);
}
/**
* ()
* Get detailed course information including chapters, lessons, attachments, and quizzes
* @param courseId - / Course ID
*/
@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<GetMyCourseResponse> {
const token = request.headers.authorization?.replace('Bearer ', '');
if (!token) {
throw new ValidationError('No token provided');
}
return await CoursesInstructorService.getmyCourse({ token, course_id: courseId });
}
/**
*
* Update course information (only for course instructors)
* @param courseId - / Course ID
*/
@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<UpdateMyCourseResponse> {
const token = request.headers.authorization?.replace('Bearer ', '');
if (!token) {
throw new ValidationError('No token provided');
}
return await CoursesInstructorService.updateCourse(token, courseId, body.data);
}
/**
* ( thumbnail)
* Create a new course with optional thumbnail upload (status will be DRAFT by default)
* @param data - JSON string containing course data
* @param thumbnail - Optional thumbnail image file
*/
@Post('')
@Security('jwt', ['instructor'])
@SuccessResponse('201', 'Course created successfully')
@Response('400', 'Validation error')
@Response('500', 'Internal server error')
public async createCourse(
@Request() request: any,
@FormField() data: string,
@UploadedFile() thumbnail?: Express.Multer.File
): Promise<createCourseResponse> {
const token = request.headers.authorization?.replace('Bearer ', '');
if (!token) throw new ValidationError('No token provided');
const decoded = jwt.verify(token, config.jwt.secret) as { id: number };
const parsed = JSON.parse(data);
const { error, value } = CreateCourseValidator.validate(parsed);
if (error) throw new ValidationError(error.details[0].message);
// Validate thumbnail file type if provided
if (thumbnail && !thumbnail.mimetype?.startsWith('image/')) throw new ValidationError('Only image files are allowed for thumbnail');
return await CoursesInstructorService.createCourse(value, decoded.id, thumbnail);
}
/**
* ()
* Delete a course (only primary instructor can delete)
* @param courseId - / Course ID
*/
@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<DeleteMyCourseResponse> {
const token = request.headers.authorization?.replace('Bearer ', '');
if (!token) throw new ValidationError('No token provided')
return await CoursesInstructorService.deleteCourse(token, courseId);
}
/**
*
* Submit course for admin review and approval
* @param courseId - / Course ID
*/
@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<submitCourseResponse> {
const token = request.headers.authorization?.replace('Bearer ', '');
if (!token) throw new ValidationError('No token provided')
return await CoursesInstructorService.sendCourseForReview({ token, course_id: courseId });
}
2026-01-23 13:16:41 +07:00
/**
*
* Get all course approval history
* @param courseId - / Course ID
*/
@Get('{courseId}/approvals')
@Security('jwt', ['instructor'])
@SuccessResponse('200', 'Course approvals retrieved successfully')
@Response('401', 'Invalid or expired token')
@Response('403', 'You are not an instructor of this course')
@Response('404', 'Course not found')
public async getCourseApprovals(@Request() request: any, @Path() courseId: number): Promise<GetCourseApprovalsResponse> {
const token = request.headers.authorization?.replace('Bearer ', '');
if (!token) throw new ValidationError('No token provided')
2026-01-23 13:16:41 +07:00
return await CoursesInstructorService.getCourseApprovals(token, courseId);
}
/**
*
* Get list of all instructors in a specific course
* @param courseId - / Course ID
*/
@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<listinstructorCourseResponse> {
const token = request.headers.authorization?.replace('Bearer ', '');
if (!token) throw new ValidationError('No token provided')
return await CoursesInstructorService.listInstructorsOfCourse({ token, course_id: courseId });
}
/**
*
* Add a new instructor to the course
* @param courseId - / Course ID
* @param userId - / User ID to add as instructor
*/
@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<addinstructorCourseResponse> {
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 });
}
/**
*
* Remove an instructor from the course
* @param courseId - / Course ID
* @param userId - / User ID to remove from instructors
*/
@Delete('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<removeinstructorCourseResponse> {
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 });
}
/**
*
* Set a user as the primary instructor of the course
* @param courseId - / Course ID
* @param userId - / User ID to set as primary instructor
*/
@Put('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<setprimaryCourseInstructorResponse> {
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 });
}
}