Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update a comment #964

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/modules/comments/comments.controller.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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<CommentResponseDto> {
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')
Expand Down
25 changes: 25 additions & 0 deletions src/modules/comments/comments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -43,6 +44,30 @@ export class CommentsService {
};
}

async updateComment(
commentId: string,
userId: string,
updateCommentDto: UpdateCommentDto
): Promise<CommentResponseDto> {
const comment = await this.commentRepository.findOneBy({ id: commentId });
idowuseyi marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down
9 changes: 9 additions & 0 deletions src/modules/comments/dtos/update-comment.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading