Skip to content

Commit

Permalink
Merge pull request #6 from PoolC/dev
Browse files Browse the repository at this point in the history
feat: modify loginID shown in conversation to member's name
  • Loading branch information
jimmy0006 authored Aug 6, 2024
2 parents 1929c7f + f652a68 commit b35edb9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@
public class ConversationResponse {

private String id;
private String starterLoginID;
private String otherLoginID;
private boolean starterAnonymous;
private boolean otherAnonymous;
private String starterName;
private String otherName;
private MessageResponse lastMessage;

protected ConversationResponse() {}
public static ConversationResponse of(Conversation conversation) {
public static ConversationResponse of(Conversation conversation, String starterName, String otherName) {
ConversationResponse response = new ConversationResponse();
response.setId(conversation.getId());
response.setStarterLoginID(conversation.getStarterLoginID());
response.setOtherLoginID(conversation.getOtherLoginID());
response.setStarterAnonymous(conversation.isStarterAnonymous());
response.setOtherAnonymous(conversation.isOtherAnonymous());
response.setStarterName(starterName);
response.setOtherName(otherName);
if (conversation.getLastMessage() != null) response.setLastMessage(MessageResponse.of(conversation.getLastMessage()));
return response;
}
Expand Down
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
6 changes: 6 additions & 0 deletions src/main/java/org/poolc/api/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ public List<Member> getAllMembersAndUpdateMemberIsExcepted() {
return members;
}

public String findNameByLoginID(String loginID) {
Member member = memberRepository.findByLoginID(loginID)
.orElseThrow(() -> new NoSuchElementException("No member with given login ID."));
return member.getName();
}

private String resetMemberPasswordToken(String email) {
Member resetMember = memberRepository.findByEmail(email)
.orElseThrow(() -> new NoSuchElementException("No user found with given email"));
Expand Down

0 comments on commit b35edb9

Please sign in to comment.