Skip to content

Commit

Permalink
feat : 3차 세미나 실습 과제_blog 주인 검증 메소드로 묶음 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
PicturePark1101 committed May 1, 2024
1 parent beda91d commit 65a4dcf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.example.demo.repository.BlogRepository;
import com.example.demo.service.dto.blog.BlogCreateRequest;
import com.example.demo.service.dto.blog.BlogTitleUpdateRequest;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -39,9 +40,15 @@ public void updateTitle(Long blogId, BlogTitleUpdateRequest blogTitleUpdateReque
// blogRepository.save(blog); 이렇게 해도 됨.
}

public void validateOwner(Long requestMemberId, Long findMemberId) {
public Blog validateOwner(Long blogId, Long requestMemberId) {

Blog blog = findById(blogId);
Long findMemberId = blog.getMember().getId();

if (!requestMemberId.equals(findMemberId)) {
throw new CustomValidateException(ErrorMessage.BLOG_UNAUTHORIZED);
}

return blog;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,23 @@
public class PostService {

private final PostRepository postRepository;
private final MemberService memberService;
private final BlogService blogService;

@Transactional
public String create(Long memberId, Long blogId, PostCreateRequest postCreateRequest)
{
// blog 찾기
Blog blog = blogService.findById(blogId);
Long findMemberId = blog.getMember().getId();
// 요청한 사람이 블로그 소유주인지 확인, 맞으면 블로그 객체 리턴
Blog findBlog = blogService.validateOwner(blogId, memberId);
Post post = postRepository.save(Post.create(findBlog, postCreateRequest));

// 요청한 사람이 블로그 소유주인지 확인
blogService.validateOwner(memberId, findMemberId);

Post post = postRepository.save(Post.create(blog, postCreateRequest));
// 블로그 글 생성하고 DB에 저장 후 생성된 id 반환
return post.getId().toString();
}

public PostListFindDto findAllPost(Long memberId, Long blogId){
// blog 찾기
Blog blog = blogService.findById(blogId);
Long findMemberId = blog.getMember().getId();

// 요청한 사람이 블로그 소유주인지 확인
blogService.validateOwner(memberId, findMemberId);

return PostListFindDto.of(postRepository.findByBlog(blog));
Blog findBlog = blogService.validateOwner(blogId, memberId);
return PostListFindDto.of(postRepository.findByBlog(findBlog));
}

public Post findById(Long postId) {
Expand All @@ -54,16 +44,9 @@ public Post findById(Long postId) {
);
}

// PostFindDto post = postService.findPost(memberId, blogId, postId);

public PostFindDto findPost(Long memberId, Long blogId, Long postId){
// blog 찾기
Blog blog = blogService.findById(blogId);
Long findMemberId = blog.getMember().getId();

// 요청한 사람이 블로그 소유주인지 확인
blogService.validateOwner(memberId, findMemberId);

blogService.validateOwner(blogId, memberId);
return PostFindDto.of(findById(postId));
}
}

0 comments on commit 65a4dcf

Please sign in to comment.