Skip to content

Commit

Permalink
[#56] 댓글 삭제/수정 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
rdd9223 committed Sep 28, 2023
1 parent 720935f commit 484fd8c
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
51 changes: 51 additions & 0 deletions server/src/comment/v1/comment-v1.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
Body,
Controller,
Delete,
Get,
HttpStatus,
Param,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
Expand All @@ -29,6 +31,10 @@ import { CommentV1ReportCommentResponseDto } from './dto/report-comment/comment-
import { CommentV1SwitchCommentLikeParamDto } from 'src/comment/v1/dto/switch-comment-like/comment-v1-switch-comment-like-param.dto';
import { CommentV1SwitchCommentLikeResponseDto } from './dto/switch-comment-like/comment-v1-switch-comment-like-response.dto';
import { ApiOkResponseCommon } from 'src/common/decorator/api-ok-response-common.decorator';
import { CommentV1DeleteCommentParamDto } from './dto/delete-comment/comment-v1-delete-comment-param.dto';
import { CommentV1UpdateCommentParamDto } from './dto/update-comment/comment-v1-update-comment-param.dto';
import { CommentV1UpdateCommentBodyDto } from './dto/update-comment/comment-v1-update-comment-body.dto';
import { CommentV1UpdateCommentResponseDto } from './dto/update-comment/comment-v1-update-comment-response.dto';

@ApiTags('댓글/대댓글')
@Controller('comment/v1')
Expand Down Expand Up @@ -105,4 +111,49 @@ export class CommentV1Controller {
): Promise<CommentV1CreateCommentResponseDto> {
return this.commentV1Service.createPostComment({ body, user });
}

@ApiOperation({
summary: '모임 게시글 댓글 수정',
})
@ApiOkResponseCommon(CommentV1CreateCommentResponseDto)
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: '"존재하지 않는 댓글입니다." or "권한이 없습니다."',
schema: { $ref: getSchemaPath(BaseExceptionDto) },
})
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@Put('/:commentId')
async updatePostComment(
@Param() param: CommentV1UpdateCommentParamDto,
@Body() body: CommentV1UpdateCommentBodyDto,
@GetUser() user: User,
): Promise<CommentV1UpdateCommentResponseDto> {
return this.commentV1Service.updatePostComment({
userId: user.id,
commentId: param.commentId,
body,
});
}

@ApiOperation({
summary: '모임 게시글 댓글 삭제',
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: '',
schema: { $ref: getSchemaPath(BaseExceptionDto) },
})
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@Delete('/:commentId')
async deletePostComment(
@Param() param: CommentV1DeleteCommentParamDto,
@GetUser() user: User,
): Promise<void> {
return this.commentV1Service.deletePostComment({
userId: user.id,
commentId: param.commentId,
});
}
}
59 changes: 58 additions & 1 deletion server/src/comment/v1/comment-v1.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InjectRepository } from '@nestjs/typeorm';
import { BadRequestException, Injectable, Query } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { CommentRepository } from 'src/entity/comment/comment.repository';
import dayjs from 'dayjs';
import { CommentV1CreateCommentResponseDto } from './dto/create-comment/comment-v1-create-comment-response.dto';
Expand All @@ -16,6 +16,8 @@ import { CommentV1SwitchCommentLikeResponseDto } from './dto/switch-comment-like
import { CommentV1ReportCommentParamDto } from './dto/report-comment/comment-v1-report-comment-param.dto';
import { CommentV1ReportCommentResponseDto } from './dto/report-comment/comment-v1-report-comment-response.dto';
import { ReportRepository } from 'src/entity/report/report.repository';
import { CommentV1UpdateCommentResponseDto } from './dto/update-comment/comment-v1-update-comment-response.dto';
import { CommentV1UpdateCommentBodyDto } from './dto/update-comment/comment-v1-update-comment-body.dto';

@Injectable()
export class CommentV1Service {
Expand Down Expand Up @@ -218,4 +220,59 @@ export class CommentV1Service {
commentId: comment.id,
};
}

/** 댓글 수정 */
async updatePostComment({
body,
userId,
commentId,
}: {
body: CommentV1UpdateCommentBodyDto;
userId: number;
commentId: number;
}): Promise<CommentV1UpdateCommentResponseDto> {
const comment = await this.commentRepository.findOne({
where: { id: commentId },
});

if (!comment) {
throw new BadRequestException('존재하지 않는 댓글입니다.');
}

if (comment.userId !== userId) {
throw new BadRequestException('권한이 없습니다.');
}

const nowDate = dayjs().toDate();
const input = {
contents: body.contents,
updatedDate: nowDate,
};

await this.commentRepository.update({ id: commentId }, input);

const updatedComment = await this.commentRepository.findOne({
where: { id: commentId },
});

return {
id: updatedComment.id,
contents: updatedComment.contents,
updatedDate: updatedComment.updatedDate,
};
}

/** 댓글 삭제 */
async deletePostComment({
userId,
commentId,
}: {
userId: number;
commentId: number;
}): Promise<void> {
await this.commentRepository.delete({
id: commentId,
userId,
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber } from 'class-validator';

/**
* 댓글 삭제 param Dto
* @author @rdd9223
*/
export class CommentV1DeleteCommentParamDto {
@ApiProperty({
example: 1,
description: '댓글 ID',
})
@IsNotEmpty()
@IsNumber()
commentId: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty } from 'class-validator';

/**
* 댓글 수정 body Dto
* @author @rdd9223
*/
export class CommentV1UpdateCommentBodyDto {
@ApiProperty({
example: '알고보면 쓸데있는 개발 프로세스',
description: '댓글 내용',
required: true,
})
@IsNotEmpty()
@IsString()
contents: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber } from 'class-validator';

/**
* 댓글 수정 param Dto
* @author @rdd9223
*/
export class CommentV1UpdateCommentParamDto {
@ApiProperty({
example: 1,
description: '댓글 ID',
})
@IsNotEmpty()
@IsNumber()
commentId: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PickType } from '@nestjs/swagger';
import { Comment } from 'src/entity/comment/comment.entity';

export class CommentV1UpdateCommentResponseDto extends PickType(Comment, [
'id',
'contents',
'updatedDate',
]) {}

0 comments on commit 484fd8c

Please sign in to comment.