Skip to content

Commit

Permalink
Merge pull request #246 from Geek-sasaeng/tomas
Browse files Browse the repository at this point in the history
Tomas [feat]:채팅 전송 API(Chat, PartyChatRoomMember 엔티티 여러 필드 추가, createCha… …t메소드 세부 로직 모두 반영)
  • Loading branch information
happysubin authored Nov 26, 2022
2 parents 9f12a08 + 43996a4 commit 4271b8a
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,"채팅방 멤버가 존재하지 않습니다."),



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +17,7 @@
@RequiredArgsConstructor
public class SocketHandler extends TextWebSocketHandler {

private final DeliveryPartyChatService deliveryPartyChattingService;
private final DeliveryPartyChatService deliveryPartyChatService;

HashMap<String, WebSocketSession> sessionMap = new HashMap<>(); //웹소켓 세션을 담아둘 맵

Expand All @@ -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();
}
}

Expand All @@ -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("웹소켓이 연결되었습니다.");
}

// 소켓 연결 종료 후
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public BaseResponse<Long> createPartyChatRoom(HttpServletRequest request, @Reque
public BaseResponse<String> 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("채팅송신을 성공했습니다.");
}

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/shop/geeksasang/domain/chat/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> readMembers = new ArrayList<>(); // 읽은 멤버 ID 리스트
private List<Integer> readMembers = new ArrayList<>(); // 읽은 멤버 ID 리스트

@Unwrapped(onEmpty = Unwrapped.OnEmpty.USE_EMPTY)
private BaseEntityMongo baseEntityMongo;
Expand All @@ -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<Long> readMembers, BaseEntityMongo baseEntityMongo) {
public Chat(String content, PartyChatRoom partyChatRoom, Boolean isSystemMessage, PartyChatRoomMember partyChatRoomMember, String profileImgUrl, List<Integer> 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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/shop/geeksasang/dto/chat/PostChatRes.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> 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();
}
}
16 changes: 12 additions & 4 deletions src/main/java/shop/geeksasang/dto/chat/PostChattingReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
39 changes: 0 additions & 39 deletions src/main/java/shop/geeksasang/dto/chat/PostChattingRes.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public class PostPartyChatRoomMemberReq {

private Boolean isRemittance;

private Long memberId;
private int memberId;
}
Original file line number Diff line number Diff line change
@@ -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<PartyChatRoomMember, String> {


@Query(value = "{ 'memberId' : ?0 , 'chatRoomId': ?1 , 'status' : 'ACTIVE'}")
Optional<PartyChatRoomMember> findByMemberIdAndChatRoomId(int memberId, String chatRoomId);

// @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
// List<Person> findByThePersonsFirstname(String firstname);
}
Loading

0 comments on commit 4271b8a

Please sign in to comment.