Skip to content

Commit

Permalink
feat: Comment 작성 자격 검증 (#5)
Browse files Browse the repository at this point in the history
CommentCreateRequest에서 PostTier를 받아서, 현재 사용자의 티어로 자격 검증하는 메서드 추가 postService.validateAuthority 메서드 활용
  • Loading branch information
toychip committed Dec 24, 2023
1 parent 28af44c commit a7163e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.api.TaveShot.domain.Comment.dto.request;

import com.api.TaveShot.domain.post.post.domain.PostTier;
import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -8,6 +10,16 @@
@Builder
@AllArgsConstructor
public class CommentCreateRequest {

private String comment;

private Long parentCommentId;

@NotEmpty
private String postTier;

public PostTier getPostTier() {
// 문자열을 enum으로 변환
return PostTier.findTier(postTier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.api.TaveShot.domain.Comment.dto.response.CommentResponse;
import com.api.TaveShot.domain.Comment.repository.CommentRepository;
import com.api.TaveShot.domain.Member.domain.Member;
import com.api.TaveShot.domain.Member.domain.Tier;
import com.api.TaveShot.domain.post.post.domain.Post;
import com.api.TaveShot.domain.post.post.repository.PostRepository;
import com.api.TaveShot.domain.post.post.service.PostService;
import com.api.TaveShot.global.exception.ApiException;
import com.api.TaveShot.global.exception.ErrorType;
import com.api.TaveShot.global.util.SecurityUtil;
Expand All @@ -27,13 +29,15 @@
public class CommentService {

private final CommentRepository commentRepository;
private final PostRepository postRepository;
private final PostService postService;

@Transactional // 데이터 변경하는 메서드에만 명시적으로 적용
public Long register(final Long postId, final CommentCreateRequest request) {
Member currentMember = getCurrentMember();

// ToDo 어떤 티어 게시판인지 검증
validateAuthority(request, currentMember);

// 어떤 게시판인지 Post에서 검증하는 것이 아닌 request에서 받고, member의 티어와 비교하면 됨 validateAuthority 참고
Post post = getPost(postId);

// ---------------- 부모 댓글 유무 확인 ----------------
Expand All @@ -49,13 +53,16 @@ public Long register(final Long postId, final CommentCreateRequest request) {
return createNotParent(request, currentMember, post);
}

private void validateAuthority(CommentCreateRequest request, Member currentMember) {
postService.validateAuthority(request.getPostTier(), currentMember);
}

private Member getCurrentMember() {
return SecurityUtil.getCurrentMember();
}

private Post getPost(final Long postId) {
return postRepository.findById(postId)
.orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND));
return postService.findById(postId);
}

private Optional<Comment> findParentComment(final Long parentCommentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public PostResponse register(final PostCreateRequest request) {
return postResponse(post);
}

private void validateAuthority(final PostTier postTier, final Member currentMember) {
public void validateAuthority(final PostTier postTier, final Member currentMember) {
Tier memberTier = currentMember.getTier();

boolean isContains = postTier.containsTier(memberTier);
Expand Down Expand Up @@ -169,4 +169,9 @@ public void delete(final Long postId) {
postRepository.delete(post);
}

public Post findById(Long postId) {
return postRepository.findById(postId)
.orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND));
}

}

0 comments on commit a7163e8

Please sign in to comment.