elearning/Backend/src/services/courses.service.ts

52 lines
No EOL
1.6 KiB
TypeScript

import { prisma } from '../config/database';
import { Prisma } from '@prisma/client';
import { config } from '../config';
import { logger } from '../config/logger';
import { listCourseResponse, getCourseResponse } from '../types/courses.types';
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
export class CoursesService {
async ListCourses(category_id?: number): Promise<listCourseResponse> {
try {
const where: Prisma.CourseWhereInput = {
status: 'APPROVED',
};
if (category_id) {
where.category_id = category_id;
}
const courses = await prisma.course.findMany({ where });
return {
code: 200,
message: 'Courses fetched successfully',
total: courses.length,
data: courses,
};
} catch (error) {
logger.error('Failed to fetch courses', { error });
throw error;
}
}
async GetCourseById(id: number): Promise<getCourseResponse> {
try {
const course = await prisma.course.findFirst({
where: {
id,
status: 'APPROVED' // Only show approved courses to students
}
});
return {
code: 200,
message: 'Course fetched successfully',
data: course,
};
} catch (error) {
logger.error('Failed to fetch course', { error });
throw error;
}
}
}