Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat #158] 채팅 요청 상세 조회 API #159

Merged
merged 13 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.dnd.gongmuin.chat_inquiry.dto.AcceptChatResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryDetailResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryRequest;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryResponse;
Expand Down Expand Up @@ -42,7 +43,17 @@ public ResponseEntity<CreateChatInquiryResponse> createChatInquiry(
return ResponseEntity.ok(response);
}

@Operation(summary = "채팅방 요청 목록 조회 API", description = "회원의 채팅방 목록을 조회한다.")
@Operation(summary = "채팅 요청 상세 조회 API", description = "채팅방 요청을 조회한다.")
@GetMapping("/api/chat/inquiries/{chatInquiryId}")
public ResponseEntity<ChatInquiryDetailResponse> getChatInquiryById(
@PathVariable("chatInquiryId") Long chatInquiryId,
@AuthenticationPrincipal Member member
) {
ChatInquiryDetailResponse response = chatInquiryService.getChatInquiryById(chatInquiryId, member);
return ResponseEntity.ok(response);
}

@Operation(summary = "채팅 요청 목록 조회 API", description = "회원의 채팅 목록을 조회한다.")
@GetMapping("/api/chat/inquiries")
public ResponseEntity<PageResponse<ChatInquiryResponse>> getChatInquiresByMember(
@AuthenticationPrincipal Member member,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dnd.gongmuin.chat_inquiry.dto;

import com.dnd.gongmuin.question_post.dto.response.MemberInfo;
hyun2371 marked this conversation as resolved.
Show resolved Hide resolved

public record ChatInquiryDetailResponse(
Long chatInquiryId,
String inquiryMessage,
String inquiryStatus,
boolean isInquirer,
MemberInfo chatPartner
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ public static CreateChatInquiryResponse toCreateChatInquiryResponse(
);
}

public static ChatInquiryDetailResponse toChatInquiryDetailResponse(
ChatInquiry chatInquiry,
Member member
) {
boolean isInquirer = chatInquiry.getInquirer().equals(member);
dudxo marked this conversation as resolved.
Show resolved Hide resolved
Member chatPartner = isInquirer ? chatInquiry.getAnswerer() : chatInquiry.getInquirer();
return new ChatInquiryDetailResponse(chatInquiry.getId(),
chatInquiry.getMessage(),
chatInquiry.getStatus().getLabel(),
isInquirer,
new MemberInfo(
chatPartner.getId(),
chatPartner.getNickname(),
chatPartner.getJobGroup().getLabel(),
chatPartner.getProfileImageNo()
)
);
}

public static AcceptChatResponse toAcceptChatResponse(
ChatInquiry chatInquiry,
ChatRoom chatRoom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.dnd.gongmuin.chat_inquiry.domain.ChatInquiry;
import com.dnd.gongmuin.chat_inquiry.dto.AcceptChatResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryDetailResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryMapper;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryRequest;
Expand Down Expand Up @@ -70,6 +71,12 @@ public CreateChatInquiryResponse createChatInquiry(CreateChatInquiryRequest requ
return ChatInquiryMapper.toCreateChatInquiryResponse(chatInquiry);
}

@Transactional(readOnly = true)
public ChatInquiryDetailResponse getChatInquiryById(Long chatInquiryId, Member member) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChatInquiry를 조회해서 찾는 getChatInquiryById(Long chatInquiryId)와 혼동되네요!

채팅 요청 상세 조회를 하는 메서드니, getChatInquiryDetails(Long chatInquiryId, Member member)는 어떤가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드 컨벤션이 따라서 여태까지 이렇게 작성한거라서 지금 바꾸는게 오히려 더 헷갈릴 것 같아요..!
repository 접근함수는 private이기도 하고, 반환타입도 다르니까 보고 구분할 수 있지 않을까 싶습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getChatInquiryDetailsById가 맞는거 아닐까요 그러면?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- 단건 조회 : get + 목적어 + by + 필드
- 여러건 조회 : get + 목적어s + by + 필드

여러 건 조회 메서드명이 getChatInquiresByMember니까 단건 조회는 getChatInquiryById가 맞는 것 같습니다~

ChatInquiry chatInquiry = getChatInquiryById(chatInquiryId);
return ChatInquiryMapper.toChatInquiryDetailResponse(chatInquiry, member);
}

@Transactional(readOnly = true)
public PageResponse<ChatInquiryResponse> getChatInquiresByMember(Member member, Pageable pageable) {
Slice<ChatInquiryResponse> responsePage = chatInquiryRepository.getChatInquiresByMember(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void teardown() {
creditHistoryRepository.deleteAll();
memberRepository.deleteAll();
questionPostRepository.deleteAll();
chatInquiryRepository.deleteAll();
chatRoomRepository.deleteAll();
chatMessageRepository.deleteAll();
}
Expand All @@ -82,6 +83,32 @@ void createChatInquiry() throws Exception {
.andDo(MockMvcResultHandlers.print());
}

@DisplayName("[채팅 요청 아이디로 상세 채팅 요청을 조회할 수 있다.]")
@Test
void getChatInquiryById() throws Exception {
//given
Member chatPartner = memberRepository.save(MemberFixture.member5());
QuestionPost questionPost = questionPostRepository.save(QuestionPostFixture.questionPost(loginMember));
ChatInquiry chatInquiry = chatInquiryRepository.save(
ChatInquiryFixture.chatInquiry(questionPost, loginMember, chatPartner, INQUIRY_MESSAGE)
);

//when & then
mockMvc.perform(get("/api/chat/inquiries/{chatInquiryId}", chatInquiry.getId())
.cookie(accessToken))
.andExpect(status().isOk())
.andExpect(jsonPath("$.chatInquiryId")
.value(chatInquiry.getId()))
.andExpect(jsonPath("$.inquiryStatus")
.value(InquiryStatus.PENDING.getLabel()))
.andExpect(jsonPath("$.chatPartner.memberId")
.value(chatPartner.getId()))
.andExpect(jsonPath("$.isInquirer")
.value(chatInquiry.getInquirer().equals(loginMember)))
.andExpect(jsonPath("$.inquiryStatus")
.value(InquiryStatus.PENDING.getLabel()));
}

@DisplayName("[회원의 채팅 요청 목록을 조회할 수 있다.]")
@Test
void getChatInquiresByMember() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Optional;

import org.assertj.core.api.Assertions;
hyun2371 marked this conversation as resolved.
Show resolved Hide resolved
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -21,6 +22,7 @@
import com.dnd.gongmuin.chat_inquiry.domain.ChatInquiry;
import com.dnd.gongmuin.chat_inquiry.domain.InquiryStatus;
import com.dnd.gongmuin.chat_inquiry.dto.AcceptChatResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryDetailResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryRequest;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryResponse;
Expand Down Expand Up @@ -134,6 +136,24 @@ void createInquiry_fails() {
.hasMessageContaining(MemberErrorCode.NOT_ENOUGH_CREDIT.getMessage());
}

@DisplayName("[채팅 요청 아이디로 채팅 요청 상세를 조회할 수 있다.]")
@Test
void getChatInquiryById() {
//given
Member inquirer = MemberFixture.member(1L);
Member answerer = MemberFixture.member(2L);
given(chatInquiryRepository.findById(1L))
.willReturn(Optional.of(ChatInquiryFixture.chatInquiry(
1L, QuestionPostFixture.questionPost(inquirer), inquirer, answerer, INQUIRY_MESSAGE)
));
//when
ChatInquiryDetailResponse response = chatInquiryService.getChatInquiryById(1L, inquirer);

//then
Assertions.assertThat(response.chatPartner().memberId())
.isEqualTo(answerer.getId());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

줄바꿈은 일부로 하신건가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 가독성 생각해서 줄바꿈 했습니다

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음..이전에도 이런 aseertThat().isEqualTo()는 줄바꿈을 하지 않으셨었었고, 되게 길지도 않아서 한 줄로 하는게 더 좋아보여요
그리고 static import를 하면 더 짧아지고요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 수정하겠습니다~

}

@DisplayName("[회원이 속한 채팅 요청 목록을 조회할 수 있다.]")
@Test
void getChatInquiresByMember() {
Expand Down
Loading