Skip to content

Commit 3d8e38a

Browse files
authored
Merge pull request #11 from oodd-team/OD-46
댓글 생성 기능
2 parents a8a82d9 + 9ab5968 commit 3d8e38a

10 files changed

+141
-21
lines changed

oodd.erd.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"color": ""
8787
},
8888
"meta": {
89-
"updateAt": 1728242804614,
89+
"updateAt": 1728853886556,
9090
"createAt": 1725423390543
9191
}
9292
},
@@ -573,15 +573,15 @@
573573
"U9yVRGWRzzc4ylH32boYr"
574574
],
575575
"ui": {
576-
"x": 722.5556,
576+
"x": 714.5556,
577577
"y": 1390.4444,
578578
"zIndex": 2,
579579
"widthName": 60,
580580
"widthComment": 65,
581581
"color": ""
582582
},
583583
"meta": {
584-
"updateAt": 1726549617679,
584+
"updateAt": 1728853885578,
585585
"createAt": 1725423390548
586586
}
587587
},

src/post-clothing/post-clothing.module.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { Module } from '@nestjs/common';
2-
import { PostClothingController } from './post-clothing.controller';
32
import { PostClothingService } from './post-clothing.service';
43
import { PostClothing } from 'src/common/entities/post-clothing.entity';
54
import { TypeOrmModule } from '@nestjs/typeorm';
65
import { ClothingModule } from 'src/clothing/clothing.module';
76

87
@Module({
98
imports: [TypeOrmModule.forFeature([PostClothing]), ClothingModule],
10-
controllers: [PostClothingController],
9+
controllers: [],
1110
providers: [PostClothingService],
1211
exports: [PostClothingService],
1312
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsString, MaxLength } from 'class-validator';
3+
4+
export class CreateCommentDto {
5+
@ApiProperty({
6+
example: '댓글 내용',
7+
description: '댓글 내용입니다. 최대 100자입니다.',
8+
})
9+
@IsString()
10+
@MaxLength(100)
11+
content: string;
12+
}

src/post-comment/post-comment.controller.ts

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,54 @@
1-
import { Controller, Post, Get, Patch } from '@nestjs/common';
1+
import {
2+
Controller,
3+
Post,
4+
Get,
5+
Patch,
6+
Body,
7+
Req,
8+
Query,
9+
UseGuards,
10+
} from '@nestjs/common';
211
import { PostCommentService } from './post-comment.service';
312
import {
413
CreatePostCommentSwagger,
514
DeletePostCommentSwagger,
615
GetPostCommentsSwagger,
716
} from './post-comment.swagger';
17+
import { CreateCommentDto } from './dtos/create-comment.dto';
18+
import { Request } from 'express';
19+
import { BaseResponse } from 'src/common/response/dto';
20+
import { PostService } from 'src/post/post.service';
21+
import { AuthGuard } from 'src/auth/guards/jwt.auth.guard';
822
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
923

1024
@ApiBearerAuth()
1125
@Controller('post-comment')
26+
@UseGuards(AuthGuard)
1227
@ApiTags('[서비스] 게시글 댓글')
1328
export class PostCommentController {
14-
constructor(private readonly postCommentService: PostCommentService) {}
29+
constructor(
30+
private readonly postCommentService: PostCommentService,
31+
private readonly postService: PostService,
32+
) {}
1533

1634
@Post()
1735
@CreatePostCommentSwagger('게시글 댓글 생성 API')
18-
createPostComment() {
19-
// return this.userService.getHello();
36+
async createPostComment(
37+
@Query('postId') postId: number,
38+
@Body() createCommentDto: CreateCommentDto,
39+
@Req() req: Request,
40+
): Promise<BaseResponse<any>> {
41+
const currentUserId = req.user.userId;
42+
43+
await this.postService.validatePost(postId);
44+
45+
const postComment = await this.postCommentService.createPostComment(
46+
postId,
47+
currentUserId,
48+
createCommentDto,
49+
);
50+
51+
return new BaseResponse(true, '댓글 작성 성공', postComment);
2052
}
2153

2254
@Get()

src/post-comment/post-comment.module.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Module } from '@nestjs/common';
1+
import { forwardRef, Module } from '@nestjs/common';
22
import { PostCommentController } from './post-comment.controller';
33
import { PostCommentService } from './post-comment.service';
44
import { TypeOrmModule } from '@nestjs/typeorm';
55
import { PostComment } from 'src/common/entities/post-comment.entity';
6+
import { PostModule } from 'src/post/post.module';
7+
import { UserModule } from 'src/user/user.module';
68

79
@Module({
8-
imports: [TypeOrmModule.forFeature([PostComment])],
10+
imports: [TypeOrmModule.forFeature([PostComment]), UserModule, PostModule],
911
controllers: [PostCommentController],
1012
providers: [PostCommentService],
1113
exports: [PostCommentService],

src/post-comment/post-comment.service.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { forwardRef, Inject, Injectable } from '@nestjs/common';
22
import { PostComment } from 'src/common/entities/post-comment.entity';
33
import { QueryRunner } from 'typeorm';
4+
import { InjectRepository } from '@nestjs/typeorm';
5+
import { Repository } from 'typeorm';
6+
import { CreateCommentDto } from './dtos/create-comment.dto';
7+
import { UserService } from 'src/user/user.service';
8+
import { PostService } from 'src/post/post.service';
49

510
@Injectable()
611
export class PostCommentService {
12+
constructor(
13+
@InjectRepository(PostComment)
14+
private readonly postCommentRepository: Repository<PostComment>,
15+
private readonly userService: UserService,
16+
@Inject(forwardRef(() => PostService))
17+
private readonly postService: PostService,
18+
) {}
19+
20+
// 댓글 생성
21+
async createPostComment(
22+
postId: number,
23+
currentUserId: number,
24+
createCommentDto: CreateCommentDto,
25+
): Promise<PostComment> {
26+
const post = await this.postService.findByFields({
27+
where: { id: postId, status: 'activated' },
28+
});
29+
const user = await this.userService.findByFields({
30+
where: { id: currentUserId, status: 'activated' },
31+
});
32+
const postComment = this.postCommentRepository.create({
33+
content: createCommentDto.content,
34+
post,
35+
user,
36+
});
37+
38+
return await this.postCommentRepository.save(postComment);
39+
}
40+
741
// post와 연결된 댓글 삭제
842
async deleteCommentsByPostId(
943
postId: number,

src/post-comment/post-comment.swagger.ts

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
1-
import { ApiOperation } from '@nestjs/swagger';
1+
import {
2+
ApiBadRequestResponse,
3+
ApiCreatedResponse,
4+
ApiForbiddenResponse,
5+
ApiInternalServerErrorResponse,
6+
ApiOperation,
7+
ApiUnauthorizedResponse,
8+
} from '@nestjs/swagger';
9+
import { BaseSwaggerDecorator } from 'nestjs-swagger-decorator';
10+
import { BaseResponse } from 'src/common/response/dto';
11+
import { CreateCommentDto } from './dtos/create-comment.dto';
212

313
// 게시글 댓글 생성 API Swagger
4-
export function CreatePostCommentSwagger(apiSummary: string) {
5-
return ApiOperation({ summary: apiSummary });
14+
export function CreatePostCommentSwagger(text: string) {
15+
return BaseSwaggerDecorator(
16+
{ summary: text },
17+
[],
18+
[
19+
ApiCreatedResponse({
20+
description: '댓글 작성 성공',
21+
type: CreateCommentDto,
22+
}),
23+
ApiBadRequestResponse({
24+
description: '잘못된 요청입니다.',
25+
type: BaseResponse,
26+
}),
27+
ApiUnauthorizedResponse({
28+
description: '인증되지 않은 사용자입니다.',
29+
type: BaseResponse,
30+
}),
31+
ApiForbiddenResponse({
32+
description: '권한이 없습니다.',
33+
type: BaseResponse,
34+
}),
35+
ApiInternalServerErrorResponse({
36+
description: '서버 오류입니다.',
37+
type: BaseResponse,
38+
}),
39+
],
40+
);
641
}
742

843
// 게시글 댓글 조회 API Swagger

src/post-image/post-image.module.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Module } from '@nestjs/common';
2-
import { PostImageController } from './post-image.controller';
32
import { PostImageService } from './post-image.service';
43
import { TypeOrmModule } from '@nestjs/typeorm';
54
import { PostImage } from 'src/common/entities/post-image.entity';
65

76
@Module({
87
imports: [TypeOrmModule.forFeature([PostImage])],
9-
controllers: [PostImageController],
8+
controllers: [],
109
providers: [PostImageService],
1110
exports: [PostImageService],
1211
})

src/post/post.module.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module } from '@nestjs/common';
1+
import { forwardRef, Module } from '@nestjs/common';
22
import { TypeOrmModule } from '@nestjs/typeorm';
33
import { Post } from '../common/entities/post.entity';
44
import { PostController } from './post.controller';
@@ -24,9 +24,10 @@ import { PostCommentModule } from 'src/post-comment/post-comment.module';
2424
PostClothingModule,
2525
DayjsModule,
2626
PostLikeModule,
27-
PostCommentModule,
27+
forwardRef(() => PostCommentModule),
2828
],
2929
controllers: [PostController],
3030
providers: [PostService],
31+
exports: [PostService],
3132
})
3233
export class PostModule {}

src/post/post.service.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { forwardRef, Inject, Injectable } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
3-
import { DataSource, QueryRunner, Repository } from 'typeorm';
3+
import { DataSource, FindOneOptions, QueryRunner, Repository } from 'typeorm';
44
import { Post } from '../common/entities/post.entity';
55
import { GetPostsResponse } from './dtos/total-postsResponse.dto';
66
import {
@@ -34,6 +34,7 @@ export class PostService {
3434
private readonly postStyletagService: PostStyletagService,
3535
private readonly postClothingService: PostClothingService,
3636
private readonly postLikeService: PostLikeService,
37+
@Inject(forwardRef(() => PostCommentService))
3738
private readonly postCommentService: PostCommentService,
3839
private readonly dataSource: DataSource,
3940
) {}
@@ -406,4 +407,9 @@ export class PostService {
406407
(comment) => comment.user.id === currentUserId,
407408
);
408409
}
410+
411+
// Post 조회
412+
async findByFields(fields: FindOneOptions<Post>) {
413+
return await this.postRepository.findOne(fields);
414+
}
409415
}

0 commit comments

Comments
 (0)