2026-02-11 15:04:10 +07:00
|
|
|
import { Get, Path, Put, Query, Request, Response, Route, Security, SuccessResponse, Tags } from 'tsoa';
|
2026-02-11 13:49:43 +07:00
|
|
|
import { ValidationError } from '../middleware/errorHandler';
|
|
|
|
|
import { RecommendedCoursesService } from '../services/RecommendedCourses.service';
|
|
|
|
|
import {
|
|
|
|
|
ListApprovedCoursesResponse,
|
|
|
|
|
GetCourseByIdResponse,
|
|
|
|
|
ToggleRecommendedResponse
|
|
|
|
|
} from '../types/RecommendedCourses.types';
|
|
|
|
|
|
|
|
|
|
@Route('api/admin/recommended-courses')
|
|
|
|
|
@Tags('Admin/RecommendedCourses')
|
|
|
|
|
export class RecommendedCoursesController {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ดึงรายการคอร์สที่อนุมัติแล้วทั้งหมด (สำหรับจัดการคอร์สแนะนำ)
|
|
|
|
|
* List all approved courses (for managing recommendations)
|
|
|
|
|
*/
|
|
|
|
|
@Get()
|
|
|
|
|
@Security('jwt', ['admin'])
|
|
|
|
|
@SuccessResponse('200', 'Approved courses retrieved successfully')
|
|
|
|
|
@Response('401', 'Unauthorized')
|
|
|
|
|
@Response('403', 'Forbidden - Admin only')
|
|
|
|
|
public async listApprovedCourses(@Request() request: any): Promise<ListApprovedCoursesResponse> {
|
|
|
|
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
|
|
|
|
if (!token) {
|
|
|
|
|
throw new ValidationError('No token provided');
|
|
|
|
|
}
|
|
|
|
|
return await RecommendedCoursesService.listApprovedCourses();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ดึงรายละเอียดคอร์สตาม ID
|
|
|
|
|
* Get course by ID
|
|
|
|
|
* @param courseId - รหัสคอร์ส / Course ID
|
|
|
|
|
*/
|
|
|
|
|
@Get('{courseId}')
|
|
|
|
|
@Security('jwt', ['admin'])
|
|
|
|
|
@SuccessResponse('200', 'Course retrieved successfully')
|
|
|
|
|
@Response('400', 'Course is not approved')
|
|
|
|
|
@Response('401', 'Unauthorized')
|
|
|
|
|
@Response('403', 'Forbidden - Admin only')
|
|
|
|
|
@Response('404', 'Course not found')
|
|
|
|
|
public async getCourseById(@Request() request: any, @Path() courseId: number): Promise<GetCourseByIdResponse> {
|
|
|
|
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
|
|
|
|
if (!token) {
|
|
|
|
|
throw new ValidationError('No token provided');
|
|
|
|
|
}
|
|
|
|
|
return await RecommendedCoursesService.getCourseById(courseId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* เปลี่ยนสถานะคอร์สแนะนำ
|
|
|
|
|
* Toggle course recommendation status
|
|
|
|
|
* @param courseId - รหัสคอร์ส / Course ID
|
|
|
|
|
*/
|
|
|
|
|
@Put('{courseId}/toggle')
|
|
|
|
|
@Security('jwt', ['admin'])
|
|
|
|
|
@SuccessResponse('200', 'Recommendation status updated successfully')
|
|
|
|
|
@Response('400', 'Only approved courses can be recommended')
|
|
|
|
|
@Response('401', 'Unauthorized')
|
|
|
|
|
@Response('403', 'Forbidden - Admin only')
|
|
|
|
|
@Response('404', 'Course not found')
|
|
|
|
|
public async toggleRecommended(
|
|
|
|
|
@Request() request: any,
|
2026-02-11 15:04:10 +07:00
|
|
|
@Path() courseId: number,
|
|
|
|
|
@Query() is_recommended: boolean
|
2026-02-11 13:49:43 +07:00
|
|
|
): Promise<ToggleRecommendedResponse> {
|
|
|
|
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
|
|
|
|
if (!token) {
|
|
|
|
|
throw new ValidationError('No token provided');
|
|
|
|
|
}
|
2026-02-11 15:04:10 +07:00
|
|
|
return await RecommendedCoursesService.toggleRecommended(token, courseId, is_recommended);
|
2026-02-11 13:49:43 +07:00
|
|
|
}
|
|
|
|
|
}
|