Skip to content

Commit fb6eaf9

Browse files
authored
Refactor: board 도메인 리팩토링 (#245) (#271)
* Ref: comment 도메인 리팩토링 * Ref: post 도메인 리팩토링
1 parent 51d828e commit fb6eaf9

File tree

19 files changed

+274
-223
lines changed

19 files changed

+274
-223
lines changed

src/main/java/com/back/domain/board/comment/controller/CommentController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public ResponseEntity<RsData<CommentResponse>> createComment(
4141
));
4242
}
4343

44-
// 댓글 다건 조회
44+
// 댓글 목록 조회
4545
@GetMapping
4646
public ResponseEntity<RsData<PageResponse<CommentListResponse>>> getComments(
4747
@PathVariable Long postId,
4848
@AuthenticationPrincipal CustomUserDetails user,
4949
@PageableDefault(sort = "createdAt", direction = Sort.Direction.ASC) Pageable pageable
5050
) {
5151
Long userId = (user != null) ? user.getUserId() : null;
52-
PageResponse<CommentListResponse> response = commentService.getComments(postId, userId, pageable);
52+
PageResponse<CommentListResponse> response = commentService.getComments(postId, pageable, userId);
5353
return ResponseEntity
5454
.status(HttpStatus.OK)
5555
.body(RsData.success(

src/main/java/com/back/domain/board/comment/controller/docs/CommentControllerDocs.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,18 @@ ResponseEntity<RsData<PageResponse<CommentListResponse>>> getComments(
357357
),
358358
@ApiResponse(
359359
responseCode = "404",
360-
description = "존재하지 않는 게시글 또는 댓글",
360+
description = "존재하지 않는 사용자/게시글/댓글",
361361
content = @Content(
362362
mediaType = "application/json",
363363
examples = {
364+
@ExampleObject(name = "존재하지 않는 사용자", value = """
365+
{
366+
"success": false,
367+
"code": "USER_001",
368+
"message": "존재하지 않는 사용자입니다.",
369+
"data": null
370+
}
371+
"""),
364372
@ExampleObject(name = "존재하지 않는 게시글", value = """
365373
{
366374
"success": false,
@@ -473,10 +481,18 @@ ResponseEntity<RsData<CommentResponse>> updateComment(
473481
),
474482
@ApiResponse(
475483
responseCode = "404",
476-
description = "존재하지 않는 게시글 또는 댓글",
484+
description = "존재하지 않는 사용자/게시글/댓글",
477485
content = @Content(
478486
mediaType = "application/json",
479487
examples = {
488+
@ExampleObject(name = "존재하지 않는 사용자", value = """
489+
{
490+
"success": false,
491+
"code": "USER_001",
492+
"message": "존재하지 않는 사용자입니다.",
493+
"data": null
494+
}
495+
"""),
480496
@ExampleObject(name = "존재하지 않는 게시글", value = """
481497
{
482498
"success": false,

src/main/java/com/back/domain/board/comment/controller/docs/CommentLikeControllerDocs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface CommentLikeControllerDocs {
2929
examples = @ExampleObject(value = """
3030
{
3131
"success": true,
32-
"code": "SUCCESS_201",
32+
"code": "SUCCESS_200",
3333
"message": "댓글 좋아요가 등록되었습니다.",
3434
"data": {
3535
"commentId": 25,

src/main/java/com/back/domain/board/comment/entity/Comment.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static Comment createRoot(Post post, User user, String content) {
5858
/** 대댓글 생성 */
5959
public static Comment createChild(Post post, User user, String content, Comment parent) {
6060
Comment comment = new Comment(post, user, content, parent);
61-
parent.getChildren().add(comment);
61+
parent.addChildren(comment);
6262
return comment;
6363
}
6464

@@ -71,6 +71,14 @@ public void removeLike(CommentLike like) {
7171
this.commentLikes.remove(like);
7272
}
7373

74+
public void addChildren(Comment child) {
75+
this.children.add(child);
76+
}
77+
78+
public void removeChildren(Comment child) {
79+
this.children.remove(child);
80+
}
81+
7482
// -------------------- 비즈니스 메서드 --------------------
7583
/** 댓글 내용 수정 */
7684
public void update(String content) {
@@ -88,4 +96,14 @@ public void decreaseLikeCount() {
8896
this.likeCount--;
8997
}
9098
}
99+
100+
// -------------------- 헬퍼 메서드 --------------------
101+
/** 댓글 삭제 시 연관관계 정리 */
102+
public void remove() {
103+
this.post.removeComment(this);
104+
this.user.removeComment(this);
105+
if (this.parent != null) {
106+
this.parent.removeChildren(this);
107+
}
108+
}
91109
}

src/main/java/com/back/domain/board/comment/entity/CommentLike.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ public CommentLike(Comment comment, User user) {
2929
comment.addLike(this);
3030
user.addCommentLike(this);
3131
}
32+
33+
// -------------------- 헬퍼 메서드 --------------------
34+
/** 댓글 좋아요 삭제 시 연관관계 정리 */
35+
public void remove() {
36+
this.comment.removeLike(this);
37+
this.user.removeCommentLike(this);
38+
}
3239
}

src/main/java/com/back/domain/board/comment/service/CommentLikeService.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ public class CommentLikeService {
2626

2727
/**
2828
* 댓글 좋아요 서비스
29-
* 1. User 조회
30-
* 2. Comment 조회
31-
* 3. 이미 존재하는 경우 예외 처리
32-
* 4. CommentLike 저장 및 likeCount 증가
33-
* 5. 알림 이벤트 발행 (자기 글이 아닐 경우)
29+
*
30+
* @param commentId 댓글 ID
31+
* @param userId 사용자 ID
32+
* @return 댓글 좋아요 응답 DTO
3433
*/
3534
public CommentLikeResponse likeComment(Long commentId, Long userId) {
3635
// User 조회
@@ -41,21 +40,20 @@ public CommentLikeResponse likeComment(Long commentId, Long userId) {
4140
Comment comment = commentRepository.findById(commentId)
4241
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND));
4342

44-
// 이미 좋아요를 누른 경우 예외
43+
// 이미 좋아요한 경우 예외
4544
if (commentLikeRepository.existsByUserIdAndCommentId(userId, commentId)) {
4645
throw new CustomException(ErrorCode.COMMENT_ALREADY_LIKED);
4746
}
4847

49-
// 좋아요 수 증가
48+
// CommentLike 생성 및 좋아요 수 증가 처리
5049
comment.increaseLikeCount();
51-
52-
// CommentLike 저장 및 응답 반환
5350
commentLikeRepository.save(new CommentLike(comment, user));
5451

52+
// 댓글 좋아요 이벤트 발행 (자기 댓글이 아닐 때만)
5553
if (!comment.getUser().getId().equals(userId)) {
5654
eventPublisher.publishEvent(
5755
new CommentLikedEvent(
58-
userId, // 좋아요 누른 사람
56+
userId, // 좋아요한 사용자
5957
comment.getUser().getId(), // 댓글 작성자
6058
comment.getPost().getId(), // 게시글 ID
6159
comment.getId(),
@@ -69,10 +67,10 @@ public CommentLikeResponse likeComment(Long commentId, Long userId) {
6967

7068
/**
7169
* 댓글 좋아요 취소 서비스
72-
* 1. User 조회
73-
* 2. Comment 조회
74-
* 3. CommentLike 조회
75-
* 4. CommentLike 삭제 및 likeCount 감소
70+
*
71+
* @param commentId 댓글 ID
72+
* @param userId 사용자 ID
73+
* @return 댓글 좋아요 응답 DTO
7674
*/
7775
public CommentLikeResponse cancelLikeComment(Long commentId, Long userId) {
7876
// User 조회
@@ -87,17 +85,11 @@ public CommentLikeResponse cancelLikeComment(Long commentId, Long userId) {
8785
CommentLike commentLike = commentLikeRepository.findByUserIdAndCommentId(userId, commentId)
8886
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_LIKE_NOT_FOUND));
8987

90-
// 연관관계 제거
91-
comment.removeLike(commentLike);
92-
user.removeCommentLike(commentLike);
93-
94-
// CommentLike 삭제
88+
// CommentLike 삭제 및 좋아요 수 감소 처리
89+
commentLike.remove();
9590
commentLikeRepository.delete(commentLike);
96-
97-
// 좋아요 수 감소
9891
comment.decreaseLikeCount();
9992

100-
// 응답 반환
10193
return CommentLikeResponse.from(comment);
10294
}
10395
}

0 commit comments

Comments
 (0)