Skip to content

Commit 72082cb

Browse files
committed
refactor: add logic on service
1 parent b0da4a3 commit 72082cb

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/main/java/com/back/domain/chatbot/service/ChatbotService.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.back.domain.chatbot.dto.ChatRequestDto;
44
import com.back.domain.chatbot.dto.ChatResponseDto;
5+
import com.back.domain.chatbot.dto.SaveBotMessageDto;
56
import com.back.domain.chatbot.dto.StepRecommendationResponseDto;
67
import com.back.domain.chatbot.entity.ChatConversation;
78
import com.back.domain.chatbot.enums.MessageSender;
@@ -22,7 +23,6 @@
2223
import org.springframework.core.io.Resource;
2324
import org.springframework.data.domain.Page;
2425
import org.springframework.data.domain.PageRequest;
25-
import org.springframework.data.domain.Pageable;
2626
import org.springframework.stereotype.Service;
2727
import org.springframework.transaction.annotation.Transactional;
2828
import org.springframework.util.StreamUtils;
@@ -133,6 +133,11 @@ public ChatResponseDto sendMessage(ChatRequestDto requestDto) {
133133
}
134134
}
135135

136+
// ============ 수정된 메서드들 ============
137+
138+
/**
139+
* 대화 컨텍스트 빌드 - 변경사항: sender로 구분하여 대화 재구성
140+
*/
136141
private String buildConversationContext(List<ChatConversation> recentChats) {
137142
if (recentChats.isEmpty()) {
138143
return "";
@@ -160,7 +165,7 @@ private String buildConversationContext(List<ChatConversation> recentChats) {
160165
* 대화 저장 - 변경사항: 사용자 메시지와 봇 응답을 각각 별도로 저장
161166
*/
162167
@Transactional
163-
public void saveConversation(ChatRequestDto requestDto, String response) {
168+
private void saveConversation(ChatRequestDto requestDto, String response) {
164169
// 1. 사용자 메시지 저장
165170
ChatConversation userMessage = ChatConversation.builder()
166171
.userId(requestDto.getUserId())
@@ -188,6 +193,51 @@ public List<ChatConversation> getUserChatHistory(Long userId) {
188193
return chatConversationRepository.findByUserIdOrderByCreatedAtDesc(userId);
189194
}
190195

196+
/**
197+
* FE에서 생성한 봇 메시지를 DB에 저장
198+
* 예: 인사말, 안내 메시지, 에러 메시지 등
199+
*/
200+
@Transactional
201+
public ChatConversation saveBotMessage(SaveBotMessageDto dto) {
202+
ChatConversation botMessage = ChatConversation.builder()
203+
.userId(dto.getUserId())
204+
.message(dto.getMessage())
205+
.sender(MessageSender.CHATBOT)
206+
.createdAt(LocalDateTime.now())
207+
.build();
208+
209+
return chatConversationRepository.save(botMessage);
210+
}
211+
212+
/**
213+
* 기본 인사말 생성 및 저장
214+
* 채팅 시작 시 호출하여 인사말을 DB에 저장
215+
*/
216+
@Transactional
217+
public ChatConversation createGreetingMessage(Long userId) {
218+
String greetingMessage = "안녕하세요! 🍹 바텐더 '쑤리'에요.\n" +
219+
"취향에 맞는 칵테일을 추천해드릴게요!\n" +
220+
"어떤 유형으로 찾아드릴까요?";
221+
222+
ChatConversation greeting = ChatConversation.builder()
223+
.userId(userId)
224+
.message(greetingMessage)
225+
.sender(MessageSender.CHATBOT)
226+
.createdAt(LocalDateTime.now())
227+
.build();
228+
229+
return chatConversationRepository.save(greeting);
230+
}
231+
232+
/**
233+
* 사용자의 첫 대화 여부 확인
234+
* 첫 대화인 경우 인사말 자동 생성에 활용 가능
235+
*/
236+
@Transactional(readOnly = true)
237+
public boolean isFirstConversation(Long userId) {
238+
return chatConversationRepository.findTop10ByUserIdOrderByCreatedAtDesc(userId).isEmpty();
239+
}
240+
191241
// ============ 기존 메서드들 (변경 없음) ============
192242

193243
private String buildSystemMessage(MessageType type) {

0 commit comments

Comments
 (0)