Skip to content

Commit 548aa28

Browse files
committed
Merge branch 'main' into refactor/api
2 parents b832ccc + 657c786 commit 548aa28

14 files changed

+180
-16
lines changed

server/src/comment/service/comment.service.ts

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import {
55
} from '@nestjs/common';
66
import { CommentRepository } from '../repository/comment.repository';
77
import { CreateCommentRequestDto } from '../dto/request/create-comment.dto';
8-
import { FeedRepository } from '../../feed/repository/feed.repository';
98
import { Payload } from '../../common/guard/jwt.guard';
109
import { DeleteCommentRequestDto } from '../dto/request/delete-comment.dto';
1110
import { UpdateCommentRequestDto } from '../dto/request/update-comment.dto';
1211
import { GetCommentRequestDto } from '../dto/request/get-comment.dto';
12+
import { DataSource } from 'typeorm';
13+
import { Comment } from '../entity/comment.entity';
1314
import { GetCommentResponseDto } from '../dto/response/get-comment.dto';
1415
import { FeedService } from '../../feed/service/feed.service';
1516
import { UserService } from '../../user/service/user.service';
@@ -18,17 +19,20 @@ import { UserService } from '../../user/service/user.service';
1819
export class CommentService {
1920
constructor(
2021
private readonly commentRepository: CommentRepository,
21-
private readonly feedRepository: FeedRepository,
22+
private readonly dataSource: DataSource,
2223
private readonly userService: UserService,
2324
private readonly feedService: FeedService,
2425
) {}
2526

26-
private async commentCheck(userInformation: Payload, commentId: number) {
27+
private async getValidatedComment(
28+
userInformation: Payload,
29+
commentId: number,
30+
) {
2731
const commentObj = await this.commentRepository.findOne({
2832
where: {
2933
id: commentId,
3034
},
31-
relations: ['user'],
35+
relations: ['user', 'feed'],
3236
});
3337

3438
if (!commentObj) {
@@ -52,25 +56,57 @@ export class CommentService {
5256
}
5357

5458
async create(userInformation: Payload, commentDto: CreateCommentRequestDto) {
55-
const feed = await this.feedService.getFeed(commentDto.feedId);
56-
const user = await this.userService.getUser(userInformation.id);
59+
const queryRunner = this.dataSource.createQueryRunner();
60+
await queryRunner.connect();
61+
await queryRunner.startTransaction();
5762

58-
await this.commentRepository.save({
59-
comment: commentDto.comment,
60-
feed,
61-
user,
62-
});
63+
try {
64+
const feed = await this.feedService.getFeed(commentDto.feedId);
65+
await this.userService.getUser(userInformation.id);
66+
feed.commentCount++;
67+
await queryRunner.manager.save(feed);
68+
await queryRunner.manager.save(Comment, {
69+
comment: commentDto.comment,
70+
feed: { id: commentDto.feedId },
71+
user: { id: userInformation.id },
72+
});
73+
74+
await queryRunner.commitTransaction();
75+
} catch (error) {
76+
await queryRunner.rollbackTransaction();
77+
throw error;
78+
} finally {
79+
await queryRunner.release();
80+
}
6381
}
6482

6583
async delete(userInformation: Payload, commentDto: DeleteCommentRequestDto) {
66-
await this.commentCheck(userInformation, commentDto.commentId);
67-
await this.feedRepository.delete({
68-
id: commentDto.commentId,
69-
});
84+
const comment = await this.getValidatedComment(
85+
userInformation,
86+
commentDto.commentId,
87+
);
88+
89+
const queryRunner = this.dataSource.createQueryRunner();
90+
await queryRunner.connect();
91+
await queryRunner.startTransaction();
92+
93+
try {
94+
const feed = comment.feed;
95+
feed.commentCount--;
96+
await queryRunner.manager.save(feed);
97+
await queryRunner.manager.remove(comment);
98+
99+
await queryRunner.commitTransaction();
100+
} catch (error) {
101+
await queryRunner.rollbackTransaction();
102+
throw error;
103+
} finally {
104+
await queryRunner.release();
105+
}
70106
}
71107

72108
async update(userInformation: Payload, commentDto: UpdateCommentRequestDto) {
73-
const commentObj = await this.commentCheck(
109+
const commentObj = await this.getValidatedComment(
74110
userInformation,
75111
commentDto.commentId,
76112
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class AlterFeedCommentCountColumn1754027996037
4+
implements MigrationInterface
5+
{
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE \`feed\`
8+
ADD \`comment_count\` int NOT NULL DEFAULT '0'`);
9+
}
10+
11+
public async down(queryRunner: QueryRunner): Promise<void> {
12+
await queryRunner.query(
13+
`ALTER TABLE \`feed\` DROP COLUMN \`comment_count\``,
14+
);
15+
}
16+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class UpdateFeedViewWithCommentCount1754028000000
4+
implements MigrationInterface
5+
{
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE OR REPLACE VIEW feed_view AS
9+
SELECT
10+
ROW_NUMBER() OVER (ORDER BY f.created_at) AS order_id,
11+
f.id AS id,
12+
f.title AS title,
13+
f.path AS path,
14+
f.created_at AS created_at,
15+
f.thumbnail AS thumbnail,
16+
f.view_count AS view_count,
17+
f.summary AS summary,
18+
f.like_count AS like_count,
19+
f.comment_count AS comment_count,
20+
r.name AS blog_name,
21+
r.blog_platform AS blog_platform,
22+
(
23+
SELECT JSON_ARRAYAGG(t.name)
24+
FROM tag_map tm
25+
INNER JOIN tag t ON t.id = tm.tag_id
26+
WHERE tm.feed_id = f.id
27+
) AS tag
28+
FROM feed f
29+
INNER JOIN rss_accept r ON r.id = f.blog_id
30+
GROUP BY f.id;`,
31+
);
32+
}
33+
34+
public async down(queryRunner: QueryRunner): Promise<void> {
35+
await queryRunner.query(
36+
`CREATE OR REPLACE VIEW feed_view AS
37+
SELECT
38+
ROW_NUMBER() OVER (ORDER BY f.created_at) AS order_id,
39+
f.id AS id,
40+
f.title AS title,
41+
f.path AS path,
42+
f.created_at AS created_at,
43+
f.thumbnail AS thumbnail,
44+
f.view_count AS view_count,
45+
f.summary AS summary,
46+
f.like_count AS like_count,
47+
r.name AS blog_name,
48+
r.blog_platform AS blog_platform,
49+
(
50+
SELECT JSON_ARRAYAGG(t.name)
51+
FROM tag_map tm
52+
INNER JOIN tag t ON t.id = tm.tag_id
53+
WHERE tm.feed_id = f.id
54+
) AS tag
55+
FROM feed f
56+
INNER JOIN rss_accept r ON r.id = f.blog_id
57+
GROUP BY f.id;`,
58+
);
59+
}
60+
}

server/src/feed/api-docs/readFeedDetail.api-docs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function ApiReadFeedDetail() {
3838
},
3939
},
4040
likes: { type: 'number' },
41+
comments: { type: 'number' },
4142
},
4243
},
4344
},
@@ -56,6 +57,7 @@ export function ApiReadFeedDetail() {
5657
summary: '#example/n ### exexample',
5758
tag: ['tag1', 'tag2'],
5859
likes: 0,
60+
comments: 0,
5961
},
6062
},
6163
}),

server/src/feed/api-docs/readFeedPagination.api-docs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export function ApiReadFeedPagination() {
6464
},
6565
},
6666
likes: { type: 'number' },
67+
comments: { type: 'number' },
6768
},
6869
},
6970
},
@@ -89,6 +90,7 @@ export function ApiReadFeedPagination() {
8990
isNew: false,
9091
tag: ['tag1', 'tag2'],
9192
likes: 0,
93+
comments: 0,
9294
},
9395
],
9496
lastId: 3,

server/src/feed/api-docs/readRecentFeedList.api-docs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function ApiReadRecentFeedList() {
3737
},
3838
},
3939
likes: { type: 'number' },
40+
comments: { type: 'number' },
4041
},
4142
},
4243
},
@@ -57,6 +58,7 @@ export function ApiReadRecentFeedList() {
5758
isNew: true,
5859
tag: ['tag1', 'tag2'],
5960
likes: 0,
61+
comments: 0,
6062
},
6163
],
6264
},

server/src/feed/api-docs/readTrendFeedList.api-docs.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function ApiReadTrendFeedList() {
3636
},
3737
},
3838
likes: { type: 'number' },
39+
comments: { type: 'number' },
3940
},
4041
},
4142
},
@@ -58,6 +59,7 @@ export function ApiReadTrendFeedList() {
5859
viewCount: 0,
5960
tag: ['tag1', 'tag2'],
6061
likes: 0,
62+
comments: 0,
6163
},
6264
{
6365
id: 2,
@@ -70,6 +72,7 @@ export function ApiReadTrendFeedList() {
7072
viewCount: 0,
7173
tag: ['tag1', 'tag2'],
7274
likes: 0,
75+
comments: 0,
7376
},
7477
],
7578
},
@@ -90,6 +93,7 @@ export function ApiReadTrendFeedList() {
9093
viewCount: 0,
9194
tag: ['tag1', 'tag2'],
9295
likes: 0,
96+
comments: 0,
9397
},
9498
{
9599
id: 4,
@@ -102,6 +106,7 @@ export function ApiReadTrendFeedList() {
102106
viewCount: 0,
103107
tag: ['tag1', 'tag2'],
104108
likes: 0,
109+
comments: 0,
105110
},
106111
],
107112
},

server/src/feed/api-docs/searchFeedList.api-docs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export function ApiSearchFeedList() {
103103
path: 'https://test.com/1',
104104
createdAt: '2024-10-27T02:08:55.000Z',
105105
likes: 0,
106+
comments: 0,
106107
},
107108
],
108109
totalPages: 3,

server/src/feed/dto/response/feed-detail.dto.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ export class FeedDetailResponseDto {
6262
})
6363
likes: number;
6464

65+
@ApiProperty({
66+
example: 1,
67+
description: '댓글 수',
68+
})
69+
comments: number;
70+
6571
@ApiProperty({
6672
example: ['example1', 'example2', 'example3'],
6773
description: '태그 배열',
@@ -84,6 +90,7 @@ export class FeedDetailResponseDto {
8490
viewCount: feed.viewCount,
8591
summary: feed.summary,
8692
likes: feed.likeCount,
93+
comments: feed.commentCount,
8794
tag: feed.tag ? feed.tag : [],
8895
});
8996
}

server/src/feed/dto/response/feed-pagination.dto.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class FeedResult {
1414
private isNew: boolean,
1515
private tag: string[],
1616
private likes: number,
17+
private comments: number,
1718
) {}
1819

1920
static toResultDto(feed: FeedPaginationResult) {
@@ -29,6 +30,7 @@ export class FeedResult {
2930
feed.isNew,
3031
feed.tag ? feed.tag : [],
3132
feed.likeCount,
33+
feed.commentCount,
3234
);
3335
}
3436

@@ -144,6 +146,12 @@ export class FeedTrendResponseDto {
144146
})
145147
likes: number;
146148

149+
@ApiProperty({
150+
example: 1,
151+
description: '댓글 개수',
152+
})
153+
comments: number;
154+
147155
@ApiProperty({
148156
example: ['example1', 'example2', 'example3'],
149157
description: '게시글 태그',
@@ -165,6 +173,7 @@ export class FeedTrendResponseDto {
165173
thumbnail: feed.thumbnail,
166174
viewCount: feed.viewCount,
167175
likes: feed.likeCount,
176+
comments: feed.commentCount,
168177
tag: feed.tag ? feed.tag : [],
169178
});
170179
}

0 commit comments

Comments
 (0)