feat: Implement instructor-specific course management with dedicated controller, service, types, and validation.
This commit is contained in:
parent
bca2cc944e
commit
8a2ca592bc
6 changed files with 229 additions and 12 deletions
|
|
@ -2,11 +2,10 @@ import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Delete, Contro
|
|||
import { ValidationError } from '../middleware/errorHandler';
|
||||
import { listCourseResponse } from '../types/courses.types';
|
||||
import { CoursesService } from '../services/courses.service';
|
||||
import { get } from 'http';
|
||||
|
||||
@Route('api/courses')
|
||||
@Tags('Courses')
|
||||
export class CoursesController extends Controller {
|
||||
export class CoursesController {
|
||||
private coursesService = new CoursesService();
|
||||
|
||||
@Get()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
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
|
||||
} 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<ListMyCourseResponse> {
|
||||
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<GetMyCourseResponse> {
|
||||
const token = request.headers.authorization?.replace('Bearer ', '');
|
||||
if (!token) {
|
||||
throw new ValidationError('No token provided');
|
||||
}
|
||||
return await CoursesInstructorService.getmyCourse(token, courseId);
|
||||
}
|
||||
|
||||
@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<createCourseResponse> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue