Skip to content

Commit

Permalink
feat: 채팅 요청시 가게정보 전송하도록 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
eckrin committed Nov 19, 2023
1 parent e1aeb82 commit ba2a8d3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown =true)
public class MessageRequestDto {
private Long receiverId; // 메세지 수신자
private String receiverName; // 수신자 이름 (채팅방명으로 쓰임)
// private Long receiverId; // 메세지 수신자
// private String receiverName; // 수신자 이름 (채팅방명으로 쓰임)
private Long storeId; // 1:1 채팅하기를 시작한 가게 정보
}
6 changes: 3 additions & 3 deletions src/main/java/com/kusitms/jipbap/chat/domain/dto/RoomDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public class RoomDto implements Serializable {
private RoomDto() {

}
public static RoomDto create(MessageRequestDto messageRequestDto, User user) {
public static RoomDto create(String receiverName, User user) {
RoomDto roomDto = new RoomDto();
roomDto.roomName = messageRequestDto.getReceiverName();
roomDto.roomName = receiverName;
roomDto.roomId = UUID.randomUUID().toString();
roomDto.senderName = user.getUsername();
roomDto.receiverName = messageRequestDto.getReceiverName();
roomDto.receiverName = receiverName;

return roomDto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ private void init() {
// 채팅방 생성
public MessageResponseDto createRoom(MessageRequestDto dto, String email) {
User sender = userRepository.findByEmail(email).orElseThrow(()->new InvalidEmailException("회원정보가 존재하지 않습니다."));
User receiver = userRepository.findById(dto.getReceiverId()).orElseThrow(()->new InvalidEmailException("수신 회원정보가 존재하지 않습니다."));
// User receiver = userRepository.findById(dto.getReceiverId()).orElseThrow(()->new InvalidEmailException("수신 회원정보가 존재하지 않습니다."));
Store store = storeRepository.findById(dto.getStoreId()).orElseThrow(()->new StoreNotExistsException("가게 정보를 찾을 수 없습니다."));
User receiver = store.getOwner();

// 4. 다른 사람들은 들어올 수 없도록 1:1 (구매자:판매자) 채팅방 구성하기
Room messageRoom = roomRepository.findBySenderNameAndReceiverName(sender.getUsername(), receiver.getUsername());
// 5. room 생성하기 - (이미 생성된 채팅방이 아닌 경우)
if (messageRoom == null) {
RoomDto roomDto = RoomDto.create(dto, sender);
RoomDto roomDto = RoomDto.create(receiver.getUsername(), sender);
opsHashMessageRoom.put(Message_Rooms, roomDto.getRoomId(), roomDto); // redis hash 에 채팅방 저장해서, 서버간 채팅방 공유 가능
messageRoom = roomRepository.save(new Room(roomDto.getId(), roomDto.getRoomName(), roomDto.getSenderName(), roomDto.getRoomId(), roomDto.getReceiverName(), sender, store));

Expand Down Expand Up @@ -145,7 +146,7 @@ public RoomDto findRoom(String roomId, String email) {
);

// 9. sender & receiver 모두 messageRoom 조회 가능
room = roomRepository.findByRoomIdAndUserOrRoomIdAndReceiverName(roomId, user, roomId, user.getUsername());
room = roomRepository.findByRoomIdAndUserOrRoomIdAndReceiverName(roomId, user, roomId, room.getReceiverName());
if (room == null) {
throw new IllegalArgumentException("채팅방이 존재하지 않습니다.");
}
Expand Down

0 comments on commit ba2a8d3

Please sign in to comment.