Skip to content

Commit

Permalink
feat: comment status, converter 추가 #63
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangyoo committed Feb 20, 2023
1 parent f203347 commit c3c732a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import com.main36.pikcha.domain.member.entity.Member;
import com.main36.pikcha.domain.post.entity.Post;
import com.main36.pikcha.global.audit.Auditable;
import com.main36.pikcha.global.exception.BusinessLogicException;
import com.main36.pikcha.global.exception.ExceptionCode;
import com.main36.pikcha.global.utils.CommentStatusConverter;
import lombok.*;
import org.springframework.batch.item.validator.SpringValidator;

import javax.persistence.*;
import java.net.http.HttpClient;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Getter
Expand All @@ -29,6 +35,9 @@ public class Comment extends Auditable {
@JoinColumn(name = "member_id")
private Member member;

@Convert(converter = CommentStatusConverter.class)
private CommentStatus status = CommentStatus.Alive;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
Expand All @@ -47,4 +56,25 @@ public void updateParent(Comment parent){
public List<Comment> getChildren(){
return this.children;
}

@Getter
public enum CommentStatus {
Alive("생존", "1"),
Dead("사망", "2");

private String status;
private String code;

CommentStatus(String status, String code) {
this.status = status;
this.code = code;
}

public static CommentStatus ofCode(String code) {
return Arrays.stream(CommentStatus.values())
.filter(v-> v.getCode().equals(code))
.findAny()
.orElseThrow(()-> new BusinessLogicException(ExceptionCode.COMMENT_STATUS_INVALID));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ExceptionCode {
ATTRACTION_IMAGE_EXISTS(409, "Attraction image exists"),

COMMENT_NOT_FOUND(404, "Comment not found"),
COMMENT_STATUS_INVALID(404, "Comment status is inValid"),
COMMENT_EXISTS(409, "Comment exists"),

NOT_AUTHOR(403, "This Member didn't write the Comment"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.main36.pikcha.global.utils;

import com.main36.pikcha.domain.comment.entity.Comment;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class CommentStatusConverter implements AttributeConverter<Comment.CommentStatus, String> {
@Override
public String convertToDatabaseColumn(Comment.CommentStatus attribute) {
return attribute.getCode();
}

@Override
public Comment.CommentStatus convertToEntityAttribute(String dbData) {
return Comment.CommentStatus.ofCode(dbData);
}
}

0 comments on commit c3c732a

Please sign in to comment.