Skip to content

Commit cd52e3a

Browse files
committed
[fix]: 1:1 → 1:N 관계 변경 및 중복 채팅방 방지 로직 추가 #44
1 parent ab9ee58 commit cd52e3a

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

src/main/java/org/dfbf/soundlink/domain/chat/entity/ChatRoom.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ChatRoom {
3232
@JoinColumn (name = "request_user_id")
3333
private User requestUserId;
3434

35-
@OneToOne
35+
@ManyToOne
3636
@JoinColumn(name = "record_id")
3737
private EmotionRecord recordId;
3838

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.dfbf.soundlink.domain.chat.repository;
22

33
import org.dfbf.soundlink.domain.chat.entity.ChatRoom;
4+
import org.dfbf.soundlink.domain.emotionRecord.entity.EmotionRecord;
5+
import org.dfbf.soundlink.domain.user.entity.User;
46
import org.springframework.data.jpa.repository.JpaRepository;
57

68
public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {
9+
boolean existsByRequestUserIdAndRecordId(User requestUserId, EmotionRecord recordId);
710
}

src/main/java/org/dfbf/soundlink/domain/chat/service/ChatRoomService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public ResponseResult createChatRoom(@AuthenticationPrincipal Long userId, Long
4343
//감정기록 조회
4444
EmotionRecord emotionRecord = emotionRecordRepository.findById(recordId)
4545
.orElseThrow(EmotionRecordNotFoundException::new);
46+
//이미 존재하는 채팅방인지 확인
47+
if(chatRoomRepository.existsByRequestUserIdAndRecordId(requestUserId,emotionRecord)){
48+
return new ResponseResult(ErrorCode.CHAT_FAILED, "이미 존재하는 채팅방입니다.");
49+
}
4650

4751
Long responseUserId = emotionRecord.getUser().getUserId();
4852

@@ -62,9 +66,6 @@ public ResponseResult createChatRoom(@AuthenticationPrincipal Long userId, Long
6266
redisTemplate.opsForValue().set("Room::"+chatRoom.getChatRoomId(), String.valueOf(chatReqDto));
6367

6468
return new ResponseResult(ErrorCode.SUCCESS, chatRoom);
65-
}
66-
catch (DataIntegrityViolationException e) {
67-
return new ResponseResult(ErrorCode.CHAT_FAILED, "채팅방 생성 실패: 이미 존재하는 데이터입니다."); // recordId 값 중복 시
6869
} catch (Exception e) {
6970
return new ResponseResult(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage());
7071
}

0 commit comments

Comments
 (0)