|
| 1 | +import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common'; |
| 2 | +import { ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; |
| 3 | +import { ICQRSHandler } from '@common/cqrs'; |
| 4 | +import { Forum } from '@domain/forum'; |
| 5 | +import { CreateForumDto, ForumDto, ForumPermissionDto, UpdateForumPermissionDto } from '@infrastructure/forum'; |
| 6 | +import { CreateForumCommand, FetchAllForums, GetForumHierarchy, UpdateForumPermissionsCommand } from '@modules/forum'; |
| 7 | +import { CurrentUser, FirebaseGuard } from '@modules/auth/decorators'; |
| 8 | +import { Identifiable } from '@domain/core'; |
| 9 | +import { AuthUser } from '@domain/auth/entity'; |
| 10 | +import { IdentifiableResponse } from '@api/rest/core'; |
| 11 | +import { UserRole } from '@domain/auth/enum'; |
| 12 | + |
| 13 | +@Controller('v1/forum') |
| 14 | +@ApiTags('Forum') |
| 15 | +export class ForumController { |
| 16 | + constructor(private readonly cqrsHandler: ICQRSHandler) {} |
| 17 | + |
| 18 | + @Post() |
| 19 | + @FirebaseGuard(UserRole.ADMIN) |
| 20 | + @ApiOperation({ summary: 'Create a forum' }) |
| 21 | + @ApiResponse({ |
| 22 | + status: 201, |
| 23 | + description: '', |
| 24 | + type: IdentifiableResponse, |
| 25 | + }) |
| 26 | + @ApiBody({ type: CreateForumDto, description: "Creates a new forum and it's permissions" }) |
| 27 | + async createForum(@CurrentUser() authUser: AuthUser, @Body() forumDto: CreateForumDto): Promise<Identifiable> { |
| 28 | + return await this.cqrsHandler.execute(CreateForumCommand, { |
| 29 | + data: forumDto, |
| 30 | + userRole: authUser.customClaims.role, |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + @Get() |
| 35 | + @ApiOperation({ summary: 'Fetch all forums' }) |
| 36 | + @ApiResponse({ |
| 37 | + status: 200, |
| 38 | + description: 'Returns all forums', |
| 39 | + type: [ForumDto], |
| 40 | + }) |
| 41 | + async getAllForums(): Promise<Forum[]> { |
| 42 | + return await this.cqrsHandler.fetch(FetchAllForums, {}); |
| 43 | + } |
| 44 | + |
| 45 | + @Get(':forumId') |
| 46 | + @ApiOperation({ summary: 'Fetch forum and all its children' }) |
| 47 | + @ApiResponse({ |
| 48 | + status: 200, |
| 49 | + description: 'Fetch forum and all its children', |
| 50 | + type: [ForumDto], |
| 51 | + }) |
| 52 | + async getForumHierarchy(@Param('forumId') id: string): Promise<Forum[]> { |
| 53 | + return await this.cqrsHandler.fetch(GetForumHierarchy, id); |
| 54 | + } |
| 55 | + |
| 56 | + @Put(':forumId') |
| 57 | + @ApiOperation({ summary: 'Update a forums permissions' }) |
| 58 | + @ApiResponse({ |
| 59 | + status: 201, |
| 60 | + description: '', |
| 61 | + type: ForumPermissionDto, |
| 62 | + }) |
| 63 | + async updateForumPermissions( |
| 64 | + @Param('forumId') id: string, |
| 65 | + @Body() updateForumPermissionBody: UpdateForumPermissionDto, |
| 66 | + ): Promise<Identifiable> { |
| 67 | + return await this.cqrsHandler.execute(UpdateForumPermissionsCommand, { forumId: id, data: updateForumPermissionBody }); |
| 68 | + } |
| 69 | +} |
0 commit comments