add filter to course

This commit is contained in:
JakkrapartXD 2026-01-23 10:37:19 +07:00
parent 1cbaed72cb
commit 5e7d265ffb
2 changed files with 16 additions and 5 deletions

View file

@ -1,4 +1,4 @@
import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Delete, Controller, Security, Request, Put, Path } from 'tsoa';
import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Delete, Controller, Security, Request, Put, Path, Query } from 'tsoa';
import { ValidationError } from '../middleware/errorHandler';
import { listCourseResponse } from '../types/courses.types';
import { CoursesService } from '../services/courses.service';
@ -8,11 +8,16 @@ import { CoursesService } from '../services/courses.service';
export class CoursesController {
private coursesService = new CoursesService();
/**
* ( filter category_id )
* Get all courses (can filter by category_id)
* @param category_id - / Category ID (optional)
*/
@Get()
@SuccessResponse('200', 'Courses fetched successfully')
@Response('401', 'Invalid or expired token')
public async listCourses(): Promise<listCourseResponse> {
return await this.coursesService.ListCourses();
public async listCourses(@Query() category_id?: number): Promise<listCourseResponse> {
return await this.coursesService.ListCourses(category_id);
}
@Get('{id}')

View file

@ -6,9 +6,15 @@ import { listCourseResponse, getCourseResponse } from '../types/courses.types';
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
export class CoursesService {
async ListCourses(): Promise<listCourseResponse> {
async ListCourses(category_id?: number): Promise<listCourseResponse> {
try {
const courses = await prisma.course.findMany();
const where: Prisma.CourseWhereInput = {};
if (category_id) {
where.category_id = category_id;
}
const courses = await prisma.course.findMany({ where });
return {
code: 200,
message: 'Courses fetched successfully',