Skip to content

Commit

Permalink
[Feat] 댓글 수정 #133 (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
yr0202 authored Jan 23, 2025
2 parents d58f823 + d258476 commit e6d1910
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public enum ResponseCode implements Codable {
NOT_DELETE_AVAILABLE("6115", HttpStatus.BAD_REQUEST, "이미 삭제되었거나 삭제할 권한이 없습니다"),
FORBIDDEN("6116", HttpStatus.FORBIDDEN, "접근 권한 없음"),
NOT_EXIST_POST("6117", HttpStatus.BAD_REQUEST, "존재하지 않는 게시글입니다."),
NOT_EXIST_PARENT_COMMENT("6118", HttpStatus.BAD_REQUEST, "존재하지 않는 댓글입니다."),
NOT_EXIST_COMMENT("6118", HttpStatus.BAD_REQUEST, "존재하지 않는 댓글입니다."),
NOT_ALLOWED_NESTED_COMMENT("6119", HttpStatus.BAD_REQUEST, "대댓글에는 추가 댓글을 달 수 없습니다."),
NOT_AUTHORIZED_COMMENT_MODIFICATION("6120", HttpStatus.FORBIDDEN, "댓글 수정 권한이 없습니다."),

// 유효성 검사 오류 (형식: 62xx)
NOT_LITERAL("6211", HttpStatus.BAD_REQUEST, "문자열 형식이 아님"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -16,6 +17,7 @@
import com.palbang.unsemawang.common.util.pagination.CursorRequest;
import com.palbang.unsemawang.common.util.pagination.LongCursorResponse;
import com.palbang.unsemawang.community.dto.request.CommentRegisterRequest;
import com.palbang.unsemawang.community.dto.request.CommentUpdateRequest;
import com.palbang.unsemawang.community.dto.response.CommentReadResponse;
import com.palbang.unsemawang.community.service.CommentService;
import com.palbang.unsemawang.oauth2.dto.CustomOAuth2User;
Expand All @@ -34,7 +36,7 @@ public class CommentController {

@Operation(
description = "커서 기반 페이징 처리된 댓글 조회 기능입니다. 처음 조회시에는 cursorKey를 null로 보내시면 됩니다",
summary = "커서 기반 페이징 댓글 조회"
summary = "댓글 조회 (커서 기반 페이징)"
)
@GetMapping
public ResponseEntity<LongCursorResponse<CommentReadResponse>> getAllCommentsByPostId(
Expand Down Expand Up @@ -72,4 +74,24 @@ public ResponseEntity<Void> registerCommentByPostId(

return ResponseEntity.status(HttpStatus.CREATED).build();
}

@Operation(
description = "해당 게시글에 본인이 작성한 댓글/대댓글을 수정할 수 있습니다.",
summary = "댓글/대댓글 수정"
)
@PutMapping("/{commentId}")
public ResponseEntity<Void> updateCommentByPostId(
@AuthenticationPrincipal CustomOAuth2User auth,
@PathVariable Long postId,
@PathVariable Long commentId,
@RequestBody @Valid CommentUpdateRequest request
) {
if (auth == null || auth.getId() == null) {
throw new GeneralException(ResponseCode.EMPTY_TOKEN);
}

commentService.updateComment(postId, commentId, request, auth.getId());

return ResponseEntity.status(HttpStatus.OK).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.palbang.unsemawang.community.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommentUpdateRequest {
@Schema(required = true, description = "댓글 내용")
@NotBlank
@Size(min = 1, max = 250)
private String content;

@Schema(required = false, description = "익명 여부", defaultValue = "false")
private Boolean isAnonymous;
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@ public void deleteComment() {
this.isDeleted = true;
this.deletedAt = LocalDateTime.now();
}

public void updateComment(String content, Boolean isAnonymous) {
this.content = content;
this.isAnonymous = isAnonymous;
this.updatedAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface PostRepository extends JpaRepository<Post, Long> {
Optional<Post> findByIdAndMember(Long id, Member member);

@Query("SELECT p FROM Post p WHERE p.id = :postId AND p.member = :member AND p.isDeleted = false")
Optional<Post> findBuMemberIsNotDeleted(Long postId, Member member);
Optional<Post> findByMemberIsNotDeleted(Long postId, Member member);

// 조회수 증가
@Modifying
Expand Down Expand Up @@ -54,4 +54,6 @@ List<Post> findPopularPosts(
@Param("cursorId") Long cursorId,
Pageable pageable
);

Optional<Post> findByIdAndIsDeletedFalse(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.palbang.unsemawang.community.service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
Expand All @@ -11,6 +12,7 @@
import com.palbang.unsemawang.common.util.pagination.CursorRequest;
import com.palbang.unsemawang.common.util.pagination.LongCursorResponse;
import com.palbang.unsemawang.community.dto.request.CommentRegisterRequest;
import com.palbang.unsemawang.community.dto.request.CommentUpdateRequest;
import com.palbang.unsemawang.community.dto.response.CommentReadResponse;
import com.palbang.unsemawang.community.entity.Comment;
import com.palbang.unsemawang.community.entity.Post;
Expand All @@ -23,6 +25,7 @@

@Service
@RequiredArgsConstructor
@Transactional
public class CommentService {
private final CommentRepository commentRepository;
private final PostRepository postRepository;
Expand Down Expand Up @@ -82,7 +85,7 @@ public void registerComment(Long postId, CommentRegisterRequest request, String
if (request.getParentCommentId() != null) {
// 부모 댓글 존재 확인
parentComment = commentRepository.findById(request.getParentCommentId())
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_PARENT_COMMENT));
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_COMMENT));

// 대대댓글 방지 검증 로직
if (parentComment.getParentComment() != null) {
Expand All @@ -102,4 +105,22 @@ public void registerComment(Long postId, CommentRegisterRequest request, String
// 댓글 저장
commentRepository.save(comment);
}
}

public void updateComment(Long postId, Long commentUd, CommentUpdateRequest request, String memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_MEMBER_ID));

postRepository.findByIdAndIsDeletedFalse(postId)
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_POST));

Comment comment = commentRepository.findById(commentUd)
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_COMMENT));

// 로그인한 사용자와 댓글 작성자가 일치하는지 확인
if (!Objects.equals(comment.getMember().getId(), member.getId())) {
throw new GeneralException(ResponseCode.NOT_AUTHORIZED_COMMENT_MODIFICATION);
}

comment.updateComment(request.getContent(), request.getIsAnonymous()); // dirty-check -> save() 필요없음
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public boolean delete(PostDeleteRequest postDeleteRequest) {
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_MEMBER_ID));

// 2. 회원, 게시글 id가 일치하되 삭제되지 않은 게시글 조회
Post post = postRepository.findBuMemberIsNotDeleted(postDeleteRequest.getPostId(), member)
Post post = postRepository.findByMemberIsNotDeleted(postDeleteRequest.getPostId(), member)
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_DELETE_AVAILABLE));

// 3. 게시글 삭제 처리
Expand Down

0 comments on commit e6d1910

Please sign in to comment.