Skip to content

Commit

Permalink
refactor: #13 메소드 추출 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
ehs208 committed Oct 29, 2024
1 parent bede5f1 commit 8df1e9f
Showing 1 changed file with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,43 @@ public class FollowService {
// 팔로우
public void followUser(FollowRequestDto requestDto, HttpServletRequest request) {
User user = getUser(request);
User targetUser = userRepository.findByCustomId(requestDto.getCustomId())
.orElseThrow(UserNotFoundException::new);
User targetUser = getTargetUser(requestDto);

boolean isFollowing = followRepository.findByFollowerAndFollowing(user, targetUser).isPresent();

if (isFollowing) {
if (isFollowing(user, targetUser)) {
throw new FollowOperationError();
}

Follow follow = Follow.create(user, targetUser);
followRepository.save(follow);
}

// 이미 팔로우 중인지 확인
private boolean isFollowing(User user, User targetUser) {
return followRepository.findByFollowerAndFollowing(user, targetUser).isPresent();
}

// 언팔로우
public void unfollowUser(FollowRequestDto requestDto, HttpServletRequest request) {
User user = getUser(request);
User targetUser = userRepository.findByCustomId(requestDto.getCustomId())
.orElseThrow(UserNotFoundException::new);
User targetUser = getTargetUser(requestDto);

Follow follow = getFollowRelation(user, targetUser);

followRepository.delete(follow);
}

// 팔로우 관계 조회
private Follow getFollowRelation(User user, User targetUser) {
Follow follow = followRepository.findByFollowerAndFollowing(user, targetUser)
.orElseThrow(FollowOperationError::new);
return follow;
}

followRepository.delete(follow);
// 팔로우(언팔로우) 대상 유저 조회
private User getTargetUser(FollowRequestDto requestDto) {
User targetUser = userRepository.findByCustomId(requestDto.getCustomId())
.orElseThrow(UserNotFoundException::new);
return targetUser;
}

// 팔로잉 목록 조회
Expand Down

0 comments on commit 8df1e9f

Please sign in to comment.