Skip to content

Commit

Permalink
[#72] 댓글/게시글 삭제시 count 줄이기
Browse files Browse the repository at this point in the history
  • Loading branch information
rdd9223 committed Oct 2, 2023
1 parent 54ca5ca commit a0b0407
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
21 changes: 18 additions & 3 deletions server/src/comment/v1/comment-v1.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,24 @@ export class CommentV1Service {
userId: number;
commentId: number;
}): Promise<void> {
await this.commentRepository.delete({
id: commentId,
userId,
const comment = await this.commentRepository.findOne({
where: { id: commentId },
});

const transaction = await this.commentRepository.getTransaction();

await transaction.transaction(async (transactionalEntityManager) => {
await transactionalEntityManager.delete('Comment', {
id: commentId,
userId,
});

await transactionalEntityManager.decrement(
'Post',
{ id: comment.postId },
'commentCount',
1,
);
});
}
}
8 changes: 6 additions & 2 deletions server/src/entity/comment/comment.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Repository } from 'typeorm';
import { EntityManager, Repository } from 'typeorm';
import { CustomRepository } from 'src/db/typeorm-ex.decorator';
import { Comment } from './comment.entity';

@CustomRepository(Comment)
export class CommentRepository extends Repository<Comment> {}
export class CommentRepository extends Repository<Comment> {
async getTransaction(): Promise<EntityManager> {
return this.manager;
}
}
8 changes: 6 additions & 2 deletions server/src/entity/post/post.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Repository } from 'typeorm';
import { EntityManager, Repository } from 'typeorm';
import { CustomRepository } from 'src/db/typeorm-ex.decorator';
import { Post } from './post.entity';

@CustomRepository(Post)
export class PostRepository extends Repository<Post> {}
export class PostRepository extends Repository<Post> {
async getTransaction(): Promise<EntityManager> {
return this.manager;
}
}
11 changes: 10 additions & 1 deletion server/src/post/v1/post-v1.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,15 @@ export class PostV1Service {
throw new ForbiddenException('권한이 없습니다.');
}

await this.postRepository.delete({ id: postId });
const transaction = await this.postRepository.getTransaction();
await transaction.transaction(async (manager) => {
await manager.delete('Like', { postId });
await manager.decrement(
'Meeting',
{ id: post.meetingId },
'postCount',
1,
);
});
}
}

0 comments on commit a0b0407

Please sign in to comment.