From a5cfe531891d091360dd9ab952d8490d4ff32434 Mon Sep 17 00:00:00 2001 From: Oluwaseyi Idowu Date: Sat, 24 Aug 2024 04:46:03 +0100 Subject: [PATCH 1/2] feat: update a comment --- src/modules/comments/comments.controller.ts | 17 +++++++++++- src/modules/comments/comments.service.ts | 26 +++++++++++++++++++ .../comments/dtos/update-comment.dto.ts | 9 +++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/modules/comments/dtos/update-comment.dto.ts diff --git a/src/modules/comments/comments.controller.ts b/src/modules/comments/comments.controller.ts index 24adfd0f..f8b58555 100644 --- a/src/modules/comments/comments.controller.ts +++ b/src/modules/comments/comments.controller.ts @@ -1,8 +1,9 @@ -import { Controller, Body, Post, Request } from '@nestjs/common'; +import { Controller, Body, Post, Request, 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') @@ -18,4 +19,18 @@ export class CommentsController { const { userId } = req.user; 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); + } } diff --git a/src/modules/comments/comments.service.ts b/src/modules/comments/comments.service.ts index f2f2e763..db97aab2 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( @@ -42,4 +43,29 @@ export class CommentsService { commentedBy, }; } + + 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, + }; + } } 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; +} From c04f46a4d4daea05dec7bcf355f76b1e47ffd2f5 Mon Sep 17 00:00:00 2001 From: Oluwaseyi Idowu <106804924+idowuseyi@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:07:52 +0100 Subject: [PATCH 2/2] Update comments.service.ts --- src/modules/comments/comments.service.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/comments/comments.service.ts b/src/modules/comments/comments.service.ts index f12f8882..cb561dc4 100644 --- a/src/modules/comments/comments.service.ts +++ b/src/modules/comments/comments.service.ts @@ -66,7 +66,8 @@ export class CommentsService { 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) {