diff --git a/src/main/java/com/diareat/diareat/user/repository/UserRepository.java b/src/main/java/com/diareat/diareat/user/repository/UserRepository.java index 2c45f0a..743ec3a 100644 --- a/src/main/java/com/diareat/diareat/user/repository/UserRepository.java +++ b/src/main/java/com/diareat/diareat/user/repository/UserRepository.java @@ -7,4 +7,5 @@ public interface UserRepository extends JpaRepository { List findAllByNameContaining(String name); // 회원이 팔로우를 위해 검색한 유저 목록 조회 + boolean existsByName(String name); // 회원가입 시 닉네임 중복 확인 } diff --git a/src/main/java/com/diareat/diareat/user/service/UserService.java b/src/main/java/com/diareat/diareat/user/service/UserService.java index 2ce3bee..7d688bd 100644 --- a/src/main/java/com/diareat/diareat/user/service/UserService.java +++ b/src/main/java/com/diareat/diareat/user/service/UserService.java @@ -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(); } @@ -89,6 +91,9 @@ public List 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)); } @@ -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); }