Skip to content

Commit

Permalink
feat: 사용자가 참여중인 채팅방 목록 API #39
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjun100 committed Aug 17, 2024
1 parent 5535f61 commit 0148ff0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.team13.servicechat.dto.ChatroomDto;
import com.team13.servicechat.dto.JwtPayloadDto;
import com.team13.servicechat.entity.Chatroom;
import com.team13.servicechat.feign.UserFeignClient;
import com.team13.servicechat.service.ChatroomService;
import com.team13.servicechat.service.JwtService;
Expand All @@ -12,6 +13,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Log4j2
Expand Down Expand Up @@ -46,7 +48,7 @@ public String serviceConnectionTest() {
// 채팅방 정보(공동구매 데이터 및 메시지 리스트) 반환
@GetMapping("/chatroom")
public ResponseEntity<ChatroomDto> getChatroom(@CookieValue("LTK") String loginToken,
@RequestParam("id") String chatroomId) {
@RequestParam("id") String chatroomId) {

JwtPayloadDto payload = jwtService.getPayloadFromToken(loginToken);

Expand All @@ -61,6 +63,16 @@ public ResponseEntity<ChatroomDto> getChatroom(@CookieValue("LTK") String loginT
}


// 사용자가 속한 채팅방 리스트 반환
@GetMapping("/chatroom/list")
public List<ChatroomDto> getChatroomList(@CookieValue("LTK") String loginToken) {

JwtPayloadDto payload = jwtService.getPayloadFromToken(loginToken);

return chatroomService.getChatroomList(payload.getUserId());
}


// 채팅방 생성 후 채팅방 ID 반환
// feign 클라이언트 전용
@PostMapping("/chatroom")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class ChatroomDto {
private long postId;
private List<Long> userIds;
private List<ChatMessageDto> messages;
private String lastMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import com.team13.servicechat.entity.UserJoinedChats;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface UserJoinedChatRepository extends MongoRepository<UserJoinedChats, Long> {
public interface UserJoinedChatsRepository extends MongoRepository<UserJoinedChats, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.team13.servicechat.entity.UserJoinedChats;
import com.team13.servicechat.repository.ChatMessageRepository;
import com.team13.servicechat.repository.ChatroomRepository;
import com.team13.servicechat.repository.UserJoinedChatRepository;
import com.team13.servicechat.repository.UserJoinedChatsRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -27,7 +27,7 @@ public class ChatroomService {
private final ChatMessageRepository chatMessageRepository;

// 사용자가 속한 채팅방 리스트를 관리하는 레포지토리
private final UserJoinedChatRepository userJoinedChatRepository;
private final UserJoinedChatsRepository userJoinedChatsRepository;


@PostConstruct
Expand All @@ -41,12 +41,12 @@ private void init() {
.userIds(new ArrayList<>(List.of(1L, 2L)))
.build());

userJoinedChatRepository.save(UserJoinedChats.builder()
userJoinedChatsRepository.save(UserJoinedChats.builder()
.id(1L)
.chatroomIds(List.of("test-chatroom"))
.build());

userJoinedChatRepository.save(UserJoinedChats.builder()
userJoinedChatsRepository.save(UserJoinedChats.builder()
.id(2L)
.chatroomIds(List.of("test-chatroom"))
.build());
Expand Down Expand Up @@ -79,13 +79,13 @@ public String createChatroom(String userId, Long postId) {
.build();

// 만약 이미 사용자 정보가 저장되어 있다면 해당 정보로 치환함
if (userJoinedChatRepository.existsById(Long.parseLong(userId))) {
userJoinedChats = userJoinedChatRepository.findById(Long.parseLong(userId)).orElseThrow();
if (userJoinedChatsRepository.existsById(Long.parseLong(userId))) {
userJoinedChats = userJoinedChatsRepository.findById(Long.parseLong(userId)).orElseThrow();
}

// 생성된 채팅방 추가 후 변경내용 저장
userJoinedChats.addChatroomId(savedChatroom.getId());
userJoinedChatRepository.save(userJoinedChats);
userJoinedChatsRepository.save(userJoinedChats);

return savedChatroom.getId();
}
Expand Down Expand Up @@ -115,13 +115,13 @@ public void joinChatroom(String chatroomId, String userId) {
.build();

// 만약 이미 사용자 정보가 저장되어 있다면 해당 정보로 치환함
if (userJoinedChatRepository.existsById(Long.parseLong(userId))) {
userJoinedChats = userJoinedChatRepository.findById(Long.parseLong(userId)).orElseThrow();
if (userJoinedChatsRepository.existsById(Long.parseLong(userId))) {
userJoinedChats = userJoinedChatsRepository.findById(Long.parseLong(userId)).orElseThrow();
}

// 생성된 채팅방 추가 후 변경내용 저장
userJoinedChats.addChatroomId(chatroomId);
userJoinedChatRepository.save(userJoinedChats);
userJoinedChatsRepository.save(userJoinedChats);
}
}

Expand Down Expand Up @@ -181,6 +181,35 @@ public ChatroomDto getChatroomDtoById(String chatroomId) {
}


// 사용자가 속한 채팅방 리스트 반환
public List<ChatroomDto> getChatroomList(String userId) {

Optional<UserJoinedChats> userJoinedChats = userJoinedChatsRepository.findById(Long.parseLong(userId));

// 사용자가 참여한 채팅방이 존재하는 경우에만 결과 반환
if (userJoinedChats.isPresent()) {
List<ChatroomDto> chatroomDtos = new ArrayList<>();

// 채팅방 ID를 추출하여 chatroomDtos에 채팅방 정보 추가
for (String chatroomId : userJoinedChats.get().getChatroomIds()) {

// 채팅방 정보 가져오기
Optional<Chatroom> opChatroom = chatroomRepository.findById(chatroomId);

// 존재하는 채팅방인 경우에만 chatroomDtos에 추가
opChatroom.ifPresent(chatroom -> chatroomDtos.add(ChatroomDto.builder()
.id(chatroom.getId())
.postId(chatroom.getPostId())
.lastMessage(chatroom.getLastMessage())
.build()));
}

return chatroomDtos;
}

return new ArrayList<>();
}


// 채팅 메시지 가져오기
public Optional<ChatMessage> getMessageById(Long messageId) {
Expand Down

0 comments on commit 0148ff0

Please sign in to comment.