Skip to Content
BackendNestJs37-Follower Comment Count

1. Follower Count 증가 & 감소 - 1

user 엔티티에 팔로우, 팔로워 수 컬럼을 추가해준다.

users.entity.ts
1
@Column({ default: 0 })
2
followerCount: number
3
4
@Column({ default: 0 })
5
followeeCount: number

users 컨트롤러

users/users.controller.ts
1
import { QueryRunner as QR } from 'typeorm'
2
// 나머지 import 생략
3
4
// **** 팔로우 요청 승인
5
@Patch('follow/:id/confirm')
6
@UseInterceptors(TransactionInterceptor)
7
async patchFollowConfirm(
8
@User() user: UsersModel, //
9
@Param('id', ParseIntPipe) followerId: number,
10
@QueryRunner() qr: QR,
11
) {
12
await this.usersService.confirmFollow(followerId, user.id, qr)
13
await this.usersService.incrementFollowerCount(user.id, qr)
14
return true
15
}
16
17
// **** 팔로우 요청 삭제
18
@Delete('follow/:id')
19
async deleteFollow(
20
@User() user: UsersModel, //
21
@Param('id', ParseIntPipe) followeeId: number,
22
@QueryRunner() qr: QR,
23
) {
24
await this.usersService.deleteFollow(user.id, followeeId, qr)
25
await this.usersService.decrementFollowerCount(followeeId, qr)
26
return true
27
}

users 서비스

users/users.service.ts
1
@Injectable()
2
export class UsersService {
3
// 생략
4
5
getUsersRepository(qr?: QueryRunner) {
6
return qr //
7
? qr.manager.getRepository<UsersModel>(UsersModel)
8
: this.usersRepository
9
}
10
11
getUserFollowRepository(qr?: QueryRunner) {
12
return qr //
13
? qr.manager.getRepository<UserFollowersModel>(UserFollowersModel)
14
: this.userFollowersRepository
15
}
16
17
// 생략
18
19
// **** 4) 팔로우 요청
20
async followUser(followerId: number, followeeId: number, qr?: QueryRunner) {
21
const userFollowersRepository = this.getUserFollowRepository(qr)
22
23
await userFollowersRepository.save({
24
follower: { id: followerId },
25
followee: { id: followeeId },
26
})
27
28
return true
29
}
30
31
// **** 5) 팔로워들 조회
32
// 생략
33
34
// **** 팔로우 요청 승인
35
async confirmFollow(followerId: number, followeeId: number, qr?: QueryRunner) {
36
const userFollowersRepository = this.getUserFollowRepository(qr)
37
38
const existing = await userFollowersRepository.findOne({
39
where: {
40
follower: { id: followerId },
41
followee: { id: followeeId },
42
},
43
relations: {
44
follower: true,
45
followee: true,
46
},
47
})
48
49
if (!existing) {
50
throw new BadRequestException('존재하지 않는 팔로우 요청입니다!')
51
}
52
53
await userFollowersRepository.save({
54
...existing,
55
isConfirmed: true,
56
})
57
58
return true
59
}
60
61
// **** 팔로우 요청 삭제
62
async deleteFollow(followerId: number, followeeId: number, qr?: QueryRunner) {
63
const userFollowersRepository = this.getUserFollowRepository(qr)
64
65
await userFollowersRepository.delete({
66
follower: {
67
id: followerId,
68
},
69
followee: {
70
id: followeeId,
71
},
72
})
73
74
return true
75
}
76
77
async incrementFollowerCount(userId: number, qr?: QueryRunner) {
78
const userRepository = this.getUsersRepository(qr)
79
80
await userRepository.increment(
81
{ id: userId }, //
82
'followerCount',
83
1,
84
)
85
}
86
87
async decrementFollowerCount(userId: number, qr?: QueryRunner) {
88
const userRepository = this.getUsersRepository(qr)
89
90
await userRepository.decrement(
91
{ id: userId }, //
92
'followerCount',
93
1,
94
)
95
}
96
}

2. Follower Count 증가 & 감소 - 2

posts 서비스에 댓글 수를 표시하는 기능을 추가한다.

posts.service.ts
1
// **** 포스트 댓글 수 증가
2
async incrementCommentCount(postId: number, qr?: QueryRunner) {
3
const repository = this.getRepository(qr)
4
5
await repository.increment({ id: postId }, 'commentCount', 1)
6
}
7
8
// **** 포스트 댓글 수 감소
9
async decrementCommentCount(postId: number, qr?: QueryRunner) {
10
const repository = this.getRepository(qr)
11
await repository.decrement({ id: postId }, 'commentCount', 1)
12
}

3. Comment Count

comments 서비스에 queryRunner를 추가한다.

comments.service.ts
1
getRepository(qr?: QueryRunner) {
2
return qr //
3
? qr.manager.getRepository<CommentsModel>(CommentsModel)
4
: this.commentsRepository
5
}
6
7
// **** (3) 댓글 생성
8
async createComment(
9
dto: CreateCommentsDto, //
10
postId: number,
11
author: UsersModel,
12
qr?: QueryRunner,
13
) {
14
const repository = this.getRepository(qr)
15
16
return repository.save({
17
...dto,
18
post: { id: postId },
19
author,
20
})
21
}
22
23
// **** (5) 댓글 삭제
24
async deleteComment(id: number, qr?: QueryRunner) {
25
const repository = this.getRepository(qr)
26
27
const comment = await repository.findOne({
28
where: { id },
29
})
30
31
if (!comment) {
32
throw new BadRequestException('존재하지 않는 댓글입니다.')
33
}
34
35
await repository.delete(id)
36
37
return id
38
}

comments 컨트롤러도 다음과 같이 수정한다.

comments.controller.ts
1
@Controller('posts/:postId/comments')
2
export class CommentsController {
3
constructor(private readonly commentsService: CommentsService, private readonly postsService: PostsService) {}
4
5
// 생략
6
7
// **** (3) 댓글 생성
8
@Post()
9
@UseInterceptors(TransactionInterceptor)
10
async postComment(
11
@Param('postId', ParseIntPipe) postId: number,
12
@Body() body: CreateCommentsDto,
13
@User() user: UsersModel,
14
@QueryRunner() qr: QR,
15
) {
16
const resp = await this.commentsService.createComment(body, postId, user, qr)
17
18
await this.postsService.incrementCommentCount(postId, qr)
19
20
return resp
21
}
22
23
// 생략
24
25
// **** (5) 댓글 삭제
26
@Delete(':commentId')
27
@UseGuards(IsCommentMineOrAdminGuard)
28
@UseInterceptors(TransactionInterceptor)
29
async deleteComment(
30
@Param('commentId', ParseIntPipe) commentId: number, //
31
@Param('postId', ParseIntPipe) postId: number,
32
@QueryRunner() qr: QR,
33
) {
34
const resp = await this.commentsService.deleteComment(commentId, qr)
35
36
await this.postsService.decrementCommentCount(postId, qr)
37
38
return resp
39
}
40
}