From 211af3f486cdc12f35633b736882fb4307cfe751 Mon Sep 17 00:00:00 2001 From: becooq81 Date: Mon, 5 Aug 2024 23:56:59 +0900 Subject: [PATCH] refactor(ConversationService): modularize big chunks of code & reflect front end request --- .../service/ConversationService.java | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/poolc/api/conversation/service/ConversationService.java b/src/main/java/org/poolc/api/conversation/service/ConversationService.java index 15c93efd..8e4c915b 100644 --- a/src/main/java/org/poolc/api/conversation/service/ConversationService.java +++ b/src/main/java/org/poolc/api/conversation/service/ConversationService.java @@ -1,9 +1,9 @@ package org.poolc.api.conversation.service; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; -import org.poolc.api.comment.dto.CommentResponse; import org.poolc.api.conversation.domain.Conversation; import org.poolc.api.conversation.dto.ConversationCreateRequest; import org.poolc.api.conversation.dto.ConversationResponse; @@ -19,6 +19,7 @@ @Service @RequiredArgsConstructor public class ConversationService { + private static final String ANONYMOUS_NAME = "익명"; private final ConversationRepository conversationRepository; private final MemberService memberService; @@ -30,23 +31,29 @@ public ConversationResponse createConversation(String starterLoginID, Conversati conversationId = checkExistingConversation(starterLoginID, request.getOtherLoginID()); } if (conversationId != null) { - return ConversationResponse.of(conversationRepository.findById(conversationId).get()); + return getExistingConversationResponse(conversationId); } - Conversation conversation = new Conversation(new ConversationCreateValues(starterLoginID, request.getOtherLoginID(), request.isStarterAnonymous(), request.isOtherAnonymous())); - conversationRepository.save(conversation); - return ConversationResponse.of(conversation); + Conversation conversation = createNewConversation(starterLoginID, request); + return buildConversationResponse(conversation); } + @Transactional(readOnly = true) public ConversationResponse getConversationResponseById(String conversationId, String loginID) { Conversation conversation = findConversationById(conversationId, loginID); - return ConversationResponse.of(conversation); + String[] names = findNamesForConversation(conversation); + return ConversationResponse.of(conversation, names[0], names[1]); } @Transactional(readOnly = true) public List findAllConversationsForLoginID(String loginID) { return conversationRepository.findAllByStarterLoginIDOrOtherLoginID(loginID, loginID) - .stream().map(ConversationResponse::of) + .stream() + .sorted(Comparator.comparing(Conversation::getCreatedAt).reversed()) + .map(conversation -> { + String[] names = findNamesForConversation(conversation); + return ConversationResponse.of(conversation, names[0], names[1]); + }) .collect(Collectors.toList()); } @@ -76,6 +83,29 @@ public Conversation findConversationById(String conversationId, String loginID) return conversation; } + private Conversation createNewConversation(String starterLoginID, ConversationCreateRequest request) { + Conversation conversation = new Conversation( + new ConversationCreateValues(starterLoginID, request.getOtherLoginID(), request.isStarterAnonymous(), request.isOtherAnonymous()) + ); + conversationRepository.save(conversation); + return conversation; + } + private ConversationResponse buildConversationResponse(Conversation conversation) { + String[] names = findNamesForConversation(conversation); + return ConversationResponse.of(conversation, names[0], names[1]); + } + private ConversationResponse getExistingConversationResponse(String conversationId) { + Conversation conversation = conversationRepository.findById(conversationId).orElseThrow(() -> new NoSuchElementException("Conversation not found")); + String[] names = findNamesForConversation(conversation); + return ConversationResponse.of(conversation, names[0], names[1]); + } + private String[] findNamesForConversation(Conversation conversation) { + String starterName = ANONYMOUS_NAME, otherName = ANONYMOUS_NAME; + if (!conversation.isStarterAnonymous()) starterName = memberService.findNameByLoginID(conversation.getStarterLoginID()); + if (!conversation.isOtherAnonymous()) otherName = memberService.findNameByLoginID(conversation.getOtherLoginID()); + return new String[] {starterName, otherName}; + } + private void checkValidParties(String starterLoginID, String otherLoginID) { if (starterLoginID.equals(otherLoginID)) { throw new IllegalArgumentException("Sender and receiver cannot be the same person.");