22
33import lombok .RequiredArgsConstructor ;
44import lombok .extern .slf4j .Slf4j ;
5+ import org .dfbf .soundlink .domain .chat .entity .ChatRoom ;
6+ import org .dfbf .soundlink .domain .chat .repository .ChatRoomRepository ;
57import org .dfbf .soundlink .domain .emotionRecord .dto .request .EmotionRecordRequestDTO ;
68import org .dfbf .soundlink .domain .emotionRecord .dto .request .EmotionRecordUpdateRequestDTO ;
79import org .dfbf .soundlink .domain .emotionRecord .dto .response .*;
1416import org .dfbf .soundlink .domain .emotionRecord .repository .SpotifyMusicRepository ;
1517import org .dfbf .soundlink .domain .user .entity .User ;
1618import org .dfbf .soundlink .domain .user .repository .UserRepository ;
17- import org .dfbf .soundlink .global .comm .enums .Emotions ;
1819import org .dfbf .soundlink .global .exception .ErrorCode ;
1920import org .dfbf .soundlink .global .exception .ResponseResult ;
2021import org .springframework .dao .DataAccessException ;
@@ -36,6 +37,9 @@ public class EmotionRecordService {
3637 private final EmotionRecordRepository emotionRecordRepository ;
3738 private final UserRepository userRepository ;
3839
40+ private final EmotionRecordCacheService emotionRecordCacheService ;
41+ private final ChatRoomRepository chatRoomRepository ;
42+
3943 @ Transactional
4044 public ResponseResult saveEmotionRecordWithMusic (Long userId , EmotionRecordRequestDTO request ) {
4145
@@ -63,6 +67,13 @@ public ResponseResult saveEmotionRecordWithMusic(Long userId, EmotionRecordReque
6367 .build ();
6468 emotionRecordRepository .save (emotionRecord );
6569
70+ // 게시글 생성 시, 해당 조건에 맞는 캐시 키 삭제
71+ emotionRecordCacheService .evictEmotionRecordCache (
72+ userId ,
73+ request .spotifyId (),
74+ request .emotion ().name ()
75+ );
76+
6677 return new ResponseResult (ErrorCode .SUCCESS );
6778 } catch (UserNotFoundException e ) {
6879 return new ResponseResult (ErrorCode .FAIL_TO_FIND_USER , e .getMessage ());
@@ -99,33 +110,22 @@ public ResponseResult getEmotionRecordsByLoginId(String userTag, int page, int s
99110 }
100111 }
101112
113+ // 동적 검색 (로그인 사용자를 제외한 EmotionRecord 조회) - 캐시 적용
114+ @ Transactional (readOnly = true )
102115 public ResponseResult getEmotionRecordsExcludingUserIdByFilters (Long userId , List <String > emotionList , String spotifyId , int page , int size ) {
103- List <Emotions > emotionEnums = null ;
104116 ResponseResult pageValidationResult = validateAndCreatePageable (page , size );
105117 if (pageValidationResult .getCode () != 200 /*SUCCESS*/ ) {
106118 return pageValidationResult ;
107119 }
108120
109- Pageable pageable = (Pageable ) pageValidationResult .getData ();
110-
111- if (emotionList != null && !emotionList .isEmpty ()) {
112- try {
113- emotionEnums = emotionList .stream ()
114- .map (e -> Emotions .valueOf (e .toUpperCase ()))
115- .toList ();
116- } catch (IllegalArgumentException e ) {
117- return new ResponseResult (ErrorCode .FAIL_TO_FIND_EMOTION , "잘못된 감정 값이 포함되어 있습니다." );
118- }
119- }
120-
121121 try {
122- Page < EmotionRecord > recordsPage = emotionRecordRepository . findByFilters ( userId , emotionEnums , spotifyId , pageable );
123- List <EmotionRecordResponseMainDTO > dtoList = recordsPage . getContent ()
124- . stream ()
125- . map ( EmotionRecordResponseMainDTO :: fromEntity )
126- . toList ();
127-
128- return new ResponseResult (ErrorCode .SUCCESS , EmotionRecordPageResponseDTO . fromPage ( recordsPage , dtoList ));
122+ // EmotionRecordCacheService의 결합 캐시 조회 및 Fallback 처리
123+ EmotionRecordPageResponseDTO <EmotionRecordResponseMainDTO > result =
124+ emotionRecordCacheService . getEmotionRecords ( userId , emotionList , spotifyId , page , size );
125+ return new ResponseResult ( ErrorCode . SUCCESS , result );
126+ } catch ( IllegalArgumentException e ) {
127+ // 잘못된 감정 값이 포함된 경우
128+ return new ResponseResult (ErrorCode .FAIL_TO_FIND_EMOTION , e . getMessage ( ));
129129 } catch (DataAccessException e ) {
130130 return new ResponseResult (ErrorCode .DB_ERROR , e .getMessage ());
131131 } catch (Exception e ) {
@@ -172,7 +172,7 @@ public ResponseResult getVideoIdBySpotifyId(String spotifyId) {
172172 public ResponseResult updateEmotionRecord (Long recordId , EmotionRecordUpdateRequestDTO updateDTO ) {
173173 try {
174174 // 기존 감정 기록 조회
175- EmotionRecord record = emotionRecordRepository .findByRecordId (recordId )
175+ EmotionRecord emotionRecord = emotionRecordRepository .findByRecordId (recordId )
176176 .orElseThrow (EmotionRecordNotFoundException ::new );
177177
178178 // SpotifyMusic이 DB에 있는지 먼저 확인
@@ -190,11 +190,17 @@ public ResponseResult updateEmotionRecord(Long recordId, EmotionRecordUpdateRequ
190190 return spotifyMusicRepository .save (newMusic );
191191 });
192192
193- record .updateEmotionRecord (updateDTO .emotion (), updateDTO .comment (), spotifyMusic );
193+ emotionRecord .updateEmotionRecord (updateDTO .emotion (), updateDTO .comment (), spotifyMusic );
194194
195195 // 수정된 정보를 Response DTO로 변환
196- EmotionRecordUpdateResponseDTO responseDTO = EmotionRecordUpdateResponseDTO .fromEntity (record );
197-
196+ EmotionRecordUpdateResponseDTO responseDTO = EmotionRecordUpdateResponseDTO .fromEntity (emotionRecord );
197+
198+ // 게시글 수정 시, 해당 조건에 맞는 캐시 키 삭제
199+ emotionRecordCacheService .evictEmotionRecordCache (
200+ emotionRecord .getUser ().getUserId (),
201+ updateDTO .spotifyId (),
202+ updateDTO .emotion ()
203+ );
198204 return new ResponseResult (ErrorCode .SUCCESS , responseDTO );
199205 } catch (EmotionRecordNotFoundException e ) {
200206 return new ResponseResult (ErrorCode .FAIL_TO_FIND_EMOTION_RECORD , e .getMessage ());
@@ -208,7 +214,27 @@ public ResponseResult updateEmotionRecord(Long recordId, EmotionRecordUpdateRequ
208214 @ Transactional
209215 public ResponseResult deleteEmotionRecord (Long recordId ) {
210216 try {
217+ EmotionRecord emotionRecord = emotionRecordRepository .findByRecordId (recordId )
218+ .orElseThrow (EmotionRecordNotFoundException ::new );
219+
220+ // 해당 EmotionRecord를 참조하는 채팅방 조회
221+ List <ChatRoom > chatRooms = chatRoomRepository .findByRecordId (emotionRecord );
222+
223+ // 채팅방이 존재하면 모두 삭제 후 삭제 내용을 즉시 반영
224+ if (chatRooms != null && !chatRooms .isEmpty ()) {
225+ chatRoomRepository .deleteAll (chatRooms );
226+ chatRoomRepository .flush (); // 즉시 DB에 반영
227+ }
228+
211229 int deletedCount = emotionRecordRepository .deleteByRecordId (recordId );
230+ emotionRecordRepository .flush (); // 즉시 DB에 반영
231+
232+ // 게시글 삭제 시, 해당 조건에 맞는 캐시 키 삭제
233+ emotionRecordCacheService .evictEmotionRecordCache (
234+ emotionRecord .getUser ().getUserId (),
235+ emotionRecord .getSpotifyMusic ().getSpotifyId (),
236+ emotionRecord .getEmotion ().name ()
237+ );
212238
213239 // 삭제할 데이터가 없는 경우
214240 if (deletedCount == 0 ) {
0 commit comments