Skip to content

Commit

Permalink
refactor:[kakao-tech-campus-2nd-step3#84]- refact User
Browse files Browse the repository at this point in the history
불필요한 공백 제거,중복되는 코드 변경, User Entity 설정 변경
  • Loading branch information
yooonwodyd committed Nov 12, 2024
1 parent 7c820ef commit d21fad1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,4 @@ public RedisConnectionFactory redisConnectionFactory() {
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}



}
1 change: 1 addition & 0 deletions src/main/java/com/helpmeCookies/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
public interface UserRepository extends JpaRepository<User, Long>, UserCustomRepository {
Optional<User> findById(Long id);
Optional<User> findByUserInfoEmail(String email);
boolean existsByNicknameAndIdNot(String nickname, Long userId);
Optional<User> findByNickname(String nickname);
Optional<User> findByUserInfoPhone(String phone);
}
72 changes: 42 additions & 30 deletions src/main/java/com/helpmeCookies/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -39,25 +34,25 @@ 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);
}

@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));
Expand All @@ -70,29 +65,24 @@ public UserTypeDto getUserType(Long userId) {
.map(artistInfo -> artistInfo.getArtistType().getType()) // 값이 있을 때 처리
.orElse("User"); // 값이 없을 때 기본값

//artistType을 확인
return UserTypeDto.builder()
.userType(usertype)
.build();
}

@Transactional
public Page<UserFollowingRes> 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()
Expand All @@ -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;
}
}

0 comments on commit d21fad1

Please sign in to comment.