diff --git a/src/modules/comments/comments.controller.ts b/src/modules/comments/comments.controller.ts index a0d6f48d..bdce0397 100644 --- a/src/modules/comments/comments.controller.ts +++ b/src/modules/comments/comments.controller.ts @@ -1,8 +1,9 @@ -import { Controller, Body, Post, Request, Get, Param } from '@nestjs/common'; +import { Controller, Body, Post, Request, Get, Param, Patch } from '@nestjs/common'; import { CommentsService } from './comments.service'; import { CreateCommentDto } from './dtos/create-comment.dto'; import { CommentResponseDto } from './dtos/comment-response.dto'; import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; +import { UpdateCommentDto } from './dtos/update-comment.dto'; @ApiBearerAuth() @ApiTags('Comments') @@ -19,6 +20,19 @@ export class CommentsController { return await this.commentsService.addComment(createCommentDto, userId); } + @ApiOperation({ summary: 'Update a comment' }) + @ApiResponse({ status: 200, description: 'The comment has been successfully updated.' }) + @ApiResponse({ status: 404, description: 'User or Comment not found.' }) + @ApiResponse({ status: 500, description: 'Internal server error.' }) + @Patch('update/:id') + async updateComment( + @Param('id') id: string, + @Body() updateCommentDto: UpdateCommentDto, + @Request() req + ): Promise { + const { userId } = req.user; + return await this.commentsService.updateComment(id, userId, updateCommentDto); + @ApiOperation({ summary: 'Get a comment' }) @ApiResponse({ status: 200, description: 'The comment has been retrieved successfully.' }) @Get(':id') diff --git a/src/modules/comments/comments.service.ts b/src/modules/comments/comments.service.ts index cad87eba..cb561dc4 100644 --- a/src/modules/comments/comments.service.ts +++ b/src/modules/comments/comments.service.ts @@ -6,6 +6,7 @@ import { CreateCommentDto } from './dtos/create-comment.dto'; import { User } from '../user/entities/user.entity'; import { CommentResponseDto } from './dtos/comment-response.dto'; import { CustomHttpException } from '../../helpers/custom-http-filter'; +import { UpdateCommentDto } from './dtos/update-comment.dto'; @Injectable() export class CommentsService { constructor( @@ -43,6 +44,30 @@ export class CommentsService { }; } + async updateComment( + commentId: string, + userId: string, + updateCommentDto: UpdateCommentDto + ): Promise { + const comment = await this.commentRepository.findOneBy({ id: commentId }); + if (!comment) { + throw new CustomHttpException('Comment not found', 404); + } + + const user = await this.userRepository.findOne({ where: { id: userId } }); + if (!user) { + throw new CustomHttpException('User not found', 404); + } + + await this.commentRepository.update(commentId, updateCommentDto); + const updatedComment = await this.commentRepository.findOneBy({ id: commentId }); + + return { + message: 'Comment updated successfully!', + savedComment: updatedComment, + commentedBy: user.first_name + ' ' + user.last_name, + } + async getAComment(commentId: string) { const comment = await this.commentRepository.findOneBy({ id: commentId }); if (!comment) { diff --git a/src/modules/comments/dtos/update-comment.dto.ts b/src/modules/comments/dtos/update-comment.dto.ts new file mode 100644 index 00000000..ca0e509b --- /dev/null +++ b/src/modules/comments/dtos/update-comment.dto.ts @@ -0,0 +1,9 @@ +import { IsString, IsNotEmpty } from 'class-validator'; +import { ApiProperty } from '@nestjs/swagger'; + +export class UpdateCommentDto { + @ApiProperty({ description: 'Comment content' }) + @IsString() + @IsNotEmpty() + comment: string; +}