1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167import {
BadRequestException,
Injectable,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-comment.dto';
import { Comments } from 'src/entities/Comments';
import { InjectRepository } from '@nestjs/typeorm';
import { DataSource, Repository } from 'typeorm';
import { UserRoleType, Users } from 'src/entities/Users';
import { Posts } from 'src/entities/Posts';
@Injectable()
export class CommentsService {
constructor(
@InjectRepository(Comments)
private commentsRepository: Repository<Comments>,
@InjectRepository(Users)
private usersRepository: Repository<Users>,
@InjectRepository(Posts)
private postsRepository: Repository<Posts>,
private dataSource: DataSource,
) {}
async createComment(userId: number, createCommentDto: CreateCommentDto) {
const { content, postId, parentId } = createCommentDto;
const user = await this.usersRepository.findOne({
where: { id: userId },
});
if (!user) {
throw new NotFoundException('ν΄λΉ μμ΄λμ μ μ λ₯Ό μ°Ύμ μ μμ΅λλ€.');
}
const post = await this.postsRepository.findOne({
where: { id: postId },
});
if (!post) {
throw new NotFoundException('ν΄λΉ κΈμ μ°Ύμ μ μμ΅λλ€.');
}
const comment = new Comments();
comment.content = content;
comment.post = post;
comment.user = user;
if (parentId) {
const parentComment = await this.commentsRepository.findOne({
where: { id: parentId },
});
if (!parentComment) {
throw new NotFoundException(
'λλκΈμ μμ±ν λκΈμ΄ μ‘΄μ¬νμ§ μμ΅λλ€.',
);
}
// λΆλͺ¨ νμ
comment.parent = parentComment;
}
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
await queryRunner.manager.getRepository(Comments).save(comment);
await queryRunner.commitTransaction();
return comment;
} catch (error) {
await queryRunner.rollbackTransaction();
throw new BadRequestException(`λκΈ μμ± μ€ν¨ error: ${error} `);
} finally {
await queryRunner.release();
}
}
async getCommentById(id: number) {
const comment = await this.commentsRepository
.createQueryBuilder('comment')
.leftJoin('comment.user', 'user')
.addSelect(['user.id', 'user.nickname'])
.where('comment.id = :id', { id })
.getOne();
if (!comment) {
throw new NotFoundException('μ‘΄μ¬νμ§ μλ λκΈμ
λλ€.');
}
return comment;
}
async getCommentsByPostId(postId: number) {
try {
const comments = await this.commentsRepository
.createQueryBuilder('comment')
.leftJoin('comment.user', 'user')
.leftJoin('comment.parent', 'parentComment')
.addSelect(['user.nickname', 'parentComment.id'])
.where('comment.post.id = :postId', { postId })
.withDeleted() // λκΈμ μμ λ λκΈλ κ°μ Έμ¨λ€
.getMany();
console.log(comments);
// κ³μΈ΅ ꡬ쑰λ₯Ό λ§λ€μ΄ 리ν΄
return this.buildCommentsHierarchy(comments);
} catch (error) {
throw new BadRequestException('λκΈ μ‘°ν μ€ν¨');
}
}
async updateComment(
id: number,
userId: number,
userRole: UserRoleType,
updateCommentDto: UpdateCommentDto,
) {
const comment = await this.getCommentById(id);
if (userRole !== UserRoleType.ADMIN && comment.user.id !== userId) {
throw new UnauthorizedException('λκΈ μμ κΆνμ΄ μμ΅λλ€.');
}
const newComment = {
...comment,
...updateCommentDto,
};
try {
return await this.commentsRepository.save(newComment);
} catch (error) {
throw new BadRequestException('λκΈ μμ μ€ν¨');
}
}
async deleteComment(id: number, userId: number, userRole: UserRoleType) {
const comment = await this.getCommentById(id);
if (userRole !== UserRoleType.ADMIN && comment.user.id !== userId) {
throw new UnauthorizedException('λκΈ μμ κΆνμ΄ μμ΅λλ€.');
}
try {
await this.commentsRepository.softRemove(comment);
} catch (error) {
throw new BadRequestException('κΈ μμ μ€ν¨');
}
}
// 무μμ λκΈ λ°°μ΄μ κ³μΈ΅ κ΅¬μ‘°λ‘ λ§λ€μ΄ μ£Όλ ν¨μ
private buildCommentsHierarchy(comments: Comments[]): Comments[] {
const commentsMap = new Map<number, Comments>();
const roots: Comments[] = [];
comments.forEach((comment) => {
commentsMap.set(comment.id, comment);
comment.children = [];
});
comments.forEach((comment) => {
if (comment.parent) {
const parent = commentsMap.get(comment.parent.id);
if (parent) {
parent.children.push(comment);
}
} else {
roots.push(comment);
}
});
return roots;
}
}