diff --git a/src/main/java/shop/geeksasang/config/exception/response/BaseResponseStatus.java b/src/main/java/shop/geeksasang/config/exception/response/BaseResponseStatus.java index 1bbd7dcc..390e3f55 100644 --- a/src/main/java/shop/geeksasang/config/exception/response/BaseResponseStatus.java +++ b/src/main/java/shop/geeksasang/config/exception/response/BaseResponseStatus.java @@ -94,6 +94,7 @@ public enum BaseResponseStatus { BLANK_KEYWORD(false,2205,"검색어를 입력해주세요"), NOT_EXIST_ANNOUNCEMENT(false, 2206, "존재하지 않는 공지사항입니다."), NOT_EXISTS_CHATTING_ROOM(false,2207,"채팅방이 존재하지 않습니다."), + NOT_EXISTS_PARTYCHATROOM_MEMBER(false,2208,"채팅방 멤버가 존재하지 않습니다."), diff --git a/src/main/java/shop/geeksasang/config/websocket/SocketHandler.java b/src/main/java/shop/geeksasang/config/websocket/SocketHandler.java index 2f13125d..c9761d14 100644 --- a/src/main/java/shop/geeksasang/config/websocket/SocketHandler.java +++ b/src/main/java/shop/geeksasang/config/websocket/SocketHandler.java @@ -3,13 +3,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import lombok.RequiredArgsConstructor; -import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Component; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.handler.TextWebSocketHandler; -import shop.geeksasang.dto.chat.PostChattingRes; +import shop.geeksasang.dto.chat.PostChatRes; import shop.geeksasang.service.chat.DeliveryPartyChatService; import java.util.HashMap; @@ -18,7 +17,7 @@ @RequiredArgsConstructor public class SocketHandler extends TextWebSocketHandler { - private final DeliveryPartyChatService deliveryPartyChattingService; + private final DeliveryPartyChatService deliveryPartyChatService; HashMap sessionMap = new HashMap<>(); //웹소켓 세션을 담아둘 맵 @@ -31,11 +30,12 @@ public void handleTextMessage(WebSocketSession session, TextMessage message) { try { // json 형식으로 변환 후 전송 ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); - PostChattingRes postChattingRes = mapper.readValue(msg, PostChattingRes.class); + PostChatRes postChatRes = mapper.readValue(msg, PostChatRes.class); - deliveryPartyChattingService.createChatting(1, postChattingRes.getEmail(), postChattingRes.getChatRoomId(), postChattingRes.getContent());//TODO: userId 넣는 부분 멤버 엔티티 구현 후 수정 + deliveryPartyChatService.createChat(postChatRes.getMemberId(), postChatRes.getEmail(), postChatRes.getChatRoomId(), postChatRes.getContent(), postChatRes.getIsSystemMessage(), postChatRes.getProfileImgUrl());//TODO: userId 넣는 부분 멤버 엔티티 구현 후 수정 } catch (Exception e) { System.out.println("웹소켓 메시지 전송 에러 발생"); + e.printStackTrace(); } } @@ -45,7 +45,7 @@ public void afterConnectionEstablished(WebSocketSession session) throws Exceptio super.afterConnectionEstablished(session); sessionMap.put(session.getId(), session); - System.out.println("connect"); + System.out.println("웹소켓이 연결되었습니다."); } // 소켓 연결 종료 후 diff --git a/src/main/java/shop/geeksasang/controller/chat/DeliveryPartyChatController.java b/src/main/java/shop/geeksasang/controller/chat/DeliveryPartyChatController.java index 2bcbaec7..535d394e 100644 --- a/src/main/java/shop/geeksasang/controller/chat/DeliveryPartyChatController.java +++ b/src/main/java/shop/geeksasang/controller/chat/DeliveryPartyChatController.java @@ -44,7 +44,7 @@ public BaseResponse createPartyChatRoom(HttpServletRequest request, @Reque public BaseResponse createPartyChatting(HttpServletRequest request, @RequestBody PostChattingReq dto){ JwtInfo jwtInfo = (JwtInfo) request.getAttribute("jwtInfo"); System.out.println("dto.getChatRoomId() = " + dto.getChatRoomId()); - deliveryPartyChattingService.createChatting(jwtInfo.getUserId(), "tomas", dto.getChatRoomId(), dto.getContent()); + deliveryPartyChattingService.createChat(jwtInfo.getUserId(), dto.getEmail(), dto.getChatRoomId(), dto.getContent(), dto.getIsSystemMessage(), dto.getProfileImgUrl()); return new BaseResponse("채팅송신을 성공했습니다."); } diff --git a/src/main/java/shop/geeksasang/domain/chat/Chat.java b/src/main/java/shop/geeksasang/domain/chat/Chat.java index 38726155..3fb7fc3e 100644 --- a/src/main/java/shop/geeksasang/domain/chat/Chat.java +++ b/src/main/java/shop/geeksasang/domain/chat/Chat.java @@ -28,11 +28,12 @@ public class Chat implements Serializable { private Boolean isSystemMessage; - private String nickName; + @DocumentReference(lazy = true) + private PartyChatRoomMember partyChatRoomMember; private String profileImgUrl; - private List readMembers = new ArrayList<>(); // 읽은 멤버 ID 리스트 + private List readMembers = new ArrayList<>(); // 읽은 멤버 ID 리스트 @Unwrapped(onEmpty = Unwrapped.OnEmpty.USE_EMPTY) private BaseEntityMongo baseEntityMongo; @@ -42,14 +43,14 @@ public Chat(String content) { this.baseEntityMongo = new BaseEntityMongo(); } - public Chat(String content, PartyChatRoom partyChatRoom, Boolean isSystemMessage, String nickName, String profileImgUrl, List readMembers, BaseEntityMongo baseEntityMongo) { + public Chat(String content, PartyChatRoom partyChatRoom, Boolean isSystemMessage, PartyChatRoomMember partyChatRoomMember, String profileImgUrl, List readMembers) { this.content = content; this.partyChatRoom = partyChatRoom; this.isSystemMessage = isSystemMessage; - this.nickName = nickName; + this.partyChatRoomMember = partyChatRoomMember; this.profileImgUrl = profileImgUrl; this.readMembers = readMembers; - this.baseEntityMongo = baseEntityMongo; + this.baseEntityMongo = new BaseEntityMongo(); } } diff --git a/src/main/java/shop/geeksasang/domain/chat/PartyChatRoomMember.java b/src/main/java/shop/geeksasang/domain/chat/PartyChatRoomMember.java index 3e51950e..7eb35353 100644 --- a/src/main/java/shop/geeksasang/domain/chat/PartyChatRoomMember.java +++ b/src/main/java/shop/geeksasang/domain/chat/PartyChatRoomMember.java @@ -9,6 +9,7 @@ import org.springframework.data.mongodb.core.mapping.DocumentReference; import java.time.LocalDateTime; +import java.util.List; @Document //@Document는객체를 몽고DB에 영속화시킴 = SpringDataJpa의 @Entity와 같은 역할 @ToString @@ -17,16 +18,18 @@ @AllArgsConstructor public class PartyChatRoomMember { @Id - private Long id; + private int id; - private Long memberId; + private int memberId; private LocalDateTime enterTime; private boolean isRemittance; @DocumentReference(lazy = true) // 다대일 private PartyChatRoom partyChatRoom; - public PartyChatRoomMember(LocalDateTime enterTime, boolean isRemittance, Long memberId) { + private String email; + + public PartyChatRoomMember(LocalDateTime enterTime, boolean isRemittance, int memberId) { this.enterTime = enterTime; this.isRemittance = isRemittance; this.memberId = memberId; diff --git a/src/main/java/shop/geeksasang/dto/chat/PostChatRes.java b/src/main/java/shop/geeksasang/dto/chat/PostChatRes.java new file mode 100644 index 00000000..d65778d4 --- /dev/null +++ b/src/main/java/shop/geeksasang/dto/chat/PostChatRes.java @@ -0,0 +1,80 @@ +package shop.geeksasang.dto.chat; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import shop.geeksasang.domain.chat.Chat; + +import javax.validation.constraints.NotEmpty; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +@NoArgsConstructor +@Getter +public class PostChatRes { + + private String chatId; + + @NotEmpty + private String content; + + @NotEmpty + private String chatRoomId; + + private Boolean isSystemMessage; + + private int memberId; + + private String email; + + private String profileImgUrl; + + private List readMembers = new ArrayList<>(); // 읽은 멤버 ID 리스트 + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul") + private LocalDateTime createdAt; + + public PostChatRes(String chatRoomId, String content, LocalDateTime createdAt, int memberId) { + this.chatRoomId = chatRoomId; + this.content = content; + this.createdAt = createdAt; + this.memberId = memberId; + } + + public PostChatRes(String email, String chatRoomId, String content, LocalDateTime createdAt, int memberId) { + this.email = email; + this.chatRoomId = chatRoomId; + this.content = content; + this.createdAt = createdAt; + this.memberId = memberId; + } + + @Builder + public PostChatRes(String chatId, String content, String chatRoomId, Boolean isSystemMessage, int memberId, String email, String profileImgUrl, List readMembers, LocalDateTime createdAt) { + this.chatId = chatId; + this.content = content; + this.chatRoomId = chatRoomId; + this.isSystemMessage = isSystemMessage; + this.memberId = memberId; + this.email = email; + this.profileImgUrl = profileImgUrl; + this.readMembers = readMembers; + this.createdAt = createdAt; + } + + public static PostChatRes toDto(Chat chat, String email){ + return PostChatRes.builder() + .chatId(chat.getId()) + .content(chat.getContent()) + .chatRoomId(chat.getPartyChatRoom().getId()) + .isSystemMessage(chat.getIsSystemMessage()) + .memberId(chat.getPartyChatRoomMember().getMemberId()) + .email(chat.getPartyChatRoomMember().getEmail()) + .profileImgUrl(chat.getProfileImgUrl()) + .readMembers(chat.getReadMembers()) + .createdAt(chat.getBaseEntityMongo().getCreatedAt()) + .build(); + } +} diff --git a/src/main/java/shop/geeksasang/dto/chat/PostChattingReq.java b/src/main/java/shop/geeksasang/dto/chat/PostChattingReq.java index 8903e1e9..9f4f7119 100644 --- a/src/main/java/shop/geeksasang/dto/chat/PostChattingReq.java +++ b/src/main/java/shop/geeksasang/dto/chat/PostChattingReq.java @@ -9,13 +9,21 @@ public class PostChattingReq { @NotEmpty - private String ChatRoomId; + private String content; @NotEmpty - private String content; + private String chatRoomId; + + private Boolean isSystemMessage; + + private int memberId; + + private String email; + + private String profileImgUrl; - public PostChattingReq(String ChatRoomId, String content) { - this.ChatRoomId = ChatRoomId; + public PostChattingReq(String chatRoomId, String content) { + this.chatRoomId = chatRoomId; this.content = content; } } diff --git a/src/main/java/shop/geeksasang/dto/chat/PostChattingRes.java b/src/main/java/shop/geeksasang/dto/chat/PostChattingRes.java deleted file mode 100644 index f6cdda5c..00000000 --- a/src/main/java/shop/geeksasang/dto/chat/PostChattingRes.java +++ /dev/null @@ -1,39 +0,0 @@ -package shop.geeksasang.dto.chat; - -import com.fasterxml.jackson.annotation.JsonFormat; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import javax.validation.constraints.NotEmpty; -import java.time.LocalDateTime; - -@NoArgsConstructor -@Getter -public class PostChattingRes { - - private String chattingId; - - private String email; - - @NotEmpty - private String ChatRoomId; - - @NotEmpty - private String content; - - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul") - private LocalDateTime createdAt; - - public PostChattingRes(String ChatRoomId, String content, LocalDateTime createdAt) { - this.ChatRoomId = ChatRoomId; - this.content = content; - this.createdAt = createdAt; - } - - public PostChattingRes(String email, String ChatRoomId, String content, LocalDateTime createdAt) { - this.email = email; - this.ChatRoomId = ChatRoomId; - this.content = content; - this.createdAt = createdAt; - } -} diff --git a/src/main/java/shop/geeksasang/dto/chat/chatmember/PostPartyChatRoomMemberReq.java b/src/main/java/shop/geeksasang/dto/chat/chatmember/PostPartyChatRoomMemberReq.java index 4c8f8daf..7cea5f5f 100644 --- a/src/main/java/shop/geeksasang/dto/chat/chatmember/PostPartyChatRoomMemberReq.java +++ b/src/main/java/shop/geeksasang/dto/chat/chatmember/PostPartyChatRoomMemberReq.java @@ -13,5 +13,5 @@ public class PostPartyChatRoomMemberReq { private Boolean isRemittance; - private Long memberId; + private int memberId; } diff --git a/src/main/java/shop/geeksasang/repository/chat/PartyChatRoomMemberRepository.java b/src/main/java/shop/geeksasang/repository/chat/PartyChatRoomMemberRepository.java new file mode 100644 index 00000000..304ad486 --- /dev/null +++ b/src/main/java/shop/geeksasang/repository/chat/PartyChatRoomMemberRepository.java @@ -0,0 +1,19 @@ +package shop.geeksasang.repository.chat; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; +import org.springframework.stereotype.Repository; +import shop.geeksasang.domain.chat.PartyChatRoomMember; + +import java.util.Optional; + +@Repository +public interface PartyChatRoomMemberRepository extends MongoRepository { + + + @Query(value = "{ 'memberId' : ?0 , 'chatRoomId': ?1 , 'status' : 'ACTIVE'}") + Optional findByMemberIdAndChatRoomId(int memberId, String chatRoomId); + +// @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}") +// List findByThePersonsFirstname(String firstname); +} diff --git a/src/main/java/shop/geeksasang/service/chat/DeliveryPartyChatService.java b/src/main/java/shop/geeksasang/service/chat/DeliveryPartyChatService.java index 59f3f546..bc1bfd38 100644 --- a/src/main/java/shop/geeksasang/service/chat/DeliveryPartyChatService.java +++ b/src/main/java/shop/geeksasang/service/chat/DeliveryPartyChatService.java @@ -12,7 +12,8 @@ import shop.geeksasang.domain.chat.ChatRoom; import shop.geeksasang.domain.chat.PartyChatRoomMember; import shop.geeksasang.domain.chat.PartyChatRoom; -import shop.geeksasang.dto.chat.PostChattingRes; +import shop.geeksasang.dto.chat.PostChatRes; +import shop.geeksasang.repository.chat.PartyChatRoomMemberRepository; import shop.geeksasang.repository.chat.PartyChatRoomRepository; import shop.geeksasang.rabbitmq.MQController; import shop.geeksasang.repository.chat.ChatRepository; @@ -27,13 +28,14 @@ @Transactional(readOnly = true) public class DeliveryPartyChatService { - private final ChatRoomRepository ChatRoomRepository; - private final ChatRepository chattingRepository; + private final ChatRoomRepository chatRoomRepository; + private final ChatRepository chatRepository; private final PartyChatRoomRepository partyChatRoomRepository; private final MQController mqController; + private final PartyChatRoomMemberRepository partyChatRoomMemberRepository; @Transactional(readOnly = false) - public String createChatRoom(int userId, String title){ + public String createChatRoom(int memberId, String title){ List chattings = new ArrayList<>(); List participants = new ArrayList<>(); PartyChatRoom ChatRoom = new PartyChatRoom(title, chattings, participants, "123", "국민", "Delivery", false, 5); @@ -42,25 +44,34 @@ public String createChatRoom(int userId, String title){ } @Transactional(readOnly = false) - public void createChatting(int userId, String email, String ChatRoomId, String content) { + public void createChat(int memberId, String email, String chatRoomId, String content, Boolean isSystemMessage, String profileImgUrl) { + + PartyChatRoom partyChatRoom = partyChatRoomRepository.findByPartyChatRoomId(chatRoomId) + .orElseThrow(() -> new BaseException(BaseResponseStatus.NOT_EXISTS_CHATTING_ROOM)); + + PartyChatRoomMember partyChatRoomMember = partyChatRoomMemberRepository.findByMemberIdAndChatRoomId(memberId, chatRoomId) + .orElseThrow(() -> new BaseException(BaseResponseStatus.NOT_EXISTS_PARTYCHATROOM_MEMBER)); + + List readMembers = new ArrayList<>(); + // mongoDB 채팅 저장 - Chat chatting = new Chat(content); - Chat saveChatting = chattingRepository.save(chatting); + Chat chat = new Chat(content, partyChatRoom, isSystemMessage, partyChatRoomMember, profileImgUrl, readMembers); + Chat saveChat = chatRepository.save(chat); // json 형식으로 변환 후 RabbitMQ 전송 ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); - PostChattingRes postChattingRes = new PostChattingRes(email, ChatRoomId, saveChatting.getContent(), saveChatting.getBaseEntityMongo().getCreatedAt()); // ObjectMapper가 java8의 LocalDateTime을 지원하지 않는 에러 해결 - String saveChattingJsonStr = null; + PostChatRes postChatRes = PostChatRes.toDto(saveChat, email); + String saveChatJson = null; try { - saveChattingJsonStr = mapper.writeValueAsString(postChattingRes); + saveChatJson = mapper.writeValueAsString(postChatRes); } catch (JsonProcessingException e) { e.printStackTrace(); } - mqController.sendMessage(saveChattingJsonStr, ChatRoomId); // rabbitMQ 메시지 publish + mqController.sendMessage(saveChatJson, chatRoomId); // rabbitMQ 메시지 publish } @Transactional(readOnly = false) - public void joinPartyChatRoom(String ChatRoomId, LocalDateTime enterTime, boolean isRemittance, Long memberId){ + public void joinPartyChatRoom(String ChatRoomId, LocalDateTime enterTime, boolean isRemittance, int memberId){ PartyChatRoom partyChatRoom = partyChatRoomRepository.findByPartyChatRoomId(ChatRoomId) .orElseThrow(() -> new BaseException(BaseResponseStatus.NOT_EXISTS_CHATTING_ROOM)); @@ -73,27 +84,27 @@ public void joinPartyChatRoom(String ChatRoomId, LocalDateTime enterTime, boolea } @Transactional(readOnly = true) - public List findAllPartyChatRooms(int userId){ -// List partyChatRoomList = ChatRoomRepository.findAll().stream() + public List findAllPartyChatRooms(int memberId){ +// List partyChatRoomList = chatRoomRepository.findAll().stream() // .map(ChatRoom -> ChatRoom.getId()) // .collect(Collectors.toList()); - List partyChatRoomList = ChatRoomRepository.findAll(); + List partyChatRoomList = chatRoomRepository.findAll(); return partyChatRoomList; } @Transactional(readOnly = true) - public List findPartyChatRoom(int userId, String partyChatRoomId){ + public List findPartyChatRoom(int memberId, String partyChatRoomId){ // Query query = new Query(); // query.addCriteria(Criteria.where("id").is(partyChatRoomId)); - List partyChatRoomList = ChatRoomRepository.findAllByPartyChatRoomId(partyChatRoomId); + List partyChatRoomList = chatRoomRepository.findAllByPartyChatRoomId(partyChatRoomId); return partyChatRoomList; } @Transactional(readOnly = true) - public List findPartyChattings(int userId, String partyChatRoomId){ - List chattingList = chattingRepository.findAll(); + public List findPartyChattings(int memberId, String partyChatRoomId){ + List chattingList = chatRepository.findAll(); return chattingList;