add filter to course
This commit is contained in:
parent
1cbaed72cb
commit
5e7d265ffb
2 changed files with 16 additions and 5 deletions
|
|
@ -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}')
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue