Skip to content

Commit

Permalink
[REFACTOR #109] 자유게시판&성지순례 인증글 댓글 등록, 삭제 리펙토링 (#110)
Browse files Browse the repository at this point in the history
[REFACTOR #109] 자유게시판&성지순례 인증글 댓글 등록, 삭제 리펙토링
  • Loading branch information
jorippppong authored Jul 22, 2024
2 parents ef0f708 + e2614e8 commit 36ca539
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 88 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'

// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'


//image
implementation 'com.google.cloud:google-cloud-storage:2.20.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.favoriteplace.app.controller;

import com.favoriteplace.app.domain.Member;
import com.favoriteplace.app.dto.community.CommentResponseDto;
import com.favoriteplace.app.dto.community.GuestBookRequestDto;
import com.favoriteplace.app.dto.community.GuestBookResponseDto;
import com.favoriteplace.app.dto.community.PostResponseDto;
import com.favoriteplace.app.dto.community.*;
import com.favoriteplace.app.repository.MemberRepository;
import com.favoriteplace.app.service.community.CommentCommandService;
import com.favoriteplace.app.service.community.CommentQueryService;
import com.favoriteplace.global.exception.ErrorCode;
import com.favoriteplace.global.exception.RestApiException;
import com.favoriteplace.global.util.SecurityUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,6 +23,7 @@ public class GuestBookCommentController {
private final SecurityUtil securityUtil;
private final CommentQueryService commentQueryService;
private final CommentCommandService commentCommandService;
private final MemberRepository memberRepository;

@GetMapping("/my-comments")
public GuestBookResponseDto.MyGuestBookCommentDto getMyComments(
Expand Down Expand Up @@ -57,9 +58,10 @@ public CommentResponseDto.PostCommentDto getGuestBookComments(
@PostMapping("/{guestbook_id}/comments")
public ResponseEntity<PostResponseDto.SuccessResponseDto> createGuestBookComment(
@PathVariable("guestbook_id") Long guestbookId,
@RequestBody GuestBookRequestDto.GuestBookCommentDto guestBookCommentDto
@RequestBody CommentRequestDto.CreateComment guestBookCommentDto
){
Member member = securityUtil.getUser();
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.createGuestBookComment(member, guestbookId, guestBookCommentDto);
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("댓글이 성공적으로 등록했습니다.").build(),
Expand All @@ -70,10 +72,11 @@ public ResponseEntity<PostResponseDto.SuccessResponseDto> createGuestBookComment
@PutMapping("/comments/{comment_id}")
public ResponseEntity<PostResponseDto.SuccessResponseDto> modifyGuestBookComment(
@PathVariable("comment_id") Long commentId,
@RequestBody GuestBookRequestDto.GuestBookCommentDto guestBookCommentDto
@RequestBody CommentRequestDto.ModifyComment dto
){
Member member = securityUtil.getUser();
commentCommandService.modifyGuestBookComment(member, commentId, guestBookCommentDto.getContent());
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.modifyComment(member, commentId, dto.getContent());
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("댓글 성공적으로 수정했습니다.").build(),
HttpStatus.OK
Expand All @@ -85,7 +88,8 @@ public ResponseEntity<PostResponseDto.SuccessResponseDto> deleteGuestBookComment
@PathVariable("comment_id") Long commentId
){
Member member = securityUtil.getUser();
commentCommandService.deleteGuestBookComment(member, commentId);
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.deleteComment(member, commentId);
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("댓글 성공적으로 삭제했습니다.").build(),
HttpStatus.OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import com.favoriteplace.app.dto.community.CommentRequestDto;
import com.favoriteplace.app.dto.community.CommentResponseDto;
import com.favoriteplace.app.dto.community.PostResponseDto;
import com.favoriteplace.app.repository.MemberRepository;
import com.favoriteplace.app.service.community.CommentCommandService;
import com.favoriteplace.app.service.community.CommentQueryService;
import com.favoriteplace.global.exception.ErrorCode;
import com.favoriteplace.global.exception.RestApiException;
import com.favoriteplace.global.util.SecurityUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -24,6 +26,7 @@ public class PostCommentController {
private final SecurityUtil securityUtil;
private final CommentQueryService commentQueryService;
private final CommentCommandService commentCommandService;
private final MemberRepository memberRepository;

@GetMapping("/my-comments")
public PostResponseDto.MyCommentDto getMyComments(
Expand Down Expand Up @@ -58,10 +61,11 @@ public CommentResponseDto.PostCommentDto getPostComments(
@PostMapping("/{post_id}/comments")
public ResponseEntity<PostResponseDto.SuccessResponseDto> createPostComment(
@PathVariable("post_id") long postId,
@RequestBody CommentRequestDto dto
@RequestBody CommentRequestDto.CreateComment dto
){
Member member = securityUtil.getUser();
commentCommandService.createPostComment(member, postId, dto.getContent());
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.createPostComment(member, postId, dto);
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("댓글을 성공적으로 등록했습니다.").build(),
HttpStatus.OK
Expand All @@ -71,10 +75,11 @@ public ResponseEntity<PostResponseDto.SuccessResponseDto> createPostComment(
@PutMapping("/comments/{comment_id}")
public ResponseEntity<PostResponseDto.SuccessResponseDto> modifyPostComment(
@PathVariable("comment_id") long commentId,
@RequestBody CommentRequestDto dto
@RequestBody CommentRequestDto.ModifyComment dto
){
Member member = securityUtil.getUser();
commentCommandService.modifyPostComment(member, commentId, dto.getContent());
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.modifyComment(member, commentId, dto.getContent());
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("댓글을 성공적으로 수정했습니다.").build(),
HttpStatus.OK
Expand All @@ -86,7 +91,8 @@ public ResponseEntity<PostResponseDto.SuccessResponseDto> deletePostComment(
@PathVariable("comment_id") long commentId
){
Member member = securityUtil.getUser();
commentCommandService.deletePostComment(member, commentId);
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.deleteComment(member, commentId);
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("댓글을 성공적으로 삭제했습니다.").build(),
HttpStatus.OK
Expand Down
55 changes: 41 additions & 14 deletions src/main/java/com/favoriteplace/app/domain/community/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@

import com.favoriteplace.app.domain.Member;
import com.favoriteplace.app.domain.common.BaseTimeEntity;
import com.favoriteplace.app.domain.community.GuestBook;
import com.favoriteplace.app.domain.community.Post;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import com.favoriteplace.app.domain.enums.CommentType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.aspectj.weaver.ArrayReferenceType;
import org.checkerframework.checker.units.qual.A;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.util.ArrayList;
import java.util.List;

@Getter
@Builder
@NoArgsConstructor(access = PROTECTED)
@AllArgsConstructor(access = PRIVATE)
@Entity

public class Comment extends BaseTimeEntity {

@Id @GeneratedValue(strategy = IDENTITY)
Expand All @@ -46,13 +49,37 @@ public class Comment extends BaseTimeEntity {
@Column(nullable = false)
private String content;

public void setGuestBook(GuestBook guestBook) {
this.guestBook = guestBook;
}
@Enumerated(EnumType.STRING)
private CommentType commentType;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "parent_comment_id")
private Comment parentComment; // 최상위 부모 댓글

@OneToMany(mappedBy = "parentComment")
private List<Comment> childComments = new ArrayList<>();

public void setPost(Post post){
this.post = post;
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "reference_comment_id")
private Comment referenceComment; // 대댓글 내에서 어떤 댓글을 참조한 것인지 (최상위 댓글을 참조하는 경우는 포함 X)

@Column(nullable = false)
@Builder.Default
private Boolean isDeleted = false;

public void setGuestBook(GuestBook guestBook) {this.guestBook = guestBook;}
public void setPost(Post post){this.post = post;}

public void addParentComment(Comment parentComment){
this.parentComment = parentComment;
parentComment.getChildComments().add(this);
}

public void setContent(String content){this.content = content;}
public void setReferenceComment(Comment referenceComment){this.referenceComment = referenceComment;}
public void setCommentType(CommentType commentType){this.commentType = commentType;}
public void modifyContent(String content){this.content = content;}
public void softDeleteComment(){
this.isDeleted = true;
this.content = "[삭제된 댓글입니다.]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ public void addComment(Comment comment){
comment.setGuestBook(this);
this.comments.add(comment);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public class Post extends BaseTimeEntity {

public void decreaseLikeCount(){this.likeCount--;}

public void addComment(Comment comment){
comment.setPost(this);
this.comments.add(comment);
}

public void disconnectImages(){
for(Image image:this.images){
image.setPost(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.favoriteplace.app.domain.enums;

public enum CommentType {
PARENT_COMMENT, CHILD_COMMENT
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CommentRequestDto {
private String content;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class CreateComment{
private Long parentCommentId;
private Long referenceCommentId;
private String content;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ModifyComment{
private String content;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
Long countByMember(Member member);
Boolean existsByParentComment(Comment parentComment);
Boolean existsByReferenceComment(Comment referenceComment);
}
Loading

0 comments on commit 36ca539

Please sign in to comment.