elearning/Backend/src/controllers/CategoriesController.ts
JakkrapartXD 522a0eec8a
Some checks failed
Build and Deploy Backend / Build Backend Docker Image (push) Successful in 48s
Build and Deploy Backend / Deploy E-learning Backend to Dev Server (push) Successful in 9s
Build and Deploy Backend / Notify Deployment Status (push) Successful in 2s
Build and Deploy Frontend Learner / Build Frontend Learner Docker Image (push) Failing after 33s
Build and Deploy Frontend Learner / Deploy E-learning Frontend Learner to Dev Server (push) Has been skipped
Build and Deploy Frontend Learner / Notify Deployment Status (push) Failing after 1s
refactor: update user identification to pass userId directly to services instead of JWT tokens.
2026-03-04 17:19:58 +07:00

56 lines
No EOL
2.5 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> {
// Validate body
const { error } = CreateCategoryValidator.validate(body);
if (error) throw new ValidationError(error.details[0].message);
return await this.categoryService.createCategory(request.user.id, 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> {
// Validate body
const { error } = UpdateCategoryValidator.validate(body);
if (error) throw new ValidationError(error.details[0].message);
return await this.categoryService.updateCategory(request.user.id, 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> {
return await this.categoryService.deleteCategory(request.user.id, id);
}
}