elearning/Backend/src/controllers/CategoriesController.ts
JakkrapartXD b56f604890
All checks were successful
Build and Deploy Backend / Build Backend Docker Image (push) Successful in 26s
Build and Deploy Backend / Deploy E-learning Backend to Dev Server (push) Successful in 3s
Build and Deploy Backend / Notify Deployment Status (push) Successful in 2s
feat: introduce Joi validation schemas and integrate them across various controllers for categories, lessons, courses, chapters, announcements, and admin course approvals.
2026-02-18 15:59:40 +07:00

61 lines
No EOL
2.7 KiB
TypeScript

import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Delete, Controller, Security, Request, Put, Path } from 'tsoa';
import { ValidationError } from '../middleware/errorHandler';
import { CategoryService } from '../services/categories.service';
import { createCategory, createCategoryResponse, deleteCategoryResponse, updateCategory, updateCategoryResponse, ListCategoriesResponse } from '../types/categories.type';
import { CreateCategoryValidator, UpdateCategoryValidator } from '../validators/categories.validator';
@Route('api/categories')
@Tags('Categories')
export class CategoriesController {
private categoryService = new CategoryService();
@Get()
@SuccessResponse('200', 'Categories fetched successfully')
@Response('401', 'Invalid or expired token')
public async listCategories(): Promise<ListCategoriesResponse> {
return await this.categoryService.listCategories();
}
}
@Route('api/admin/categories')
@Tags('Admin/Categories')
export class CategoriesAdminController {
private categoryService = new CategoryService();
@Post()
@Security('jwt', ['admin'])
@SuccessResponse('200', 'Category created successfully')
@Response('401', 'Invalid or expired token')
public async createCategory(@Request() request: any, @Body() body: createCategory): Promise<createCategoryResponse> {
const token = request.headers.authorization?.replace('Bearer ', '') || '';
// Validate body
const { error } = CreateCategoryValidator.validate(body);
if (error) throw new ValidationError(error.details[0].message);
return await this.categoryService.createCategory(token, body);
}
@Put('{id}')
@Security('jwt', ['admin'])
@SuccessResponse('200', 'Category updated successfully')
@Response('401', 'Invalid or expired token')
public async updateCategory(@Request() request: any, @Body() body: updateCategory): Promise<updateCategoryResponse> {
const token = request.headers.authorization?.replace('Bearer ', '') || '';
// Validate body
const { error } = UpdateCategoryValidator.validate(body);
if (error) throw new ValidationError(error.details[0].message);
return await this.categoryService.updateCategory(token, body.id, body);
}
@Delete('{id}')
@Security('jwt', ['admin'])
@SuccessResponse('200', 'Category deleted successfully')
@Response('401', 'Invalid or expired token')
public async deleteCategory(@Request() request: any, @Path() id: number): Promise<deleteCategoryResponse> {
const token = request.headers.authorization?.replace('Bearer ', '') || '';
return await this.categoryService.deleteCategory(token, id);
}
}