knowledge-group.controller.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. Controller,
  3. Get,
  4. Post,
  5. Put,
  6. Delete,
  7. Body,
  8. Param,
  9. UseGuards,
  10. Request,
  11. } from '@nestjs/common';
  12. import { CombinedAuthGuard } from '../auth/combined-auth.guard';
  13. import { RolesGuard } from '../auth/roles.guard';
  14. import { Roles } from '../auth/roles.decorator';
  15. import { UserRole } from '../user/user-role.enum';
  16. import { KnowledgeGroupService, CreateGroupDto, UpdateGroupDto } from './knowledge-group.service';
  17. import { I18nService } from '../i18n/i18n.service';
  18. import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse } from '@nestjs/swagger';
  19. @ApiTags('Knowledge Groups')
  20. @Controller('knowledge-groups')
  21. @UseGuards(CombinedAuthGuard, RolesGuard)
  22. export class KnowledgeGroupController {
  23. constructor(
  24. private readonly groupService: KnowledgeGroupService,
  25. private readonly i18nService: I18nService,
  26. ) { }
  27. @Get()
  28. @ApiBearerAuth('JWT')
  29. @ApiOperation({ summary: '获取知识分组列表', description: '返回当前租户的所有知识分组(树形结构)' })
  30. @ApiResponse({ status: 200, description: '返回分组列表' })
  31. async findAll(@Request() req) {
  32. // All users can see all groups for their tenant (returns tree structure)
  33. return await this.groupService.findAll(req.user.id, req.user.tenantId);
  34. }
  35. @Get(':id')
  36. @ApiBearerAuth('JWT')
  37. @ApiOperation({ summary: '获取单个知识分组' })
  38. @ApiResponse({ status: 200, description: '返回分组详情' })
  39. async findOne(@Param('id') id: string, @Request() req) {
  40. return await this.groupService.findOne(id, req.user.id, req.user.tenantId);
  41. }
  42. @Post()
  43. @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  44. @ApiBearerAuth('JWT')
  45. @ApiOperation({ summary: '创建知识分组' })
  46. @ApiResponse({ status: 201, description: '创建成功' })
  47. async create(@Body() createGroupDto: CreateGroupDto, @Request() req) {
  48. return await this.groupService.create(req.user.id, req.user.tenantId, createGroupDto);
  49. }
  50. @Put(':id')
  51. @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  52. @ApiBearerAuth('JWT')
  53. @ApiOperation({ summary: '更新知识分组' })
  54. @ApiResponse({ status: 200, description: '更新成功' })
  55. async update(
  56. @Param('id') id: string,
  57. @Body() updateGroupDto: UpdateGroupDto,
  58. @Request() req,
  59. ) {
  60. return await this.groupService.update(id, req.user.id, req.user.tenantId, updateGroupDto);
  61. }
  62. @Delete(':id')
  63. @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
  64. @ApiBearerAuth('JWT')
  65. @ApiOperation({ summary: '删除知识分组' })
  66. @ApiResponse({ status: 200, description: '删除成功' })
  67. async remove(@Param('id') id: string, @Request() req) {
  68. await this.groupService.remove(id, req.user.id, req.user.tenantId);
  69. return { message: this.i18nService.getMessage('groupDeleted') };
  70. }
  71. @Get(':id/files')
  72. @ApiBearerAuth('JWT')
  73. @ApiOperation({ summary: '获取分组内的文件' })
  74. @ApiResponse({ status: 200, description: '返回文件列表' })
  75. async getGroupFiles(@Param('id') id: string, @Request() req) {
  76. const files = await this.groupService.getGroupFiles(id, req.user.id, req.user.tenantId);
  77. return { files };
  78. }
  79. }