Skip to content

Commit

Permalink
Merge pull request #221 from Kernel360/refactor/comment-refactor-#195
Browse files Browse the repository at this point in the history
Refactor: comment refactor
  • Loading branch information
jeongns2611 authored Feb 10, 2025
2 parents ffbbd22 + b45f9e0 commit e624f57
Show file tree
Hide file tree
Showing 22 changed files with 412 additions and 186 deletions.
57 changes: 20 additions & 37 deletions Common/src/main/java/com/seveneleven/entity/board/Comment.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.seveneleven.entity.board;

import com.seveneleven.entity.global.BaseEntity;
import com.seveneleven.entity.global.YesNo;
import com.seveneleven.entity.global.converter.YesNoConverter;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -21,7 +19,6 @@ public class Comment extends BaseEntity {
ref : 댓글 그룹 구분
refOrder : 댓글 그룹 순서
childCommentNum : 자식 댓글의 수
isActive : 사용 유무 (Y, N)
content : 내용
writer : 작성자 이름
registerIp : 등록자 IP
Expand All @@ -35,11 +32,11 @@ public class Comment extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = false, referencedColumnName = "id")
private Post postId;
private Post post;

@JoinColumn(name = "parent_comment_id")
@ManyToOne(fetch = FetchType.LAZY)
private Comment parentCommentId;
private Comment parentComment;

@Column(name = "ref")
private Long ref;
Expand All @@ -50,11 +47,6 @@ public class Comment extends BaseEntity {
@Column(name = "child_comment_num")
private Integer childCommentNum;

@Column(name = "is_active", nullable = false)
@Convert(converter = YesNoConverter.class)
@Enumerated(EnumType.STRING)
private YesNo isActive;

@Column(name = "content", nullable = false, columnDefinition = "TEXT")
private String content;

Expand All @@ -68,23 +60,22 @@ public class Comment extends BaseEntity {
private String modifierIp;

private Comment (Long id,
Post post,
Comment parentComment,
Long ref,
Integer refOrder,
Integer childCommentNum,
YesNo isActive,
String content,
String writer,
String registerIp,
String modifierIp) {
Post post,
Comment parentComment,
Long ref,
Integer refOrder,
Integer childCommentNum,
String content,
String writer,
String registerIp,
String modifierIp
) {
this.id = id;
this.postId = post;
this.parentCommentId = parentComment;
this.post = post;
this.parentComment = parentComment;
this.ref = ref;
this.refOrder = refOrder;
this.childCommentNum = childCommentNum;
this.isActive = isActive;
this.content = content;
this.writer = writer;
this.registerIp = registerIp;
Expand All @@ -103,32 +94,24 @@ public static Comment createComment(
String registerIp
) {
Comment comment = new Comment();
comment.postId = post;
comment.parentCommentId = parentComment;
comment.post = post;
comment.parentComment = parentComment;
comment.ref = ref;
comment.refOrder = refOrder;
comment.childCommentNum = childCommentNum;
comment.isActive = YesNo.YES;
comment.content = content;
comment.writer = writer;
comment.registerIp = registerIp;
return comment;
comment.modifierIp = null;
return comment;
}

// 수정 메서드
public void updateComment(
String content,
String modifierIp
) {
this.content = content;
this.modifierIp = modifierIp;
}

// 삭제 메서드
public void deleteComment(
String content,
String modifierIp
) {
this.isActive = YesNo.NO;
this.content = content;
this.modifierIp = modifierIp;
}

Expand Down
112 changes: 112 additions & 0 deletions Common/src/main/java/com/seveneleven/entity/board/CommentHistory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.seveneleven.entity.board;

import com.seveneleven.entity.board.constant.HistoryAction;
import com.seveneleven.entity.global.BaseEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "comment_history")
public class CommentHistory extends BaseEntity {

/*
id : 댓글 ID
postId : 게시물 ID
parentCommentId : 부모 댓글 ID
ref : 댓글 그룹 구분
refOrder : 댓글 그룹 순서
childCommentNum : 자식 댓글의 수
content : 내용
writer : 작성자 이름
action : 작업 종류 (CREATE, UPDATE, DELETE)
registerIp : 등록자 IP
modifierIp : 수정자 IP
*/

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "post_id", nullable = false)
private Long postId;

@Column(name = "parent_comment_id")
private Long parentCommentId;

@Column(name = "ref")
private Long ref;

@Column(name = "ref_order")
private Integer refOrder;

@Column(name = "child_comment_num")
private Integer childCommentNum;

@Column(name = "content", nullable = false, columnDefinition = "TEXT")
private String content;

@Column(name = "writer", nullable = false)
private String writer;

@Column(name = "action", nullable = false)
@Enumerated(EnumType.STRING)
private HistoryAction action; // 작업 종류 (CREATE, UPDATE, DELETE)

@Column(name = "register_ip", length = 50)
private String registerIp;

@Column(name = "modifier_ip", length = 50)
private String modifierIp;

private CommentHistory(
Long postId,
Long parentCommentId,
Long ref,
Integer refOrder,
Integer childCommentNum,
String content,
String writer,
HistoryAction action,
String registerIp,
String modifierIp
) {
this.postId = postId;
this.parentCommentId = parentCommentId;
this.ref = ref;
this.refOrder = refOrder;
this.childCommentNum = childCommentNum;
this.content = content;
this.writer = writer;
this.action = action;
this.registerIp = registerIp;
this.modifierIp = modifierIp;
}

// 댓글 이력 생성
public static CommentHistory createCommentHistory(Comment comment, HistoryAction action, String registerIp, String modifierIp) {
return new CommentHistory(
comment.getPost().getId(),
getParentCommentId(comment),
comment.getRef(),
comment.getRefOrder(),
comment.getChildCommentNum(),
comment.getContent(),
comment.getWriter(),
action,
registerIp,
modifierIp
);
}

private static Long getParentCommentId(Comment comment) {
if (comment.getParentComment() != null) {
return comment.getParentComment().getId();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.seveneleven.entity.board;

import com.seveneleven.entity.board.constant.PostAction;
import com.seveneleven.entity.board.constant.HistoryAction;
import com.seveneleven.entity.board.constant.PostStatus;
import com.seveneleven.entity.board.constant.TaskPriority;
import com.seveneleven.entity.global.BaseEntity;
Expand All @@ -17,7 +17,7 @@
@Table(name = "post_history")
public class PostHistory extends BaseEntity {

/*
/*
id : 게시물 이력 ID
projectStepId : 프로젝트 단계 ID
parentPostId : 부모 게시물 ID
Expand Down Expand Up @@ -68,7 +68,7 @@ public class PostHistory extends BaseEntity {

@Column(name = "action", nullable = false)
@Enumerated(EnumType.STRING)
private PostAction action; // 작업 종류 (CREATE, UPDATE, DELETE)
private HistoryAction action; // 작업 종류 (CREATE, UPDATE, DELETE)

@Column(name = "registered_ip", length = 50)
private String registeredIp; // 등록자 IP
Expand All @@ -77,7 +77,7 @@ public class PostHistory extends BaseEntity {
private String modifierIp; // 수정자 IP

// 게시글 이력 생성
public static PostHistory createPostHistory(Post post, PostAction action, String ip) {
public static PostHistory createPostHistory(Post post, HistoryAction action, String ip) {
PostHistory postHistory = new PostHistory();
postHistory.projectStepId = post.getProjectStep().getId();
postHistory.parentPostId = postHistory.getParentPostId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@Getter
@RequiredArgsConstructor
public enum PostAction {
public enum HistoryAction {

CREATE("생성"),
UPDATE("수정"),
Expand Down
4 changes: 2 additions & 2 deletions Common/src/main/java/com/seveneleven/response/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public enum ErrorCode {
NOT_FOUND_PROJECT_STEP(2501, HttpStatus.NOT_FOUND, "해당 프로젝트 단계를 찾을 수 없습니다."),
NOT_FOUND_MEMBER(2502, HttpStatus.NOT_FOUND, "해당 회원을 찾을 수 없습니다."),
NOT_FOUND_POST(2503, HttpStatus.NOT_FOUND, "해당 게시물을 찾을 수 없습니다."),
NOT_MATCH_WRITER(2504, HttpStatus.FORBIDDEN, "작성자만 수정 및 삭제할 수 있습니다."),
NOT_HAVE_EDIT_PERMISSION(2504, HttpStatus.FORBIDDEN, "수정 및 삭제 권한이 없습니다."),
NOT_FOUND_WRITER(2505, HttpStatus.NOT_FOUND, "작성자 정보를 찾을 수 없습니다."),
NOT_DELETE_PARENT_POST(2506, HttpStatus.BAD_REQUEST, "관련된 게시글이 존재하는 경우 해당 게시글을 삭제할 수 없습니다."),
NOT_DELETE_PARENT_COMMENT(2507, HttpStatus.BAD_REQUEST, "관련된 댓글이 존재하는 경우, 해당 댓글을 삭제할 수 없습니다."),
NOT_MATCH_PROJECTSTEPID(2508, HttpStatus.BAD_REQUEST, "게시글의 프로젝트 단계가 일치하지 않습니다."),
NOT_FOUND_COMMENT(2509, HttpStatus.NOT_FOUND, "해당 댓글(부모 댓글)을 찾을 수 없습니다."),
NOT_FOUND_COMMENT(2509, HttpStatus.NOT_FOUND, "해당 댓글을 찾을 수 없습니다."),
// 3000번대 코드 : DB 관련
FILE_NOT_FOUND_ERROR(3000, HttpStatus.NOT_FOUND, "해당 파일을 찾을 수 없습니다."),
LINK_NOT_FOUND_ERROR(3001, HttpStatus.NOT_FOUND, "해당 링크를 찾을 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public String getEmail(){

@Override
public String getUsername() {
return member.getUsername();
return member.getName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class MemberDto {

private Long id;
private String loginId;
private String username;
private String name;
private String email;
private String password;
private Role role;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ResponseEntity<APIResponse<SuccessCode>> createComment(@AuthenticationPri
@Valid @RequestBody PostCommentRequest postCommentRequest,
HttpServletRequest request
) {
commentService.createComment(postId, postCommentRequest, request, user.getMember().getId());
commentService.createComment(postId, postCommentRequest, request, user.getUsername());
return ResponseEntity.status(SuccessCode.CREATED.getStatus())
.body(APIResponse.success(SuccessCode.CREATED));
}
Expand All @@ -40,7 +40,7 @@ public ResponseEntity<APIResponse<SuccessCode>> updateComment(@AuthenticationPri
@RequestBody PatchCommentRequest patchCommentRequest,
HttpServletRequest request
) {
commentService.updateComment(postId, commentId, patchCommentRequest, request, user.getMember().getId());
commentService.updateComment(postId, commentId, patchCommentRequest, request, user);
return ResponseEntity.status(SuccessCode.UPDATED.getStatus())
.body(APIResponse.success(SuccessCode.UPDATED));
}
Expand All @@ -52,7 +52,7 @@ public ResponseEntity<APIResponse<SuccessCode>> deleteComment(@AuthenticationPri
@PathVariable Long commentId,
HttpServletRequest request
) {
commentService.deleteComment(postId, commentId, request, user.getMember().getId());
commentService.deleteComment(postId, commentId, request, user);
return ResponseEntity.status(SuccessCode.DELETED.getStatus())
.body(APIResponse.success(SuccessCode.DELETED));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seveneleven.board.dto;

import com.seveneleven.entity.board.Comment;
import com.seveneleven.entity.global.YesNo;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -16,7 +17,17 @@ public class GetCommentResponse {
private String writer;
private String content;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private YesNo isEdited;

private GetCommentResponse(Comment comment, YesNo isEdited) {
this.commentId = comment.getId();
this.parentCommentId = getParentCommentId(comment.getParentComment());
this.registerId = comment.getCreatedBy();
this.writer = comment.getWriter();
this.content = comment.getContent();
this.createdAt = getCreatedAt(comment);
this.isEdited = isEdited;
}

@Override
public String toString() {
Expand All @@ -25,24 +36,21 @@ public String toString() {
'}';
}

private GetCommentResponse(Comment comment) {
this.commentId = comment.getId();
this.parentCommentId = getParentCommentId(comment.getParentCommentId());
this.registerId = comment.getCreatedBy();
this.writer = comment.getWriter();
this.content = comment.getContent();
this.createdAt = comment.getCreatedAt();
this.updatedAt = comment.getUpdatedAt();
// Comment 를 GetCommentResponse 로 변환
public static GetCommentResponse toDto(Comment comment, YesNo isEdited) {
return new GetCommentResponse(comment, isEdited);
}

// Comment 를 GetCommentResponse 로 변환
public static GetCommentResponse toDto(Comment comment) {
return new GetCommentResponse(comment);
private static LocalDateTime getCreatedAt(Comment comment) {
if(comment.getModifierIp() != null) {
return comment.getUpdatedAt();
}
return comment.getCreatedAt();
}

private static Long getParentCommentId(Comment parentCommentId) {
if(parentCommentId != null) {
return parentCommentId.getId();
private static Long getParentCommentId(Comment parentComment) {
if(parentComment != null) {
return parentComment.getId();
}
return null;
}
Expand Down
Loading

0 comments on commit e624f57

Please sign in to comment.