Skip to content

Commit 306084d

Browse files
committed
Merge branch 'feat/#30' into release
2 parents 0f9a5a7 + 30390d4 commit 306084d

5 files changed

Lines changed: 83 additions & 7 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.back.web7_9_codecrete_be.domain.community.comment.controller;
22

33
import com.back.web7_9_codecrete_be.domain.community.comment.dto.request.CommentCreateRequest;
4+
import com.back.web7_9_codecrete_be.domain.community.comment.dto.request.CommentUpdateRequest;
45
import com.back.web7_9_codecrete_be.domain.community.comment.service.CommentService;
56
import com.back.web7_9_codecrete_be.domain.users.entity.User;
67
import com.back.web7_9_codecrete_be.global.rq.Rq;
@@ -50,4 +51,16 @@ public RsData<?> deleteComment(
5051
commentService.delete(commentId, user.getId());
5152
return RsData.success("댓글이 삭제되었습니다.");
5253
}
54+
55+
@Operation(summary = "댓글 수정", description = "작성자 본인만 댓글을 수정할 수 있습니다.")
56+
@PatchMapping("/{commentId}")
57+
public RsData<?> updateComment(
58+
@PathVariable Long postId,
59+
@PathVariable Long commentId,
60+
@Valid @RequestBody CommentUpdateRequest req
61+
) {
62+
User user = rq.getUser();
63+
commentService.update(commentId, req, user.getId());
64+
return RsData.success("댓글이 수정되었습니다.");
65+
}
5366
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.back.web7_9_codecrete_be.domain.community.comment.dto.request;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
import jakarta.validation.constraints.NotBlank;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
8+
@Getter
9+
@NoArgsConstructor
10+
@Schema(description = "댓글 수정 요청 DTO")
11+
public class CommentUpdateRequest {
12+
13+
@NotBlank(message = "댓글 내용은 비어 있을 수 없습니다.")
14+
@Schema(description = "수정할 댓글 내용", example = "내용을 수정했습니다.")
15+
private String content;
16+
}

src/main/java/com/back/web7_9_codecrete_be/domain/community/comment/dto/response/CommentResponse.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,31 @@ public class CommentResponse {
2424
@Schema(description = "작성일시", example = "2025-01-03T18:40:00")
2525
private LocalDateTime createdDate;
2626

27+
@Schema(description = "수정일시", example = "2025-01-04T10:15:00")
28+
private LocalDateTime modifiedDate;
29+
30+
@Schema(description = "수정 여부", example = "true")
31+
private Boolean isEdited;
32+
2733
public static CommentResponse from(Comment comment) {
2834
return CommentResponse.builder()
2935
.commentId(comment.getCommentId())
3036
.userId(comment.getUserId())
3137
.content(comment.getContent())
3238
.createdDate(comment.getCreatedDate())
39+
.modifiedDate(comment.getModifiedDate())
40+
.isEdited(isEdited(comment))
3341
.build();
3442
}
43+
44+
private static boolean isEdited(Comment comment) {
45+
if (comment.getModifiedDate() == null) {
46+
return false;
47+
}
48+
49+
LocalDateTime created = comment.getCreatedDate().withNano(0);
50+
LocalDateTime modified = comment.getModifiedDate().withNano(0);
51+
52+
return !created.isEqual(modified);
53+
}
3554
}

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.back.web7_9_codecrete_be.domain.community.comment.service;
22

33
import com.back.web7_9_codecrete_be.domain.community.comment.dto.request.CommentCreateRequest;
4+
import com.back.web7_9_codecrete_be.domain.community.comment.dto.request.CommentUpdateRequest;
45
import com.back.web7_9_codecrete_be.domain.community.comment.dto.response.CommentPageResponse;
56
import com.back.web7_9_codecrete_be.domain.community.comment.dto.response.CommentResponse;
67
import com.back.web7_9_codecrete_be.domain.community.comment.entity.Comment;
78
import com.back.web7_9_codecrete_be.domain.community.comment.repository.CommentRepository;
89
import com.back.web7_9_codecrete_be.domain.community.post.entity.Post;
9-
import com.back.web7_9_codecrete_be.domain.community.post.repository.PostRepository;
10+
import com.back.web7_9_codecrete_be.domain.community.post.service.PostService;
1011
import com.back.web7_9_codecrete_be.global.error.code.CommentErrorCode;
11-
import com.back.web7_9_codecrete_be.global.error.code.PostErrorCode;
1212
import com.back.web7_9_codecrete_be.global.error.exception.BusinessException;
1313
import lombok.RequiredArgsConstructor;
1414
import org.springframework.data.domain.Page;
@@ -24,13 +24,12 @@
2424
public class CommentService {
2525

2626
private final CommentRepository commentRepository;
27-
private final PostRepository postRepository;
27+
private final PostService postService;
2828

2929
// 댓글 생성
3030
@Transactional
3131
public Long create(Long postId, CommentCreateRequest req, Long userId) {
32-
Post post = postRepository.findById(postId)
33-
.orElseThrow(() -> new BusinessException(PostErrorCode.POST_NOT_FOUND));
32+
Post post = postService.getPostOrThrow(postId);
3433

3534
Comment comment = Comment.create(
3635
post,
@@ -44,6 +43,8 @@ public Long create(Long postId, CommentCreateRequest req, Long userId) {
4443
// 댓글 조회
4544
public CommentPageResponse<CommentResponse> getComments(Long postId, int page) {
4645

46+
postService.validatePostExists(postId);
47+
4748
Pageable pageable = PageRequest.of(
4849
page - 1,
4950
20,
@@ -63,10 +64,24 @@ public void delete(Long commentId, Long userId) {
6364
Comment comment = commentRepository.findById(commentId)
6465
.orElseThrow(() -> new BusinessException(CommentErrorCode.COMMENT_NOT_FOUND));
6566

67+
validateOwner(comment, userId);
68+
69+
commentRepository.delete(comment);
70+
}
71+
72+
@Transactional
73+
public void update(Long commentId, CommentUpdateRequest req, Long userId) {
74+
Comment comment = commentRepository.findById(commentId)
75+
.orElseThrow(() -> new BusinessException(CommentErrorCode.COMMENT_NOT_FOUND));
76+
77+
validateOwner(comment, userId);
78+
79+
comment.update(req.getContent());
80+
}
81+
82+
private void validateOwner(Comment comment, Long userId) {
6683
if (!comment.getUserId().equals(userId)) {
6784
throw new BusinessException(CommentErrorCode.NO_COMMENT_PERMISSION);
6885
}
69-
70-
commentRepository.delete(comment);
7186
}
7287
}

src/main/java/com/back/web7_9_codecrete_be/domain/community/post/service/PostService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,17 @@ public PostPageResponse<PostResponse> getPostsByCategory(
121121

122122
return PostPageResponse.from(result);
123123
}
124+
125+
@Transactional(readOnly = true)
126+
public void validatePostExists(Long postId) {
127+
if (!postRepository.existsById(postId)) {
128+
throw new BusinessException(PostErrorCode.POST_NOT_FOUND);
129+
}
130+
}
131+
132+
@Transactional(readOnly = true)
133+
public Post getPostOrThrow(Long postId) {
134+
return postRepository.findById(postId)
135+
.orElseThrow(() -> new BusinessException(PostErrorCode.POST_NOT_FOUND));
136+
}
124137
}

0 commit comments

Comments
 (0)