Skip to content

Commit b35edb9

Browse files
authored
Merge pull request #6 from PoolC/dev
feat: modify loginID shown in conversation to member's name
2 parents 1929c7f + f652a68 commit b35edb9

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/main/java/org/poolc/api/conversation/dto/ConversationResponse.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,16 @@
1111
public class ConversationResponse {
1212

1313
private String id;
14-
private String starterLoginID;
15-
private String otherLoginID;
16-
private boolean starterAnonymous;
17-
private boolean otherAnonymous;
14+
private String starterName;
15+
private String otherName;
1816
private MessageResponse lastMessage;
1917

2018
protected ConversationResponse() {}
21-
public static ConversationResponse of(Conversation conversation) {
19+
public static ConversationResponse of(Conversation conversation, String starterName, String otherName) {
2220
ConversationResponse response = new ConversationResponse();
2321
response.setId(conversation.getId());
24-
response.setStarterLoginID(conversation.getStarterLoginID());
25-
response.setOtherLoginID(conversation.getOtherLoginID());
26-
response.setStarterAnonymous(conversation.isStarterAnonymous());
27-
response.setOtherAnonymous(conversation.isOtherAnonymous());
22+
response.setStarterName(starterName);
23+
response.setOtherName(otherName);
2824
if (conversation.getLastMessage() != null) response.setLastMessage(MessageResponse.of(conversation.getLastMessage()));
2925
return response;
3026
}

src/main/java/org/poolc/api/conversation/service/ConversationService.java

+37-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.poolc.api.conversation.service;
22

3+
import java.util.Comparator;
34
import java.util.List;
45
import java.util.stream.Collectors;
56
import lombok.RequiredArgsConstructor;
6-
import org.poolc.api.comment.dto.CommentResponse;
77
import org.poolc.api.conversation.domain.Conversation;
88
import org.poolc.api.conversation.dto.ConversationCreateRequest;
99
import org.poolc.api.conversation.dto.ConversationResponse;
@@ -19,6 +19,7 @@
1919
@Service
2020
@RequiredArgsConstructor
2121
public class ConversationService {
22+
private static final String ANONYMOUS_NAME = "익명";
2223
private final ConversationRepository conversationRepository;
2324
private final MemberService memberService;
2425

@@ -30,23 +31,29 @@ public ConversationResponse createConversation(String starterLoginID, Conversati
3031
conversationId = checkExistingConversation(starterLoginID, request.getOtherLoginID());
3132
}
3233
if (conversationId != null) {
33-
return ConversationResponse.of(conversationRepository.findById(conversationId).get());
34+
return getExistingConversationResponse(conversationId);
3435
}
35-
Conversation conversation = new Conversation(new ConversationCreateValues(starterLoginID, request.getOtherLoginID(), request.isStarterAnonymous(), request.isOtherAnonymous()));
36-
conversationRepository.save(conversation);
37-
return ConversationResponse.of(conversation);
36+
Conversation conversation = createNewConversation(starterLoginID, request);
37+
return buildConversationResponse(conversation);
3838
}
3939

40+
4041
@Transactional(readOnly = true)
4142
public ConversationResponse getConversationResponseById(String conversationId, String loginID) {
4243
Conversation conversation = findConversationById(conversationId, loginID);
43-
return ConversationResponse.of(conversation);
44+
String[] names = findNamesForConversation(conversation);
45+
return ConversationResponse.of(conversation, names[0], names[1]);
4446
}
4547

4648
@Transactional(readOnly = true)
4749
public List<ConversationResponse> findAllConversationsForLoginID(String loginID) {
4850
return conversationRepository.findAllByStarterLoginIDOrOtherLoginID(loginID, loginID)
49-
.stream().map(ConversationResponse::of)
51+
.stream()
52+
.sorted(Comparator.comparing(Conversation::getCreatedAt).reversed())
53+
.map(conversation -> {
54+
String[] names = findNamesForConversation(conversation);
55+
return ConversationResponse.of(conversation, names[0], names[1]);
56+
})
5057
.collect(Collectors.toList());
5158
}
5259

@@ -76,6 +83,29 @@ public Conversation findConversationById(String conversationId, String loginID)
7683
return conversation;
7784
}
7885

86+
private Conversation createNewConversation(String starterLoginID, ConversationCreateRequest request) {
87+
Conversation conversation = new Conversation(
88+
new ConversationCreateValues(starterLoginID, request.getOtherLoginID(), request.isStarterAnonymous(), request.isOtherAnonymous())
89+
);
90+
conversationRepository.save(conversation);
91+
return conversation;
92+
}
93+
private ConversationResponse buildConversationResponse(Conversation conversation) {
94+
String[] names = findNamesForConversation(conversation);
95+
return ConversationResponse.of(conversation, names[0], names[1]);
96+
}
97+
private ConversationResponse getExistingConversationResponse(String conversationId) {
98+
Conversation conversation = conversationRepository.findById(conversationId).orElseThrow(() -> new NoSuchElementException("Conversation not found"));
99+
String[] names = findNamesForConversation(conversation);
100+
return ConversationResponse.of(conversation, names[0], names[1]);
101+
}
102+
private String[] findNamesForConversation(Conversation conversation) {
103+
String starterName = ANONYMOUS_NAME, otherName = ANONYMOUS_NAME;
104+
if (!conversation.isStarterAnonymous()) starterName = memberService.findNameByLoginID(conversation.getStarterLoginID());
105+
if (!conversation.isOtherAnonymous()) otherName = memberService.findNameByLoginID(conversation.getOtherLoginID());
106+
return new String[] {starterName, otherName};
107+
}
108+
79109
private void checkValidParties(String starterLoginID, String otherLoginID) {
80110
if (starterLoginID.equals(otherLoginID)) {
81111
throw new IllegalArgumentException("Sender and receiver cannot be the same person.");

src/main/java/org/poolc/api/member/service/MemberService.java

+6
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ public List<Member> getAllMembersAndUpdateMemberIsExcepted() {
226226
return members;
227227
}
228228

229+
public String findNameByLoginID(String loginID) {
230+
Member member = memberRepository.findByLoginID(loginID)
231+
.orElseThrow(() -> new NoSuchElementException("No member with given login ID."));
232+
return member.getName();
233+
}
234+
229235
private String resetMemberPasswordToken(String email) {
230236
Member resetMember = memberRepository.findByEmail(email)
231237
.orElseThrow(() -> new NoSuchElementException("No user found with given email"));

0 commit comments

Comments
 (0)