Skip to content

Commit

Permalink
[COZY-235] fix : 반환값 추가 및 응답 형식 수정 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
veronees authored Aug 30, 2024
1 parent fbd80a3 commit fa5d30f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public ResponseEntity<ApiResponse<String>> createChat(
ErrorStatus._CHATROOM_NOT_FOUND,
ErrorStatus._CHATROOM_MEMBER_MISMATCH
})
public ResponseEntity<List<ChatResponseDto>> getChatList(
public ResponseEntity<ApiResponse<ChatResponseDto>> getChatList(
@AuthenticationPrincipal MemberDetails memberDetails,
@PathVariable Long chatRoomId) {
return ResponseEntity.ok(
chatQueryService.getChatList(memberDetails.getMember(), chatRoomId));
ApiResponse.onSuccess(chatQueryService.getChatList(memberDetails.getMember(), chatRoomId)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.cozymate.cozymate_server.domain.chat.Chat;
import com.cozymate.cozymate_server.domain.chat.dto.ChatResponseDto;
import com.cozymate.cozymate_server.domain.chat.dto.ChatResponseDto.ChatContentResponseDto;
import com.cozymate.cozymate_server.domain.chatroom.ChatRoom;
import com.cozymate.cozymate_server.domain.member.Member;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class ChatConverter {

Expand All @@ -17,14 +19,23 @@ public static Chat toEntity(ChatRoom chatRoom, Member sender, String content) {
.build();
}

public static ChatResponseDto toResponseDto(String nickname, String content, LocalDateTime createdAt) {
public static ChatContentResponseDto toChatContentResponseDto(String nickname, String content,
LocalDateTime createdAt) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy.MM.dd | HH:mm");
String formattedDateTime = createdAt.format(formatter);

return ChatResponseDto.builder()
return ChatContentResponseDto.builder()
.nickname(nickname)
.content(content)
.dateTime(formattedDateTime)
.build();
}

public static ChatResponseDto toChatResponseDto(Long recipientId,
List<ChatContentResponseDto> chatContentResponseDtoList) {
return ChatResponseDto.builder()
.recipientId(recipientId)
.chatContents(chatContentResponseDtoList)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.cozymate.cozymate_server.domain.chat.dto;

import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class ChatResponseDto {

private String nickname;
private String content;
private String dateTime;
public Long recipientId;
public List<ChatContentResponseDto> chatContents;

@Getter
@Builder
public static class ChatContentResponseDto {

private String nickname;
private String content;
private String dateTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.cozymate.cozymate_server.domain.chat.Chat;
import com.cozymate.cozymate_server.domain.chat.converter.ChatConverter;
import com.cozymate.cozymate_server.domain.chat.dto.ChatResponseDto;
import com.cozymate.cozymate_server.domain.chat.dto.ChatResponseDto.ChatContentResponseDto;
import com.cozymate.cozymate_server.domain.chat.repository.ChatRepository;
import com.cozymate.cozymate_server.domain.chatroom.ChatRoom;
import com.cozymate.cozymate_server.domain.chatroom.repository.ChatRoomRepository;
Expand All @@ -23,7 +24,7 @@ public class ChatQueryService {
private final ChatRepository chatRepository;
private final ChatRoomRepository chatRoomRepository;

public List<ChatResponseDto> getChatList(Member member, Long chatRoomId) {
public ChatResponseDto getChatList(Member member, Long chatRoomId) {
ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId)
.orElseThrow(() -> new GeneralException(ErrorStatus._CHATROOM_NOT_FOUND));

Expand All @@ -34,10 +35,14 @@ public List<ChatResponseDto> getChatList(Member member, Long chatRoomId) {

List<Chat> filteredChatList = getFilteredChatList(chatRoom, member);

List<ChatResponseDto> chatResponseDtoList = toChatResponseDtoList(filteredChatList,
List<ChatContentResponseDto> chatResponseDtoList = toChatResponseDtoList(filteredChatList,
member);

return chatResponseDtoList;
Long recipientId = member.getId().equals(chatRoom.getMemberA().getId())
? chatRoom.getMemberB().getId() : chatRoom.getMemberA().getId();


return ChatConverter.toChatResponseDto(recipientId, chatResponseDtoList);
}

private List<Chat> getFilteredChatList(ChatRoom chatRoom, Member member) {
Expand All @@ -55,14 +60,14 @@ private LocalDateTime getMemberLastDeleteAt(ChatRoom chatRoom, Member member) {
: chatRoom.getMemberBLastDeleteAt();
}

private List<ChatResponseDto> toChatResponseDtoList(List<Chat> chatList, Member member) {
private List<ChatContentResponseDto> toChatResponseDtoList(List<Chat> chatList, Member member) {
return chatList.stream()
.map(chat -> {
String senderNickname = chat.getSender().getNickname();
String nickname = senderNickname.equals(member.getNickname())
? senderNickname + " (나)"
: senderNickname;
return ChatConverter.toResponseDto(nickname, chat.getContent(),
return ChatConverter.toChatContentResponseDto(nickname, chat.getContent(),
chat.getCreatedAt());
})
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.cozymate.cozymate_server.domain.chat.Chat;
import com.cozymate.cozymate_server.domain.chat.ChatTestBuilder;
import com.cozymate.cozymate_server.domain.chat.dto.ChatResponseDto;
import com.cozymate.cozymate_server.domain.chat.dto.ChatResponseDto.ChatContentResponseDto;
import com.cozymate.cozymate_server.domain.chat.repository.ChatRepository;
import com.cozymate.cozymate_server.domain.chatroom.ChatRoom;
import com.cozymate.cozymate_server.domain.chatroom.ChatRoomTestBuilder;
Expand Down Expand Up @@ -86,9 +87,11 @@ void setUp() {
@Test
@DisplayName("해당 쪽지방의 쪽지 상세 내역을 리스트로 반환한다.")
void it_returns_chat_list() {
List<ChatResponseDto> result = chatQueryService.getChatList(me,
ChatResponseDto chatResponseDto = chatQueryService.getChatList(me,
chatRoom.getId());

List<ChatContentResponseDto> result = chatResponseDto.chatContents;

assertThat(result.size()).isEqualTo(2);
assertThat(result.get(0).getNickname()).isEqualTo(me.getNickname() + " (나)");
assertThat(result.get(0).getContent()).isEqualTo(chat.getContent());
Expand Down Expand Up @@ -135,9 +138,11 @@ void setUp() {
@Test
@DisplayName("나간 후에 온 쪽지 상세 내역만 리스트로 반환한다.")
void it_returns_chat_list_after_delete() {
List<ChatResponseDto> result = chatQueryService.getChatList(me,
ChatResponseDto chatResponseDto = chatQueryService.getChatList(me,
chatRoom.getId());

List<ChatContentResponseDto> result = chatResponseDto.getChatContents();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy.MM.dd | HH:mm");
LocalDateTime parsedDateTime = LocalDateTime.parse(result.get(0).getDateTime(),
formatter);
Expand Down

0 comments on commit fa5d30f

Please sign in to comment.