33import jakarta .mail .MessagingException ;
44import jakarta .servlet .http .HttpServletResponse ;
55import jakarta .transaction .Transactional ;
6- import lombok .AllArgsConstructor ;
6+ import lombok .RequiredArgsConstructor ;
77import lombok .extern .slf4j .Slf4j ;
88import org .dfbf .soundlink .domain .emotionRecord .entity .SpotifyMusic ;
99import org .dfbf .soundlink .domain .emotionRecord .repository .EmotionRecordRepository ;
3333
3434@ Slf4j
3535@ Service
36- @ AllArgsConstructor
36+ @ RequiredArgsConstructor
3737public class UserService {
3838
3939 private final UserRepository userRepository ;
@@ -45,8 +45,19 @@ public class UserService {
4545 private final RedisService redisService ;
4646 private final JwtProvider jwtProvider ;
4747 private final TokenProperties tokenProperties ;
48+
4849 private RedisTemplate <String , String > redisTemplate ;
4950
51+ //refreshToken을 쿠키로 설정
52+ private ResponseCookie getRefreshToken (String refreshToken ) {
53+ return ResponseCookie
54+ .from ("REFRESHTOKEN" , refreshToken )
55+ .domain ("localhost" )
56+ .path ("/" )
57+ .httpOnly (true )
58+ .maxAge (tokenProperties .getRefreshTokenExpirationTime ()) //만료시간 설정
59+ .build ();
60+ }
5061
5162 // 회원가입
5263 public ResponseResult signUp (UserSignUpDto userSignUpDto ) {
@@ -72,26 +83,31 @@ public ResponseResult getUser(Long userId) {
7283 }
7384
7485 // 회원정보 수정
75- @ Transactional
7686 public ResponseResult updateUser (Long userId , UserUpdateDto userUpdateDto ) {
7787 try {
78- User user = userRepository .findById (userId ).orElseThrow (() -> new NoUserDataException () );
88+ User user = userRepository .findById (userId ).orElseThrow (NoUserDataException :: new );
7989 user .update (userUpdateDto , passwordEncoder );
8090
81- // SpotifyMusic 객체 찾고 없으면 새로 생성
91+ // SpotifyMusic 객체 찾기 ( 없으면 새로 생성 & 저장)
8292 SpotifyMusic spotifyMusic = spotifyMusicRepository .findById (userUpdateDto .spotifyId ())
83- .orElse (new SpotifyMusic (userUpdateDto .spotifyId (), userUpdateDto .title (), userUpdateDto .artist (), userUpdateDto .albumImage ()));
84-
85- // SpotifyMusic 저장
86- spotifyMusicRepository .save (spotifyMusic );
93+ .orElseGet (() -> {
94+ SpotifyMusic sm = new SpotifyMusic (userUpdateDto );
95+ spotifyMusicRepository .save (sm );
96+ return sm ;
97+ });
8798
88- // ProfileMusic 객체 찾고 없으면 새로 생성
99+ // ProfileMusic 객체 찾기 ( 없으면 새로 생성 & 저장)
89100 ProfileMusic profileMusic = profileMusicRepository .findByUserId (userId )
90- .orElse (new ProfileMusic (user , spotifyMusic ));
101+ .map (pm -> {
102+ pm .update (spotifyMusic );
103+ return pm ;
104+ })
105+ .orElseGet (() -> profileMusicRepository .save (new ProfileMusic (user , spotifyMusic )));
91106
92- // ProfileMusic이 업데이트
93- profileMusic .update (spotifyMusic );
94- profileMusicRepository .save (profileMusic );
107+ /**
108+ * orElse -> 일단 함수는 실행, 그러나 값이 null이면 orElse의 값으로 대체 (함수O, 람다x)
109+ * orElseGet -> null일때만 실행 (함수O, 람다O)
110+ */
95111
96112 return new ResponseResult (ErrorCode .SUCCESS );
97113 } catch (NoUserDataException e ) {
@@ -104,6 +120,9 @@ public ResponseResult updateUser(Long userId, UserUpdateDto userUpdateDto) {
104120 public ResponseResult deleteUser (Long userId ) {
105121 try {
106122 User user = userRepository .findById (userId ).orElseThrow (() -> new NoUserDataException ());
123+
124+ profileMusicRepository .deleteByUser (user ); // 유저 프로필 음악 삭제
125+ emotionRecordRepository .deleteByUser (user ); // 유저 감정 기록 삭제
107126 userRepository .deleteById (userId ); // 유저 삭제
108127
109128 return new ResponseResult (ErrorCode .SUCCESS );
@@ -169,24 +188,13 @@ public ResponseResult checkEmail(String email){
169188
170189 //닉네임 중복 확인
171190 public ResponseResult checkNickName (String nickName ){
172- boolean exists =userRepository .existsByNickname (nickName );
191+ boolean exists = userRepository .existsByNickname (nickName );
173192 if (exists ){
174193 return new ResponseResult (ErrorCode .DUPLICATE_NICKNAME );
175194 }
176195 return new ResponseResult (ErrorCode .NOT_DUPLICATE_NICKNAME );
177196 }
178197
179- //refreshToken을 쿠키로 설정
180- private ResponseCookie getRefreshToken (String refreshToken ) {
181- return ResponseCookie
182- .from ("REFRESHTOKEN" , refreshToken )
183- .domain ("localhost" )
184- .path ("/" )
185- .httpOnly (true )
186- .maxAge (tokenProperties .getRefreshTokenExpirationTime ()) //만료시간 설정
187- .build ();
188- }
189-
190198 //로그인
191199 public ResponseResult login (LoginReqDto loginReqDto , HttpServletResponse response ) {
192200 try {
@@ -238,7 +246,4 @@ public ResponseResult logout(HttpServletResponse response) {
238246 return new ResponseResult (ErrorCode . INTERNAL_SERVER_ERROR ,"로그아웃 중 오류가 발생했습니다." );
239247 }
240248 }
241-
242-
243-
244249}
0 commit comments