1
1
package org .poolc .api .conversation .service ;
2
2
3
+ import java .util .Comparator ;
3
4
import java .util .List ;
4
5
import java .util .stream .Collectors ;
5
6
import lombok .RequiredArgsConstructor ;
6
- import org .poolc .api .comment .dto .CommentResponse ;
7
7
import org .poolc .api .conversation .domain .Conversation ;
8
8
import org .poolc .api .conversation .dto .ConversationCreateRequest ;
9
9
import org .poolc .api .conversation .dto .ConversationResponse ;
19
19
@ Service
20
20
@ RequiredArgsConstructor
21
21
public class ConversationService {
22
+ private static final String ANONYMOUS_NAME = "익명" ;
22
23
private final ConversationRepository conversationRepository ;
23
24
private final MemberService memberService ;
24
25
@@ -30,23 +31,29 @@ public ConversationResponse createConversation(String starterLoginID, Conversati
30
31
conversationId = checkExistingConversation (starterLoginID , request .getOtherLoginID ());
31
32
}
32
33
if (conversationId != null ) {
33
- return ConversationResponse . of ( conversationRepository . findById ( conversationId ). get () );
34
+ return getExistingConversationResponse ( conversationId );
34
35
}
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 );
38
38
}
39
39
40
+
40
41
@ Transactional (readOnly = true )
41
42
public ConversationResponse getConversationResponseById (String conversationId , String loginID ) {
42
43
Conversation conversation = findConversationById (conversationId , loginID );
43
- return ConversationResponse .of (conversation );
44
+ String [] names = findNamesForConversation (conversation );
45
+ return ConversationResponse .of (conversation , names [0 ], names [1 ]);
44
46
}
45
47
46
48
@ Transactional (readOnly = true )
47
49
public List <ConversationResponse > findAllConversationsForLoginID (String loginID ) {
48
50
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
+ })
50
57
.collect (Collectors .toList ());
51
58
}
52
59
@@ -76,6 +83,29 @@ public Conversation findConversationById(String conversationId, String loginID)
76
83
return conversation ;
77
84
}
78
85
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
+
79
109
private void checkValidParties (String starterLoginID , String otherLoginID ) {
80
110
if (starterLoginID .equals (otherLoginID )) {
81
111
throw new IllegalArgumentException ("Sender and receiver cannot be the same person." );
0 commit comments