Skip to content

Commit

Permalink
🦺 Feat: UserService 일부 예외처리 로직 추가 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Oct 13, 2023
1 parent 831f20f commit 925c4dc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

public interface UserRepository extends JpaRepository<User, Long> {
List<User> findAllByNameContaining(String name); // 회원이 팔로우를 위해 검색한 유저 목록 조회
boolean existsByName(String name); // 회원가입 시 닉네임 중복 확인
}
10 changes: 9 additions & 1 deletion src/main/java/com/diareat/diareat/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class UserService {
public Long saveUser(CreateUserDto createUserDto) {
BaseNutrition baseNutrition = BaseNutrition.createNutrition(2000, 300, 80, 80);
// BaseNutrition baseNutrition = BaseNutrition.createNutrition(createUserDto.getGender(), createUserDto.getAge(), createUserDto.getHeight(), createUserDto.getWeight());
if (userRepository.existsByName(createUserDto.getName()))
throw new UserException(ResponseCode.USER_ALREADY_EXIST);
User user = User.createUser(createUserDto.getName(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition);
return userRepository.save(user).getId();
}
Expand Down Expand Up @@ -89,6 +91,9 @@ public List<ResponseSearchUserDto> searchUser(Long hostId, String name) {
public void followUser(Long userId, Long followId) {
validateUser(userId);
validateUser(followId);
// 이미 팔로우 중인 경우
if (followRepository.existsByFromUserAndToUser(userId, followId))
throw new UserException(ResponseCode.FOLLOWED_ALREADY);
followRepository.save(Follow.makeFollow(userId, followId));
}

Expand All @@ -97,11 +102,14 @@ public void followUser(Long userId, Long followId) {
public void unfollowUser(Long userId, Long unfollowId) {
validateUser(userId);
validateUser(unfollowId);
// 이미 팔로우 취소한 경우
if (followRepository.existsByFromUserAndToUser(userId, unfollowId))
throw new UserException(ResponseCode.UNFOLLOWED_ALREADY);
followRepository.delete(Follow.makeFollow(userId, unfollowId));
}

private void validateUser(Long userId) {
if(!userRepository.existsById(userId))
if (!userRepository.existsById(userId))
throw new UserException(ResponseCode.USER_NOT_FOUND);
}

Expand Down

0 comments on commit 925c4dc

Please sign in to comment.