Skip to content

Commit

Permalink
Merge pull request #16 from catchroom/develop
Browse files Browse the repository at this point in the history
์ฑ„ํŒ…๋ฐฉ ๋‚˜๊ฐ€๊ธฐ, ๋“ฑ์žฅํ•˜๊ธฐ ๊ธฐ๋Šฅ ๋ฐฐํฌ
  • Loading branch information
sungjiwoon authored Jan 19, 2024
2 parents b877f87 + 075ebf3 commit 6908896
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.catchroom.chat.chatroom.controller;

import com.catchroom.chat.chatroom.dto.ChatRoomCreateRequest;
import com.catchroom.chat.chatroom.dto.ChatRoomListGetResponse;
import com.catchroom.chat.chatroom.service.ChatRoomService;
import com.catchroom.chat.global.common.ApiResponse;
import com.catchroom.chat.message.dto.AccommodationResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -13,15 +14,10 @@
@CrossOrigin(origins = "http://localhost:3000", allowedHeaders = "*")
public class ChatRoomController {
private final ChatRoomService chatRoomService;
@GetMapping("/{userId}")
public ResponseEntity<?> findChatRoomListByMemberId(@PathVariable Long userId) {
return ResponseEntity.ok(
ApiResponse.create(6000, chatRoomService.findChatRoomListByMemberId(userId)));
}

@PostMapping("/create")
public ResponseEntity<?> createRoom(@RequestBody ChatRoomCreateRequest chatRoomCreateRequest) {
return ResponseEntity.ok(
ApiResponse.create(6002,chatRoomService.createChatRoom(chatRoomCreateRequest)));
//TODO ํ† ํฐ ํ—ค๋”์— ๋‹ด์„ ๊ฒƒ
@GetMapping("/list")
public ChatRoomListGetResponse getChatRoomList() {
return chatRoomService.getChatRoomList();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package com.catchroom.chat.chatroom.dto;

import com.catchroom.chat.chatroom.entity.ChatRoom;
import com.catchroom.chat.message.type.UserIdentity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class ChatRoomListGetResponse {
private List<ChatRoom> ChatRoomListUserIsBuyer;
private List<ChatRoom> ChatRoomListUserIsSeller;
private String chatRoomNumber;
private Long buyerId;
private Long sellerId;
private Long productId;
private UserIdentity loginUserIdentity;

public static ChatRoomListGetResponse of(List<ChatRoom> ChatRoomListUserIsBuyer, List<ChatRoom> ChatRoomListUserIsSeller) {
return ChatRoomListGetResponse.builder()
.ChatRoomListUserIsBuyer(ChatRoomListUserIsBuyer)
.ChatRoomListUserIsSeller(ChatRoomListUserIsSeller)
.build();
}
}
20 changes: 3 additions & 17 deletions src/main/java/com/catchroom/chat/chatroom/entity/ChatRoom.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.catchroom.chat.chatroom.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -26,18 +23,7 @@ public class ChatRoom implements Serializable {
private Long buyerId;
private Long productId;
private String chatRoomNumber;

public ChatRoom(Long sellerId, Long buyerId, Long productId, String chatRoomNumber) {
this.sellerId = sellerId;
this.buyerId = buyerId;
this.productId = productId;
this.chatRoomNumber = chatRoomNumber;
}

public static ChatRoom create(Long sellerId, Long buyerId, Long productId) {
String chatRoomNumber = UUID.randomUUID().toString();
ChatRoom chatRoom = new ChatRoom(sellerId,buyerId,productId,chatRoomNumber);
return chatRoom;
}
@Transient
private Boolean is_buyer;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;

public interface ChatRoomRepositoryJPA extends JpaRepository<ChatRoom,Long> {
List<ChatRoom> findAllByBuyerId(Long buyerId);

List<ChatRoom> findAllBySellerId(Long sellerId);

}
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
package com.catchroom.chat.chatroom.service;

import com.catchroom.chat.chatroom.dto.ChatRoomCreateRequest;
import com.catchroom.chat.chatroom.dto.ChatRoomCreateResponse;
import com.catchroom.chat.chatroom.dto.ChatRoomListGetResponse;
import com.catchroom.chat.chatroom.entity.ChatRoom;
import com.catchroom.chat.chatroom.repository.ChatRoomRepositoryJPA;
import com.catchroom.chat.message.feign.MainFeignClient;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Service
public class ChatRoomService {
private final ChatRoomRepositoryJPA chatRoomRepository;
private final MainFeignClient mainFeignClient;
@Resource(name = "redisTemplate") //redisTemplate bean ์ฃผ์ž….
private HashOperations<String, String, ChatRoom> opsHashChatRoom;
private static final String CHAT_ROOMS = "CHAT_ROOM_REDIS";

@Transactional(readOnly = true)
public ChatRoomListGetResponse findChatRoomListByMemberId(Long userId) {
List<ChatRoom> ChatRoomListUserIsBuyer = chatRoomRepository.findAllByBuyerId(userId);

List<ChatRoom> ChatRoomListUserIsSeller = chatRoomRepository.findAllBySellerId(userId);
return ChatRoomListGetResponse.of(ChatRoomListUserIsBuyer, ChatRoomListUserIsSeller);
}

@Transactional
public ChatRoomCreateResponse createChatRoom(ChatRoomCreateRequest chatRoomCreateRequest) {
ChatRoom chatRoom = ChatRoom.create(
chatRoomCreateRequest.getSellerId(),
chatRoomCreateRequest.getBuyerId(),
chatRoomCreateRequest.getProductId());
chatRoomRepository.save(chatRoom);
opsHashChatRoom.put(CHAT_ROOMS, chatRoom.getChatRoomNumber(), chatRoom);
return ChatRoomCreateResponse.fromEntity(chatRoom);
public ChatRoomListGetResponse getChatRoomList() {
return mainFeignClient.getChatRoomList();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.catchroom.chat.message.feign;

import com.catchroom.chat.chatroom.dto.ChatRoomListGetResponse;
import com.catchroom.chat.global.config.FeignConfig;
import com.catchroom.chat.message.dto.AccommodationResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -23,6 +25,6 @@ public interface MainFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/accommodation/{accommodationId}")
AccommodationResponse getAccommodationDto(@PathVariable Long accommodationId);



@GetMapping(value = "/chat/room/list")
ChatRoomListGetResponse getChatRoomList();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.catchroom.chat.message.controller;
package com.catchroom.chat.feign.controller;

import com.catchroom.chat.feign.service.MainFeignService;
import com.catchroom.chat.message.dto.AccommodationResponse;
import com.catchroom.chat.message.service.MainFeignService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.catchroom.chat.message.service;
package com.catchroom.chat.feign.service;

import com.catchroom.chat.feign.client.MainFeignClient;
import com.catchroom.chat.message.dto.AccommodationResponse;
import com.catchroom.chat.message.feign.MainFeignClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -19,3 +19,4 @@ public AccommodationResponse getAccommodationResponse(Long accommodationId) {
}

}

17 changes: 3 additions & 14 deletions src/main/java/com/catchroom/chat/global/handler/StompHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,12 @@ public class StompHandler implements ChannelInterceptor {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
log.info("StompHandler : {}, command : {}", message.getPayload(), accessor.getCommand());
if (StompCommand.CONNECT == accessor.getCommand()) {
// websocket ์—ฐ๊ฒฐ์š”์ฒญ
/** JWT token ์กด์žฌ x */

// String jwtToken = accessor.getFirstNativeHeader("token");
// log.info("CONNECT {}", jwtToken);
// // Header์˜ jwt token ๊ฒ€์ฆ
// jwtTokenProvider.validateToken(jwtToken);

} else if (StompCommand.SEND == accessor.getCommand()) {

} else if (StompCommand.SUBSCRIBE == accessor.getCommand()) { // ์ฑ„ํŒ…๋ฃธ ๊ตฌ๋…์š”์ฒญ
if (StompCommand.SUBSCRIBE == accessor.getCommand()) { // ์ฑ„ํŒ…๋ฃธ ๊ตฌ๋…์š”์ฒญ

// header ์ •๋ณด์—์„œ ๊ตฌ๋… destination ์ •๋ณด๋ฅผ ์–ป๊ณ , roomId๋ฅผ ์ถ”์ถœํ•œ๋‹ค. destination = uri
String roomId = chatService.getRoomId(
Optional.ofNullable((String) message.getHeaders().get("simpDestination")).orElse("InvalidRoomId")
Optional.ofNullable((String) message.getHeaders().get("simpDestination"))
.orElse("InvalidRoomId")
);

// ์ฑ„ํŒ…๋ฐฉ์— ๋“ค์–ด์˜จ ํด๋ผ์ด์–ธํŠธ sessionId๋ฅผ roomId์™€ ๋งตํ•‘ํ•ด ๋†“๋Š”๋‹ค.(๋‚˜์ค‘์— ํŠน์ • ์„ธ์…˜์ด ์–ด๋–ค ์ฑ„ํŒ…๋ฐฉ์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š”์ง€ ์•Œ๊ธฐ ์œ„ํ•จ)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public String getRoomId(String destination) {
public void sendChatMessage(ChatMessageDto chatMessage) {
chatMessage.setUserCount(chatRoomRepository.getUserCount(chatMessage.getRoomId()));

if (MessageType.ENTER.equals(chatMessage.getType())) {
chatMessage.setMessage(chatMessage.getSender() + "๋‹˜์ด ๋ฐฉ์— ์ž…์žฅํ–ˆ์Šต๋‹ˆ๋‹ค.");
chatMessage.setSender("[์•Œ๋ฆผ]");

} else if (MessageType.QUIT.equals(chatMessage.getType())) {
chatMessage.setMessage(chatMessage.getSender() + "๋‹˜์ด ๋ฐฉ์—์„œ ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.");
chatMessage.setSender("[์•Œ๋ฆผ]");
}
// if (MessageType.ENTER.equals(chatMessage.getType())) {
// chatMessage.setMessage(chatMessage.getSender() + "๋‹˜์ด ๋ฐฉ์— ์ž…์žฅํ–ˆ์Šต๋‹ˆ๋‹ค.");
// chatMessage.setSender("[์•Œ๋ฆผ]");
//
// } else if (MessageType.QUIT.equals(chatMessage.getType())) {
// chatMessage.setMessage(chatMessage.getSender() + "๋‹˜์ด ๋ฐฉ์—์„œ ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.");
// chatMessage.setSender("[์•Œ๋ฆผ]");
// }

redisPublisher.publish(chatMessage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.catchroom.chat.message.type;

public enum UserIdentity {
NONLOGINUSER,BUYER,SELLER
}

0 comments on commit 6908896

Please sign in to comment.