add filter APPROVED to couse to see olny APPROVED course
This commit is contained in:
parent
cb1e804490
commit
6acb536aef
3 changed files with 33 additions and 18 deletions
|
|
@ -2,7 +2,6 @@ import { Body, Delete, Get, Path, Post, Put, Request, Response, Route, Security,
|
||||||
import { ValidationError } from '../middleware/errorHandler';
|
import { ValidationError } from '../middleware/errorHandler';
|
||||||
import { ChaptersLessonService } from '../services/ChaptersLesson.service';
|
import { ChaptersLessonService } from '../services/ChaptersLesson.service';
|
||||||
import {
|
import {
|
||||||
ListChaptersResponse,
|
|
||||||
CreateChapterResponse,
|
CreateChapterResponse,
|
||||||
UpdateChapterResponse,
|
UpdateChapterResponse,
|
||||||
DeleteChapterResponse,
|
DeleteChapterResponse,
|
||||||
|
|
@ -33,22 +32,9 @@ export class ChaptersLessonInstructorController {
|
||||||
|
|
||||||
// ============================================
|
// ============================================
|
||||||
// Chapter Endpoints
|
// Chapter Endpoints
|
||||||
|
// Note: Use CoursesInstructorController.getMyCourse to get chapters with full details
|
||||||
// ============================================
|
// ============================================
|
||||||
|
|
||||||
/**
|
|
||||||
* ดึง chapters ทั้งหมดของ course (พร้อม lessons)
|
|
||||||
* Get all chapters of a course with lessons
|
|
||||||
*/
|
|
||||||
@Get('chapters')
|
|
||||||
@Security('jwt', ['instructor'])
|
|
||||||
@SuccessResponse('200', 'Chapters retrieved successfully')
|
|
||||||
@Response('401', 'Unauthorized')
|
|
||||||
public async listChapters(@Request() request: any, @Path() courseId: number): Promise<ListChaptersResponse> {
|
|
||||||
const token = request.headers.authorization?.replace('Bearer ', '');
|
|
||||||
if (!token) throw new ValidationError('No token provided');
|
|
||||||
return await chaptersLessonService.listChapters({ token, course_id: courseId });
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* สร้าง chapter ใหม่
|
* สร้าง chapter ใหม่
|
||||||
* Create a new chapter
|
* Create a new chapter
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,28 @@ export class CoursesStudentService {
|
||||||
try {
|
try {
|
||||||
const { course_id } = input;
|
const { course_id } = input;
|
||||||
const decoded = jwt.verify(input.token, config.jwt.secret) as { id: number; type: string };
|
const decoded = jwt.verify(input.token, config.jwt.secret) as { id: number; type: string };
|
||||||
|
|
||||||
|
const course = await prisma.course.findUnique({
|
||||||
|
where: { id: course_id },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!course) throw new NotFoundError('Course not found');
|
||||||
|
|
||||||
|
if (course.status !== 'APPROVED') throw new ForbiddenError('Cannot enroll in this course. Course is not available.');
|
||||||
|
|
||||||
|
const existingEnrollment = await prisma.enrollment.findUnique({
|
||||||
|
where: {
|
||||||
|
unique_enrollment: {
|
||||||
|
user_id: decoded.id,
|
||||||
|
course_id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingEnrollment) {
|
||||||
|
throw new ValidationError('Already enrolled in this course');
|
||||||
|
}
|
||||||
|
|
||||||
const enrollment = await prisma.enrollment.create({
|
const enrollment = await prisma.enrollment.create({
|
||||||
data: {
|
data: {
|
||||||
course_id,
|
course_id,
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,9 @@ import { UnauthorizedError, ValidationError, ForbiddenError } from '../middlewar
|
||||||
export class CoursesService {
|
export class CoursesService {
|
||||||
async ListCourses(category_id?: number): Promise<listCourseResponse> {
|
async ListCourses(category_id?: number): Promise<listCourseResponse> {
|
||||||
try {
|
try {
|
||||||
const where: Prisma.CourseWhereInput = {};
|
const where: Prisma.CourseWhereInput = {
|
||||||
|
status: 'APPROVED',
|
||||||
|
};
|
||||||
|
|
||||||
if (category_id) {
|
if (category_id) {
|
||||||
where.category_id = category_id;
|
where.category_id = category_id;
|
||||||
|
|
@ -30,7 +32,12 @@ export class CoursesService {
|
||||||
|
|
||||||
async GetCourseById(id: number): Promise<getCourseResponse> {
|
async GetCourseById(id: number): Promise<getCourseResponse> {
|
||||||
try {
|
try {
|
||||||
const course = await prisma.course.findUnique({ where: { id } });
|
const course = await prisma.course.findFirst({
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
status: 'APPROVED' // Only show approved courses to students
|
||||||
|
}
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
code: 200,
|
code: 200,
|
||||||
message: 'Course fetched successfully',
|
message: 'Course fetched successfully',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue