Spaces:
Sleeping
Sleeping
| import { | |
| Controller, | |
| Put, | |
| Delete, | |
| Body, | |
| Param, | |
| Req, | |
| UseGuards, | |
| ParseIntPipe, | |
| HttpCode, | |
| HttpStatus, | |
| } from '@nestjs/common'; | |
| import { | |
| ApiTags, | |
| ApiOperation, | |
| ApiParam, | |
| ApiBody, | |
| ApiResponse, | |
| ApiBearerAuth, | |
| } from '@nestjs/swagger'; | |
| import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard'; | |
| import { RolesGuard } from '../../auth/guards/roles.guard'; | |
| import { CommunityCommentsService } from '../services/community-comments.service'; | |
| import { UpdateCommentDto } from '../dto'; | |
| ('💬 Community Comments') | |
| ('JWT-auth') | |
| ('api/community/comments') | |
| (JwtAuthGuard, RolesGuard) | |
| export class CommunityCommentsController { | |
| constructor(private readonly commentsService: CommunityCommentsService) {} | |
| (':id') | |
| ({ | |
| summary: 'Update comment', | |
| description: ` | |
| Update a community post comment. | |
| **Note**: Only the comment owner can update their comments. | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Comment ID', example: 1 }) | |
| ({ type: UpdateCommentDto }) | |
| ({ status: 200, description: 'Comment updated successfully' }) | |
| ({ status: 403, description: 'Forbidden - Not the owner' }) | |
| ({ status: 404, description: 'Comment not found' }) | |
| async update( | |
| ('id', ParseIntPipe) id: number, | |
| () dto: UpdateCommentDto, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.commentsService.update(id, dto, userId); | |
| } | |
| (':id') | |
| (HttpStatus.NO_CONTENT) | |
| ({ | |
| summary: 'Delete comment', | |
| description: ` | |
| Delete a community post comment. | |
| **Allowed**: Comment owner or Admin | |
| `, | |
| }) | |
| ({ name: 'id', description: 'Comment ID', example: 1 }) | |
| ({ status: 204, description: 'Comment deleted successfully' }) | |
| ({ status: 403, description: 'Forbidden - Not owner or admin' }) | |
| ({ status: 404, description: 'Comment not found' }) | |
| async remove(('id', ParseIntPipe) id: number, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| const roles = this.extractRoles(req.user); | |
| return this.commentsService.remove(id, userId, roles); | |
| } | |
| /** | |
| * Helper to extract role names from user object | |
| */ | |
| private extractRoles(user: any): string[] { | |
| if (!user.roles) return []; | |
| return user.roles.map((r: any) => r.roleName || r.name || r); | |
| } | |
| } | |