berryberrybin / board-project

0 stars 0 forks source link

[spring] 회원의 role 구분 - 하위 테이블 구분 컬럼(dtype) 가져오기 #25

Open berryberrybin opened 1 year ago

berryberrybin commented 1 year ago

```java
@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@DiscriminatorValue("Owner")
public class Owner extends User{

    @Column
    private String ownerName;

    @OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Shop> shopList = new ArrayList<>();

    @Builder
    public Owner(final String nickname, String ownerName) {
        super(nickname);
        this.ownerName = ownerName;
    }

}
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
public class CommentInfo {

    private UUID commentId;
    private UUID authorId;
    private String authorNickname;
    private String authorRole; // 추가한 코드 
    private String content;
    private LocalDateTime createdAt;
    private int commentLikeCount;
    private Boolean viewerHasLiked = false;
    private Boolean deleted;
    private UUID parentCommentId;
    private List<CommentInfo> childComments;

}
@Mapper
public interface CommentInfoMapper {

    CommentInfoMapper INSTANCE = Mappers.getMapper(CommentInfoMapper.class);

    @Mapping(target = "commentId", source = "comment.id")
    @Mapping(target = "authorId", source = "comment.author.id")
    @Mapping(target = "authorNickname", source = "comment.author.nickname") 
    @Mapping(target = "authorRole", source = "comment.author.dtype") // 추가한 코드 
    @Mapping(target = "parentCommentId", source = "comment.parentComment.id")
    CommentInfo of(Comment comment);
}
berryberrybin commented 1 year ago
public Slice<CommentInfo> RetrieveCommentListByFeedId(CommentQuery.RetrieveCommentListByFeedId query) {

        UUID loginUserId = query.getLoginUserId();
        findUserById(loginUserId);

        UUID feedId = query.getFeedId();
        Feed feed = findFeedById(feedId);

        Slice<CommentInfo> commentInfos = commentRepository.findAllByFeedIdAndParentCommentIsNull(feedId, query.getPageable())
            .map(comment -> CommentInfoMapper.INSTANCE.of(comment))
            .map(commentInfo -> putCommentInfoExtra(commentInfo, loginUserId));

        return commentInfos;
    }

private CommentInfo putCommentInfoExtra(CommentInfo commentInfo, UUID loginUserId){
        commentInfo.setViewerHasLiked(commentLikeRepository.existsByUserIdAndCommentId(loginUserId, commentInfo.getCommentId()));

        userRepository.getReferenceById(commentInfo.getCommentId());
                 // 추가한 코드 
        if(findUserById(commentInfo.getAuthorId()).getClass().getSimpleName().equals("Owner")){
            commentInfo.setAuthorIsOwner(true);
        }

        if(commentInfo.getChildComments() != null ){
            for(CommentInfo childCommentInfo : commentInfo.getChildComments()){
                putCommentInfoExtra(childCommentInfo, loginUserId);
            }
        }
        return commentInfo;
    }
private User findUserById(UUID userId) {
        return userRepository.findById(userId)
            .orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND, "user.byId", List.of(userId.toString())));
    }