From 2f44b9adeb1a51b78201d1728093e6244c442f27 Mon Sep 17 00:00:00 2001 From: jhsong76 <20190976@sungshin.ac.kr> Date: Wed, 3 Jan 2024 06:17:52 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20api=20=EC=B6=94=EA=B0=80=20=EA=B0=9C?= =?UTF-8?q?=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/domain/keyword/ChatService.java | 25 ++++++++++--- .../demo/domain/keyword/GptController.java | 8 ++--- .../domain/keyword/KeywordController.java | 36 +++++++++++++++++++ .../domain/keyword/KeywordRespository.java | 3 ++ 4 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/hackerton/demo/domain/keyword/KeywordController.java diff --git a/src/main/java/com/hackerton/demo/domain/keyword/ChatService.java b/src/main/java/com/hackerton/demo/domain/keyword/ChatService.java index 0008b4e..888c1ee 100644 --- a/src/main/java/com/hackerton/demo/domain/keyword/ChatService.java +++ b/src/main/java/com/hackerton/demo/domain/keyword/ChatService.java @@ -1,7 +1,10 @@ package com.hackerton.demo.domain.keyword; +import com.hackerton.demo.domain.User.User; +import com.hackerton.demo.domain.User.UserRepository; import com.theokanning.openai.completion.chat.ChatCompletionChoice; import com.theokanning.openai.service.OpenAiService; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -25,8 +28,9 @@ public class ChatService { private final KeywordRespository keywordRespository; private final PlaceRepository placeRepository; + private final UserRepository userRepository; - public List createGptComment(String content) { + public List createGptComment(String content, Long userId) { this.openAiService = new OpenAiService(apiKey, Duration.ofSeconds(20)); String prompt = content + "대신 딱 10개만 아무런 부가 설명 없이 키워드만 알려줘."; @@ -41,10 +45,10 @@ public List createGptComment(String content) { ChatCompletionChoice chatCompletionResult = openAiService.createChatCompletion(requester).getChoices().get(0); String contentResult = chatCompletionResult.getMessage().getContent(); - return extractKeywords(contentResult); + return extractKeywords(contentResult, userId); } - private List extractKeywords(String content) { + private List extractKeywords(String content, Long userId) { List keywordDtos = new ArrayList<>(); // 개행 문자로 분할하여 리스트로 변환 @@ -56,16 +60,18 @@ private List extractKeywords(String content) { keywordDtos.add(keywordDto); } - List keywords = keywordRespository.saveAll(convertToKeywords(keywordDtos)); + List keywords = keywordRespository.saveAll(convertToKeywords(keywordDtos, userId)); return convertToKeywordDtos(keywords); } - private List convertToKeywords(List keywordDtos) { + private List convertToKeywords(List keywordDtos, Long userId) { + User user = userRepository.getReferenceById(userId); List keywords = new ArrayList<>(); for (KeywordDto dto : keywordDtos) { Keyword keyword = new Keyword(); keyword.setOptionTitle(dto.getOptionTitle()); + keyword.setUser(user); keywords.add(keyword); } return keywords; @@ -138,4 +144,13 @@ private List convertToPlaceDtos(List places) { return placeDtos; } + public List getKeywordList(Long userId) { + List keywords = keywordRespository.findAllByUserId(userId); + List keywordDtos = keywords.stream() + .map(keyword -> new KeywordDto(keyword.getOptionTitle())) + .collect(Collectors.toList()); + + return keywordDtos; + } + } \ No newline at end of file diff --git a/src/main/java/com/hackerton/demo/domain/keyword/GptController.java b/src/main/java/com/hackerton/demo/domain/keyword/GptController.java index 20a9904..ac46463 100644 --- a/src/main/java/com/hackerton/demo/domain/keyword/GptController.java +++ b/src/main/java/com/hackerton/demo/domain/keyword/GptController.java @@ -18,13 +18,13 @@ public class GptController { private final ChatService chatService; - @GetMapping("/result") - @Operation(summary = "음성인식/텍스트 ChatGPT로 전달하여 키워드 추출", description = "음성인식/텍스트를 통해 ChatGPT 검색을 실행합니다. " + "검색 결과를 List로 반환하여 Keyword에 저장합니다.") + @GetMapping("/result/{userId}") + @Operation(summary = "음성인식/텍스트 ChatGPT로 전달하여 키워드 추출", description = "음성인식/텍스트를 통해 ChatGPT 검색을 실행합니다. " + "검색 결과를 List로 반환하여 Keyword와 pathvariable을 저장합니다.") @ApiResponses({ @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "GET_SUCCESS", description = "조회 성공"), }) - public ResponseEntity>> createGptComment(@RequestParam String content) { - List keywordDtos = chatService.createGptComment(content); + public ResponseEntity>> createGptComment(@RequestParam(name = "content") String content, @PathVariable(name = "userId")Long userId) { + List keywordDtos = chatService.createGptComment(content, userId); ApiResponse> apiResponse = new ApiResponse<>(ApiResponseStatus.GET_SUCCESS, keywordDtos); return ResponseEntity.ok(apiResponse); } diff --git a/src/main/java/com/hackerton/demo/domain/keyword/KeywordController.java b/src/main/java/com/hackerton/demo/domain/keyword/KeywordController.java new file mode 100644 index 0000000..2e7b8f9 --- /dev/null +++ b/src/main/java/com/hackerton/demo/domain/keyword/KeywordController.java @@ -0,0 +1,36 @@ +package com.hackerton.demo.domain.keyword; + +import com.hackerton.demo.domain.common.ApiResponse; +import com.hackerton.demo.domain.common.ApiResponseStatus; +import io.swagger.v3.oas.annotations.Operation; + +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import java.util.List; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +@Slf4j +@RequestMapping("/keyword") +public class KeywordController { + private final ChatService chatService; + + @GetMapping("/{userId}") + @Operation(summary = "해당 유저의 오늘의 키워드 조회 ", description = "pathvariable로 들어오는 유저의 키워드를 조회합니다." + + "토큰 미구현으로 1~6번 인덱스 유저 조회 가능.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "GET_SUCCESS", description = "조회 성공"), + }) + public ResponseEntity>> getKeyword(@PathVariable(name = "userId") Long userId) { + List keywordDtos = chatService.getKeywordList(userId); + ApiResponse> apiResponse = new ApiResponse<>(ApiResponseStatus.GET_SUCCESS, keywordDtos); + return ResponseEntity.ok(apiResponse); + } + +} diff --git a/src/main/java/com/hackerton/demo/domain/keyword/KeywordRespository.java b/src/main/java/com/hackerton/demo/domain/keyword/KeywordRespository.java index ac8313f..0c7f1b6 100644 --- a/src/main/java/com/hackerton/demo/domain/keyword/KeywordRespository.java +++ b/src/main/java/com/hackerton/demo/domain/keyword/KeywordRespository.java @@ -1,8 +1,11 @@ package com.hackerton.demo.domain.keyword; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface KeywordRespository extends JpaRepository { + + List findAllByUserId(Long userId); }