Skip to content

Commit 377f6dc

Browse files
committed
[Feat]: 댓글 수정, 삭제 기능 구현
1 parent 9421bb6 commit 377f6dc

File tree

3 files changed

+53
-39
lines changed

3 files changed

+53
-39
lines changed

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

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.back.domain.comment.dto.CommentResponse;
55
import com.back.domain.comment.enums.CommentSortType;
66
import com.back.domain.comment.service.CommentService;
7-
import com.back.domain.post.dto.PostSummaryResponse;
87
import com.back.global.common.ApiResponse;
98
import com.back.global.common.PageResponse;
109
import io.swagger.v3.oas.annotations.Operation;
@@ -66,35 +65,26 @@ public ApiResponse<PageResponse<CommentResponse>> getPosts(
6665
return ApiResponse.success(PageResponse.of(responses), "성공적으로 조회되었습니다.", HttpStatus.OK);
6766
}
6867

69-
// // 게시글 단건 조회
70-
// @GetMapping("/{postId}")
71-
// @Operation(summary = "게시글 상세 조회", description = "게시글 ID로 게시글을 조회합니다.")
72-
// public ApiResponse<PostDetailResponse> getPost(
73-
// @Parameter(description = "조회할 게시글 ID", required = true) @PathVariable Long postId,
74-
// @RequestParam Long userId) {
75-
// return ApiResponse.success(postService.getPost(userId, postId), "성공적으로 조회되었습니다.", HttpStatus.OK);
76-
// }
77-
//
78-
// @PutMapping("/{postId}")
79-
// @Operation(summary = "게시글 수정", description = "게시글 ID로 게시글을 수정합니다.")
80-
// public ApiResponse<Long> updatePost(
81-
// @Parameter(description = "수정할 게시글 ID", required = true) @PathVariable Long postId,
82-
// @io.swagger.v3.oas.annotations.parameters.RequestBody(
83-
// description = "수정할 게시글 정보",
84-
// required = true
85-
// )
86-
// @RequestBody @Valid PostRequest request,
87-
// @RequestParam Long userId) {
88-
// return ApiResponse.success(postService.updatePost(userId, postId, request), "성공적으로 수정되었습니다.", HttpStatus.OK);
89-
// }
90-
//
91-
// @DeleteMapping("/{postId}")
92-
// @Operation(summary = "게시글 삭제", description = "게시글 ID로 게시글을 삭제합니다.")
93-
// public ApiResponse<Void> deletePost(
94-
// @Parameter(description = "삭제할 게시글 ID", required = true) @PathVariable Long postId,
95-
// @RequestParam Long userId) {
96-
// postService.deletePost(userId, postId);
97-
// return ApiResponse.success(null, "성공적으로 삭제되었습니다.", HttpStatus.OK);
98-
// }
9968

69+
@PutMapping("/{commentId}")
70+
@Operation(summary = "댓글 수정", description = "자신의 댓글을 수정합니다.")
71+
public ApiResponse<Long> updateComment(
72+
@Parameter(description = "수정할 댓글 ID", required = true) @PathVariable Long commentId,
73+
@io.swagger.v3.oas.annotations.parameters.RequestBody(
74+
description = "수정할 댓글 정보",
75+
required = true
76+
)
77+
@RequestBody @Valid CommentRequest request,
78+
@RequestParam Long userId) {
79+
return ApiResponse.success(commentService.updateComment(userId, commentId, request), "성공적으로 수정되었습니다.", HttpStatus.OK);
80+
}
81+
82+
@DeleteMapping("/{commentId}")
83+
@Operation(summary = "댓글 삭제", description = "자신의 댓글을 삭제합니다.")
84+
public ApiResponse<Void> deletePost(
85+
@Parameter(description = "삭제할 댓글 ID", required = true) @PathVariable Long commentId,
86+
@RequestParam Long userId) {
87+
commentService.deleteComment(userId, commentId);
88+
return ApiResponse.success(null, "성공적으로 삭제되었습니다.", HttpStatus.OK);
89+
}
10090
}

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import com.back.domain.post.entity.Post;
44
import com.back.domain.user.entity.User;
55
import com.back.global.baseentity.BaseEntity;
6+
import com.back.global.exception.ApiException;
7+
import com.back.global.exception.ErrorCode;
68
import jakarta.persistence.*;
7-
import lombok.AllArgsConstructor;
8-
import lombok.Builder;
9-
import lombok.Getter;
10-
import lombok.NoArgsConstructor;
11-
import lombok.Setter;
9+
import lombok.*;
1210
import org.hibernate.annotations.ColumnDefault;
1311
import org.springframework.data.annotation.LastModifiedDate;
1412

@@ -24,9 +22,8 @@
2422
@Entity
2523
@Table(name = "comments")
2624
@Getter
27-
@Setter
28-
@NoArgsConstructor
29-
@AllArgsConstructor
25+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
26+
@AllArgsConstructor(access = AccessLevel.PROTECTED)
3027
@Builder
3128
public class Comment extends BaseEntity {
3229

@@ -56,4 +53,13 @@ public class Comment extends BaseEntity {
5653

5754
@LastModifiedDate
5855
private LocalDateTime updatedAt;
56+
57+
public void checkUser(Long userId) {
58+
if (!user.getId().equals(userId))
59+
throw new ApiException(ErrorCode.UNAUTHORIZED_USER);
60+
}
61+
62+
public void updateContent(String content) {
63+
this.content = content;
64+
}
5965
}

back/src/main/java/com/back/domain/comment/service/CommentService.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import org.springframework.data.domain.Pageable;
2020
import org.springframework.data.domain.Sort;
2121
import org.springframework.stereotype.Service;
22+
import org.springframework.transaction.annotation.Transactional;
2223

2324
/**
2425
* 댓글 관련 비즈니스 로직을 처리하는 서비스.
2526
*/
2627
@Service
2728
@RequiredArgsConstructor
29+
@Transactional(readOnly = true)
2830
public class CommentService {
2931

3032
private final UserRepository userRepository;
@@ -48,4 +50,20 @@ public Page<CommentResponse> getComments(Long userId, Long postId, Pageable page
4850
Page<Comment> commentsPage = commentRepository.findCommentsByPostId(postId, pageable);
4951
return commentsPage.map(CommentMappers.COMMENT_READ::map);
5052
}
53+
54+
@Transactional
55+
public Long updateComment(Long userId, Long commentId, CommentRequest request) {
56+
Comment comment = commentRepository.findById(commentId)
57+
.orElseThrow(() -> new ApiException(ErrorCode.COMMENT_NOT_FOUND));
58+
comment.checkUser(userId);
59+
comment.updateContent(request.content());
60+
return comment.getId();
61+
}
62+
63+
public void deleteComment(Long userId, Long commentId) {
64+
Comment comment = commentRepository.findById(commentId)
65+
.orElseThrow(() -> new ApiException(ErrorCode.COMMENT_NOT_FOUND));
66+
comment.checkUser(userId);
67+
commentRepository.delete(comment);
68+
}
5169
}

0 commit comments

Comments
 (0)