Skip to content

Commit

Permalink
Merge pull request #93 from KUSITMS-30th-TEAM-A/feature/#78/chatbot-r…
Browse files Browse the repository at this point in the history
…efactoring

[refactor] : Chatbot 도메인을 DDD 구조로 리팩토링한다
  • Loading branch information
bbbang105 authored Nov 27, 2024
2 parents 311213c + 4f60468 commit 859b0a3
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 81 deletions.
9 changes: 3 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ dependencies {
// MacOS Silicon 라이브러리 누락 문제
runtimeOnly 'io.netty:netty-resolver-dns-native-macos:4.1.104.Final:osx-aarch_64'


// Querydsl 추가
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
Expand All @@ -70,7 +69,6 @@ dependencies {

// S3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

}

java {
Expand All @@ -93,10 +91,9 @@ sourceSets {

tasks.named('compileJava') {
doFirst {
// QClass 파일 자동 삭제
def querydslGeneratedDir = layout.buildDirectory.dir("generated/sources/annotationProcessor/java/main").get().asFile
if (querydslGeneratedDir.exists()) {
querydslGeneratedDir.deleteDir()
def generatedDir = file("$build/generated/sources/annotationProcessor/java/main")
if (generatedDir.exists()) {
generatedDir.deleteDir() // 기존 Q 클래스 삭제
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.backend.chatbot.dto.request;
package kusitms.backend.chatbot.application.dto.request;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.backend.chatbot.dto.request;
package kusitms.backend.chatbot.application.dto.request;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.backend.chatbot.dto.request;
package kusitms.backend.chatbot.application.dto.request;


import jakarta.validation.constraints.NotBlank;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.backend.chatbot.dto.request;
package kusitms.backend.chatbot.application.dto.request;

public record MessageDto(
String role,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.backend.chatbot.dto.response;
package kusitms.backend.chatbot.application.dto.response;

public record ClovaChatbotAnswerDto(
ResultDto result
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.backend.chatbot.dto.response;
package kusitms.backend.chatbot.application.dto.response;

public record GetClovaChatbotAnswerResponseDto(
String answer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.backend.chatbot.dto.response;
package kusitms.backend.chatbot.application.dto.response;

public record GetGuideChatbotAnswerResponseDto(
String[] answers,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kusitms.backend.chatbot.dto.response;
package kusitms.backend.chatbot.application.dto.response;

import kusitms.backend.chatbot.dto.request.MessageDto;
import kusitms.backend.chatbot.application.dto.request.MessageDto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package kusitms.backend.chatbot.application;
package kusitms.backend.chatbot.application.factory;

import kusitms.backend.chatbot.dto.request.ChatbotRequestDto;
import kusitms.backend.chatbot.dto.request.ClovaRequestDto;
import kusitms.backend.chatbot.dto.request.MessageDto;
import kusitms.backend.chatbot.application.dto.request.ChatbotRequestDto;
import kusitms.backend.chatbot.application.dto.request.ClovaRequestDto;
import kusitms.backend.chatbot.application.dto.request.MessageDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kusitms.backend.chatbot.application;
package kusitms.backend.chatbot.application.factory;

import kusitms.backend.chatbot.domain.enums.Role;
import kusitms.backend.chatbot.dto.request.MessageDto;
import kusitms.backend.chatbot.application.dto.request.MessageDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package kusitms.backend.chatbot.application;
package kusitms.backend.chatbot.application.service;

import kusitms.backend.chatbot.application.factory.ClovaRequestFactory;
import kusitms.backend.chatbot.application.factory.MessageFactory;
import kusitms.backend.chatbot.application.dto.request.ChatbotRequestDto;
import kusitms.backend.chatbot.application.dto.response.GetClovaChatbotAnswerResponseDto;
import kusitms.backend.chatbot.domain.enums.*;
import kusitms.backend.chatbot.dto.response.GetGuideChatbotAnswerResponseDto;
import kusitms.backend.chatbot.application.dto.response.GetGuideChatbotAnswerResponseDto;
import kusitms.backend.chatbot.domain.service.ChatbotApiClient;
import kusitms.backend.chatbot.status.ChatbotErrorStatus;
import kusitms.backend.global.exception.CustomException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
@RequiredArgsConstructor
public class ChatbotService {
public class ChatbotApplicationService {
private final ChatbotApiClient chatbotApiClient;
private final ClovaRequestFactory clovaRequestFactory;
private final MessageFactory messageFactory;

/**
* 주어진 카테고리 이름, 경기장 이름, 질문 번호에 따라 답변을 조회합니다.
Expand Down Expand Up @@ -71,4 +80,19 @@ private <T extends Enum<T> & GuideAnswer> GetGuideChatbotAnswerResponseDto getAn
.map(answer -> GetGuideChatbotAnswerResponseDto.of(answer.getAnswers(), answer.getImgUrl()))
.orElseThrow(() -> new CustomException(ChatbotErrorStatus._NOT_FOUND_GUIDE_CHATBOT_ANSWER)); // 답변 없을 때 예외
}

/**
* Clova 챗봇 답변을 가져옵니다.
*
* @param message 사용자가 보낸 메시지
* @return Clova 챗봇 답변을 포함하는 Mono 객체
* @throws IllegalArgumentException 메시지가 null 또는 빈 문자열인 경우
*/
public Mono<GetClovaChatbotAnswerResponseDto> getClovaChatbotAnswer(String message) {
ChatbotRequestDto request = clovaRequestFactory.createClovaRequest();
request.getMessages().add(messageFactory.createUserMessage(message));

return chatbotApiClient.requestChatbot(request)
.map(GetClovaChatbotAnswerResponseDto::of);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kusitms.backend.chatbot.infrastructure;
package kusitms.backend.chatbot.domain.service;

import kusitms.backend.chatbot.dto.request.ChatbotRequestDto;
import kusitms.backend.chatbot.application.dto.request.ChatbotRequestDto;
import reactor.core.publisher.Mono;

public interface ChatbotApiClient {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package kusitms.backend.chatbot.infrastructure;
package kusitms.backend.chatbot.infra.adapter;

import kusitms.backend.chatbot.dto.request.ChatbotRequestDto;
import kusitms.backend.chatbot.dto.request.ClovaRequestDto;
import kusitms.backend.chatbot.dto.response.ClovaChatbotAnswerDto;
import kusitms.backend.chatbot.application.dto.request.ChatbotRequestDto;
import kusitms.backend.chatbot.application.dto.request.ClovaRequestDto;
import kusitms.backend.chatbot.application.dto.response.ClovaChatbotAnswerDto;
import kusitms.backend.chatbot.domain.service.ChatbotApiClient;
import kusitms.backend.chatbot.status.ChatbotErrorStatus;
import kusitms.backend.global.exception.CustomException;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kusitms.backend.global.config;
package kusitms.backend.chatbot.infra.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import kusitms.backend.chatbot.application.ChatbotService;
import kusitms.backend.chatbot.application.ClovaService;
import kusitms.backend.chatbot.dto.request.GetClovaChatbotAnswerRequestDto;
import kusitms.backend.chatbot.dto.response.GetClovaChatbotAnswerResponseDto;
import kusitms.backend.chatbot.dto.response.GetGuideChatbotAnswerResponseDto;
import kusitms.backend.chatbot.application.service.ChatbotApplicationService;
import kusitms.backend.chatbot.application.dto.request.GetClovaChatbotAnswerRequestDto;
import kusitms.backend.chatbot.application.dto.response.GetClovaChatbotAnswerResponseDto;
import kusitms.backend.chatbot.application.dto.response.GetGuideChatbotAnswerResponseDto;
import kusitms.backend.chatbot.status.ChatbotSuccessStatus;
import kusitms.backend.global.dto.ApiResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,8 +20,7 @@
@RequestMapping("/api/v1/chatbot")
@Validated
public class ChatbotController {
private final ChatbotService chatbotService;
private final ClovaService clovaService;
private final ChatbotApplicationService chatbotApplicationService;

/**
* 가이드 챗봇 답변 조회 API
Expand All @@ -42,7 +40,7 @@ public ResponseEntity<ApiResponse<GetGuideChatbotAnswerResponseDto>> getGuideCha
@RequestParam("categoryName") @NotBlank String categoryName,
@RequestParam("orderNumber") @Min(1) int orderNumber){

GetGuideChatbotAnswerResponseDto response = chatbotService.getGuideChatbotAnswer(stadiumName, categoryName, orderNumber);
GetGuideChatbotAnswerResponseDto response = chatbotApplicationService.getGuideChatbotAnswer(stadiumName, categoryName, orderNumber);

return ApiResponse.onSuccess(ChatbotSuccessStatus._GET_GUIDE_CHATBOT_ANSWER, response);
}
Expand All @@ -60,7 +58,7 @@ public ResponseEntity<ApiResponse<GetGuideChatbotAnswerResponseDto>> getGuideCha
public Mono<ResponseEntity<ApiResponse<GetClovaChatbotAnswerResponseDto>>> getClovaChatbotAnswer(
@Valid @RequestBody GetClovaChatbotAnswerRequestDto request) {

return clovaService.getClovaChatbotAnswer(request.message())
return chatbotApplicationService.getClovaChatbotAnswer(request.message())
.map(response -> ApiResponse.onSuccess(ChatbotSuccessStatus._GET_CLOVA_CHATBOT_ANSWER, response));
}
}
16 changes: 6 additions & 10 deletions src/test/java/kusitms/backend/chatbot/ChatbotControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper;
import com.epages.restdocs.apispec.ResourceSnippetParameters;
import com.epages.restdocs.apispec.Schema;
import kusitms.backend.chatbot.application.ChatbotService;
import kusitms.backend.chatbot.application.ClovaService;
import kusitms.backend.chatbot.dto.response.GetClovaChatbotAnswerResponseDto;
import kusitms.backend.chatbot.dto.response.GetGuideChatbotAnswerResponseDto;
import kusitms.backend.chatbot.application.dto.response.GetClovaChatbotAnswerResponseDto;
import kusitms.backend.chatbot.application.dto.response.GetGuideChatbotAnswerResponseDto;
import kusitms.backend.chatbot.application.service.ChatbotApplicationService;
import kusitms.backend.chatbot.presentation.ChatbotController;
import kusitms.backend.configuration.ControllerTestConfig;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -34,10 +33,7 @@
public class ChatbotControllerTest extends ControllerTestConfig {

@MockBean
private ChatbotService chatbotService;

@MockBean
private ClovaService clovaService;
private ChatbotApplicationService chatbotApplicationService;

@Test
@DisplayName("가이드 챗봇 답변 조회")
Expand All @@ -51,7 +47,7 @@ public void getGuideChatbotAnswer() throws Exception {
"종합운동장역을 나가기 전, 역사에 위치한 ‘라커디움파크 종합운동장역점’에서도 굿즈를 판매 중이에요!"
}, null);

Mockito.when(chatbotService.getGuideChatbotAnswer(anyString(), anyString(), anyInt()))
Mockito.when(chatbotApplicationService.getGuideChatbotAnswer(anyString(), anyString(), anyInt()))
.thenReturn(response);

// when
Expand Down Expand Up @@ -105,7 +101,7 @@ public void getClovaChatbotAnswer() throws Exception {
GetClovaChatbotAnswerResponseDto response = new GetClovaChatbotAnswerResponseDto(
"안녕하세요! 저는 야구 가이드 챗봇 '루키'에요! 야구에 대한 궁금한 점이 있다면 언제든지 물어봐 주세요!");

Mockito.when(clovaService.getClovaChatbotAnswer(anyString()))
Mockito.when(chatbotApplicationService.getClovaChatbotAnswer(anyString()))
.thenReturn(Mono.just(response));

String clovaChatbotAnswerJsonRequest = """
Expand Down

0 comments on commit 859b0a3

Please sign in to comment.