Spring 심화 pjt(2)

2023. 1. 6. 00:09스파르타 내일배움캠프/Spring 강의 정리

추가 기능 구현

  • 대댓글 기능을 만들어 보세요!
    • 대댓글 작성하기
    • 댓글 조회 시 대댓글도 함께 조회하기

+ 좋아요 기능

+ 수정 기능

+ 삭제 기능

 

 

위 부분 중 댓글 조회 부분을 제외하고 로컬 환경에서 동작 확인하였습니다. 

 

public class RecommentService {
    private final UserRepository userRepository;
    private final CommentRepository commentRepository;
    private final RecommentRepository recommentRepository;
    private final JwtUtil jwtUtil;
    
    ...
    
     User user = userRepository.findByUsername(username).orElseThrow(
                () -> new CustomException(MEMBER_NOT_FOUND)
        );

        Comment comment = commentRepository.findCommentById(requestDto.getCommentId()).orElseThrow(
                () -> new CustomException(RESOURCE_NOT_FOUND)
        );
    
    ...

또 초기에 다른 entitiy repository에 직접 접근하고 있었습니다.

 

이 부분을 이렇게 바꾸고

public class RecommentService {
    private final UserService userService;
    private final CommentService commentService;
    private final RecommentRepository recommentRepository;
    private final JwtUtil jwtUtil;
    
    ...
    
    User user = userService.findUser(username);

        Comment comment = commentService.findComment(requestDto.getCommentId());
        
    ...

 

 

public class UserService {

...

    public User findUser(String username) {
            Optional<User> optionalAuthor = userRepository.findByUsername(username);
            if (optionalAuthor.isEmpty()) {
                throw new CustomException(MEMBER_NOT_FOUND);
            }

            return optionalAuthor.get();
        }

...
}
public class CommentService {

...

    public Comment findComment(Long id) {
        Optional<Comment> optionalComment = commentRepository.findById(id);
        if (optionalComment.isEmpty()) {
            throw new CustomException(COMMENT_NOT_FOUND);
        }

        Comment comment = optionalComment.get();
        if (!comment.isAvailable()) {
            throw new CustomException(COMMENT_IS_DELETED);
        }

        return comment;
    }
    
...
}

이렇게 해당 엔티티 서비스에서 얻어오게 했습니다.

 

'스파르타 내일배움캠프 > Spring 강의 정리' 카테고리의 다른 글

No serializer found for class 오류  (0) 2023.01.05
Spring 심화 pjt  (0) 2023.01.03
Spring Security  (0) 2022.12.26
Spring 숙련 과제(최종)  (0) 2022.12.21
Spring 숙련 과제(2)  (0) 2022.12.20