Skip to content

Commit

Permalink
refactor(ConversationService): modularize big chunks of code & reflec…
Browse files Browse the repository at this point in the history
…t front end request
  • Loading branch information
becooq81 committed Aug 5, 2024
1 parent 8ce5bbb commit 211af3f
Showing 1 changed file with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +19,7 @@
@Service
@RequiredArgsConstructor
public class ConversationService {
private static final String ANONYMOUS_NAME = "익명";
private final ConversationRepository conversationRepository;
private final MemberService memberService;

Expand All @@ -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<ConversationResponse> 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());
}

Expand Down Expand Up @@ -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.");
Expand Down

0 comments on commit 211af3f

Please sign in to comment.