Skip to content

Commit

Permalink
[feat #122] 채팅방 목록 조회 API 필터링 추가 (#123)
Browse files Browse the repository at this point in the history
* [feat] : 채팅방 상태 enum 변환로직 추가

* [feat] : repository에 채팅 상태 필터 추가

* [test] : repository에 채팅 상태 필터 추가 반영

* [feat] : 사용자에게 request param으로 상태필드 받기

* [test] : 사용자에게 request param으로 상태필 받기 반영

* [test] : request param String 대신 label로 변경

* [style] : 코드 리포멧팅
  • Loading branch information
hyun2371 authored Sep 30, 2024
1 parent 32c36f1 commit b1d7de8
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dnd.gongmuin.chat.dto.request.CreateChatRoomRequest;
Expand Down Expand Up @@ -58,9 +59,10 @@ public ResponseEntity<ChatRoomDetailResponse> createChatRoom(
@Operation(summary = "채팅방 목록 조회 API", description = "회원의 채팅방 목록을 조회한다.")
@GetMapping("/api/chat-rooms")
public ResponseEntity<List<ChatRoomSimpleResponse>> getChatRoomsByMember(
@RequestParam("status") String status,
@AuthenticationPrincipal Member member
) {
List<ChatRoomSimpleResponse> response = chatRoomService.getChatRoomsByMember(member);
List<ChatRoomSimpleResponse> response = chatRoomService.getChatRoomsByMember(member, status);
return ResponseEntity.ok(response);
}

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/dnd/gongmuin/chat/domain/ChatStatus.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.dnd.gongmuin.chat.domain;

import java.util.Arrays;

import com.dnd.gongmuin.chat.exception.ChatErrorCode;
import com.dnd.gongmuin.common.exception.runtime.ValidationException;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand All @@ -12,4 +17,15 @@ public enum ChatStatus {
REJECTED("거절됨");

private final String label;

public static ChatStatus from(String input) {
return Arrays.stream(values())
.filter(status -> status.isEqual(input))
.findAny()
.orElseThrow(() -> new ValidationException(ChatErrorCode.NOT_FOUND_CHAT_STATUS));
}

private boolean isEqual(String input) {
return input.equals(this.label);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum ChatErrorCode implements ErrorCode {
NOT_FOUND_CHAT_ROOM("해당 아이디의 채팅방이 존재하지 않습니다.", "CH_002"),
UNAUTHORIZED_REQUEST("채팅 수락을 하거나 거절할 권한이 없습니다.", "CH_003"),
UNABLE_TO_CHANGE_CHAT_STATUS("이미 수락했거나 거절한 요청입니다.", "CH_004"),
UNAUTHORIZED_CHAT_ROOM("권한이 없는 채팅방입니다", "CH_005");
UNAUTHORIZED_CHAT_ROOM("권한이 없는 채팅방입니다.", "CH_005"),
NOT_FOUND_CHAT_STATUS("채팅방 상태값을 올바르게 입력해주세요.", "CH_006");

private final String message;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import java.util.List;

import com.dnd.gongmuin.chat.domain.ChatStatus;
import com.dnd.gongmuin.chat.dto.response.ChatRoomInfo;
import com.dnd.gongmuin.member.domain.Member;

public interface ChatRoomQueryRepository {
List<ChatRoomInfo> getChatRoomsByMember(Member member);
List<ChatRoomInfo> getChatRoomsByMember(Member member, ChatStatus chatStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.List;

import com.dnd.gongmuin.chat.domain.ChatStatus;
import com.dnd.gongmuin.chat.dto.response.ChatRoomInfo;
import com.dnd.gongmuin.chat.dto.response.QChatRoomInfo;
import com.dnd.gongmuin.member.domain.Member;
Expand All @@ -16,7 +17,7 @@
public class ChatRoomQueryRepositoryImpl implements ChatRoomQueryRepository {
private final JPAQueryFactory queryFactory;

public List<ChatRoomInfo> getChatRoomsByMember(Member member) {
public List<ChatRoomInfo> getChatRoomsByMember(Member member, ChatStatus chatStatus) {
return queryFactory
.select(new QChatRoomInfo(
chatRoom.id,
Expand All @@ -39,7 +40,8 @@ public List<ChatRoomInfo> getChatRoomsByMember(Member member) {
))
.from(chatRoom)
.where(chatRoom.inquirer.id.eq(member.getId())
.or(chatRoom.answerer.id.eq(member.getId())))
.or(chatRoom.answerer.id.eq(member.getId()))
.and(chatRoom.status.eq(chatStatus)))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.transaction.annotation.Transactional;

import com.dnd.gongmuin.chat.domain.ChatRoom;
import com.dnd.gongmuin.chat.domain.ChatStatus;
import com.dnd.gongmuin.chat.dto.ChatMessageMapper;
import com.dnd.gongmuin.chat.dto.ChatRoomMapper;
import com.dnd.gongmuin.chat.dto.request.CreateChatRoomRequest;
Expand Down Expand Up @@ -89,9 +90,9 @@ public ChatRoomDetailResponse createChatRoom(CreateChatRoomRequest request, Memb
}

@Transactional(readOnly = true)
public List<ChatRoomSimpleResponse> getChatRoomsByMember(Member member) {
public List<ChatRoomSimpleResponse> getChatRoomsByMember(Member member, String chatStatus) {
// 회원 채팅방 정보 가져오기
List<ChatRoomInfo> chatRoomInfos = chatRoomRepository.getChatRoomsByMember(member);
List<ChatRoomInfo> chatRoomInfos = chatRoomRepository.getChatRoomsByMember(member, ChatStatus.from(chatStatus));
// chatRoomId 리스트 추출
List<Long> chatRoomIds = chatRoomInfos.stream()
.map(ChatRoomInfo::chatRoomId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ void getChatRoomsByMember() throws Exception {
)
);
mockMvc.perform(get("/api/chat-rooms")
.cookie(accessToken))
.cookie(accessToken)
.param("status", ChatStatus.PENDING.getLabel()))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.beans.factory.annotation.Autowired;

import com.dnd.gongmuin.chat.domain.ChatRoom;
import com.dnd.gongmuin.chat.domain.ChatStatus;
import com.dnd.gongmuin.chat.dto.response.ChatRoomInfo;
import com.dnd.gongmuin.common.fixture.ChatRoomFixture;
import com.dnd.gongmuin.common.fixture.MemberFixture;
Expand Down Expand Up @@ -43,7 +44,7 @@ void getChatRoomsByMember() {
chatRoomRepository.save(ChatRoomFixture.chatRoom(questionPost, target, answerer))
));
//when
List<ChatRoomInfo> chatRoomInfos = chatRoomRepository.getChatRoomsByMember(target);
List<ChatRoomInfo> chatRoomInfos = chatRoomRepository.getChatRoomsByMember(target, ChatStatus.PENDING);
//then
Assertions.assertAll(
() -> assertThat(chatRoomInfos).hasSize(2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void activateInteraction_old() throws Exception {
mockMvc.perform(post("/api/question-posts/{questionPostId}/inactivated", questionPost.getId())
.contentType(APPLICATION_JSON)
.cookie(accessToken)
.param("type", "추천")
.param("type", InteractionType.RECOMMEND.getLabel())
)
.andExpect(status().isOk());
}
Expand Down

0 comments on commit b1d7de8

Please sign in to comment.