diff --git a/src/main/java/com/helpmeCookies/global/config/RedisConfig.java b/src/main/java/com/helpmeCookies/global/config/RedisConfig.java index 662c51f..b2f1c2d 100644 --- a/src/main/java/com/helpmeCookies/global/config/RedisConfig.java +++ b/src/main/java/com/helpmeCookies/global/config/RedisConfig.java @@ -34,7 +34,4 @@ public RedisConnectionFactory redisConnectionFactory() { redisTemplate.setConnectionFactory(redisConnectionFactory()); return redisTemplate; } - - - } diff --git a/src/main/java/com/helpmeCookies/user/entity/User.java b/src/main/java/com/helpmeCookies/user/entity/User.java index 2252d44..2fb24eb 100644 --- a/src/main/java/com/helpmeCookies/user/entity/User.java +++ b/src/main/java/com/helpmeCookies/user/entity/User.java @@ -42,6 +42,7 @@ public class User extends BaseTimeEntity { @Column(nullable = false) private Long id; + @Column(nullable = false, unique = true) private String nickname; private String userImageUrl; diff --git a/src/main/java/com/helpmeCookies/user/repository/UserRepository.java b/src/main/java/com/helpmeCookies/user/repository/UserRepository.java index 8e01f68..f4988f0 100644 --- a/src/main/java/com/helpmeCookies/user/repository/UserRepository.java +++ b/src/main/java/com/helpmeCookies/user/repository/UserRepository.java @@ -12,5 +12,6 @@ public interface UserRepository extends JpaRepository, UserCustomRepository { Optional findById(Long id); Optional findByUserInfoEmail(String email); - boolean existsByNicknameAndIdNot(String nickname, Long userId); + Optional findByNickname(String nickname); + Optional findByUserInfoPhone(String phone); } diff --git a/src/main/java/com/helpmeCookies/user/service/UserService.java b/src/main/java/com/helpmeCookies/user/service/UserService.java index 1c0bc12..f219609 100644 --- a/src/main/java/com/helpmeCookies/user/service/UserService.java +++ b/src/main/java/com/helpmeCookies/user/service/UserService.java @@ -1,21 +1,17 @@ package com.helpmeCookies.user.service; -import java.io.IOException; -import java.util.List; - import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.multipart.MultipartFile; +import com.helpmeCookies.global.exception.user.DuplicateResourceException; import com.helpmeCookies.global.exception.user.ResourceNotFoundException; import com.helpmeCookies.global.utils.AwsS3FileUtils; import com.helpmeCookies.user.dto.UserCommonInfoDto; import com.helpmeCookies.user.dto.UserDto; import com.helpmeCookies.user.dto.UserInfoDto; import com.helpmeCookies.user.dto.UserTypeDto; -import com.helpmeCookies.user.dto.request.UserReq; import com.helpmeCookies.user.dto.response.UserFollowingRes; import com.helpmeCookies.user.entity.ArtistInfo; import com.helpmeCookies.user.entity.Social; @@ -24,7 +20,6 @@ import com.helpmeCookies.user.repository.ArtistInfoRepository; import com.helpmeCookies.user.repository.SocialRepository; import com.helpmeCookies.user.repository.UserRepository; -import com.sun.jdi.request.DuplicateRequestException; import lombok.RequiredArgsConstructor; @@ -39,8 +34,7 @@ public class UserService { @Transactional public UserDto getUserInfo(Long userId) { - User user = userRepository.findById(userId) - .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다.")); + User user = checkUser(userId); return UserDto.fromEntity(user); } @@ -48,16 +42,17 @@ public UserDto getUserInfo(Long userId) { @Transactional public UserDto updateUser(UserCommonInfoDto userCommonInfoDto, UserInfoDto userInfoDto, Long userId) { - User existingUser = userRepository.findById(userId) - .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다.")); + User existingUser = checkUser(userId); - if (userRepository.existsByNicknameAndIdNot(userCommonInfoDto.nickname(), existingUser.getId())) { - throw new DuplicateRequestException("이미 존재하는 닉네임입니다."); - } - - existingUser.updateUserCommonInfo(userCommonInfoDto.nickname(), userCommonInfoDto.ImageUrl()); + userRepository.findByNickname(userCommonInfoDto.nickname()) + .ifPresent(user -> { + if(!user.getId().equals(userId)) { + throw new DuplicateResourceException("이미 사용중인 닉네임입니다."); + } + }); UserInfo userInfo = userInfoDto.toEntity(); + existingUser.updateUserCommonInfo(userCommonInfoDto.nickname(), userCommonInfoDto.ImageUrl()); existingUser.updateUserInfo(userInfo); return UserDto.fromEntity(userRepository.save(existingUser)); @@ -70,7 +65,6 @@ public UserTypeDto getUserType(Long userId) { .map(artistInfo -> artistInfo.getArtistType().getType()) // 값이 있을 때 처리 .orElse("User"); // 값이 없을 때 기본값 - //artistType을 확인 return UserTypeDto.builder() .userType(usertype) .build(); @@ -78,21 +72,17 @@ public UserTypeDto getUserType(Long userId) { @Transactional public Page getFollowingWithPaging(Long userId, Pageable pageable) { - return userRepository.findFollowingUsers(userId, pageable) .map(UserFollowingRes::fromDto); } @Transactional public void followArtist(Long userId, Long artistId) { - User user = userRepository.findById(userId) - .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다.")); + User user = checkUser(userId); + ArtistInfo artistInfo = getArtistInfo(artistId); - ArtistInfo artistInfo = artistInfoRepository.findByUserId(artistId) - .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다.")); - - if (socialRepository.existsByFollowerAndFollowing(user, artistInfo)) { - throw new DuplicateRequestException("이미 팔로우한 아티스트입니다."); + if(isFollowing(user, artistInfo)) { + throw new DuplicateResourceException("이미 팔로우한 아티스트입니다."); } Social social = Social.builder() @@ -105,15 +95,37 @@ public void followArtist(Long userId, Long artistId) { @Transactional public void unfollowArtist(Long userId, Long artistId) { - User user = userRepository.findById(userId) - .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다.")); + User user = checkUser(userId); + ArtistInfo artistInfo = getArtistInfo(artistId); - ArtistInfo artistInfo = artistInfoRepository.findByUserId(artistId) - .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다.")); + if(isFollowing(user, artistInfo)) { + Social social = getSocial(user, artistInfo); + socialRepository.delete(social); + } + } + + @Transactional + public boolean checkSignup(String email) { + return userRepository.findByUserInfoPhone(email).isPresent(); + } + + private User checkUser(Long userId) { + return userRepository.findById(userId) + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다.")); + } - Social social = socialRepository.findByFollowerAndFollowing(user, artistInfo) + private Social getSocial(User user, ArtistInfo artistInfo) { + return socialRepository.findByFollowerAndFollowing(user, artistInfo) .orElseThrow(() -> new ResourceNotFoundException("팔로우하지 않은 아티스트입니다.")); + } - socialRepository.delete(social); + private boolean isFollowing(User user, ArtistInfo artistInfo) { + return socialRepository.existsByFollowerAndFollowing(user, artistInfo); + } + + private ArtistInfo getArtistInfo(Long artistId) { + ArtistInfo artistInfo = artistInfoRepository.findByUserId(artistId) + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다.")); + return artistInfo; } }