Skip to content

Commit

Permalink
feat: connect with lambda in generate First Question API
Browse files Browse the repository at this point in the history
람다와 통신해서 첫번째 질문 생성하는 API 완료
  • Loading branch information
Relaxed-Mind committed Dec 9, 2023
1 parent 424f927 commit ddec471
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class QuestionController {
private final QuestionService questionService;

@GetMapping("/questions/{bookDiaryId}")
@Operation(summary = "[미완성] 질문 및 답변 가져오기", description = "질문 및 답변을 가져옵니다. [답변이 없으면 빈 문자열 반환]")
@Operation(summary = "질문 및 답변 가져오기", description = "질문 및 답변을 가져옵니다. [답변이 없으면 빈 문자열 반환]")
private ResponseEntity<Map<String, Object>> getQuestionsAndAnswers(@PathVariable Long bookDiaryId){
return ResponseEntity
.ok()
.body(questionService.getQuestionsAndAnswers(bookDiaryId));
}

@PostMapping("/questions/first/{bookDiaryId}")
@Operation(summary = "[미완성] 1차 질문 생성", description = "스크랩을 바탕으로 1차 질문을 생성합니다.")
@Operation(summary = "1차 질문 생성", description = "스크랩을 바탕으로 1차 질문을 생성합니다. [생성된 questionId List 반환]")
private ResponseEntity<Map<String, Object>> generateFirstQuestions(@PathVariable Long bookDiaryId){
return ResponseEntity
.ok()
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/capstone/bookdiary/domain/dto/FeignQuestionDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package capstone.bookdiary.domain.dto;

import lombok.Getter;
import org.json.JSONObject;

@Getter
public class FeignQuestionDto {
private Integer statusCode;
private JSONObject body;
}
9 changes: 9 additions & 0 deletions src/main/java/capstone/bookdiary/domain/entity/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
public class Question extends BaseTimeEntity{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long questionId;
Expand All @@ -24,6 +26,13 @@ public class Question extends BaseTimeEntity{

private Integer degree;

public Question(BookDiary bookDiary, String question, Integer degree) {
this.bookDiary = bookDiary;
this.question = question;
this.degree = degree;
this.answer = "";
}

public void answerQuestion(String answer){
this.answer = answer;
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/capstone/bookdiary/feign/FirstQuestionClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package capstone.bookdiary.feign;

import capstone.bookdiary.domain.dto.FeignQuestionDto;
import java.util.Map;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name = "firstQuestion", url = "https://6abaok06nf.execute-api.ap-northeast-2.amazonaws.com/TexttoText1/TextLambdaFunction1")
public interface FirstQuestionClient {
@PostMapping
FeignQuestionDto getFirstQuestion(@RequestBody Map<String, Object> scrapMemo);
}
28 changes: 23 additions & 5 deletions src/main/java/capstone/bookdiary/service/QuestionService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package capstone.bookdiary.service;

import capstone.bookdiary.domain.dto.FeignQuestionDto;
import capstone.bookdiary.domain.dto.QuestionAndAnswerDto;
import capstone.bookdiary.domain.dto.QuestionAndAnswerListDto;
import capstone.bookdiary.domain.dto.QuestionDto;
import capstone.bookdiary.domain.dto.ScrapResponseDto;
import capstone.bookdiary.domain.entity.BookDiary;
import capstone.bookdiary.domain.entity.Question;
import capstone.bookdiary.exception.exceptions.DataNotFoundException;
import capstone.bookdiary.feign.FirstQuestionClient;
import capstone.bookdiary.repository.BookDiaryRepository;
import capstone.bookdiary.repository.QuestionRepository;
import capstone.bookdiary.repository.ScrapRepository;
Expand All @@ -15,6 +17,7 @@
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -24,26 +27,41 @@ public class QuestionService {
private final ScrapRepository scrapRepository;
private final QuestionRepository questionRepository;
private final BookDiaryRepository bookDiaryRepository;
private final FirstQuestionClient firstQuestionClient;

public Map<String, Object> generateFirstQuestions(Long bookDiaryId){
//키워드 받아오기
// Map<String, Object> json = keywordClient.getKeyword(new KeywordParam(libraryKey,"json"));
// System.out.println("json.get(\"response\") = " + json.get("response"));
BookDiary bookDiary = bookDiaryRepository.findById(bookDiaryId)
.orElseThrow(DataNotFoundException::new);

//스크랩 받아오기
List<ScrapResponseDto> scrapList = scrapRepository.findByBookDiary(bookDiaryId);
StringBuilder scrapMemo = new StringBuilder();
for (ScrapResponseDto scrapResponseDto : scrapList) {
scrapMemo.append("\"").append(scrapResponseDto.getContent()).append("\"").append("[").append(scrapResponseDto.getMemo()).append("] ");
}
System.out.println(scrapMemo);

//TODO: AI로 전송
Map<String, Object> data = new HashMap<>();
data.put("scrapMemo", scrapMemo);
data.put("scrapMemo", scrapMemo.toString());
//scrapMemo에 담아서 AI로 전송
FeignQuestionDto firstQuestion = firstQuestionClient.getFirstQuestion(data);
JSONObject body = firstQuestion.getBody();

//받아온 결과 파싱해서 저장
List<Long> questionIds = new ArrayList<>();

String questionString = (String)body.get("gpt_generated_question");
String[] questionStrings = questionString.split("\n");
for (String qs : questionStrings) {
Question savedQuestion = questionRepository.save(new Question(bookDiary, qs, 1));
questionIds.add(savedQuestion.getQuestionId());
}

//TODO: 응답 받아서 질문들 리턴
return data;
Map<String, Object> response = new HashMap<>();
response.put("questionIds", questionIds);
return response;
}

public Map<String, Object> generateSecondQuestions(Long bookDiaryId) {
Expand Down

0 comments on commit ddec471

Please sign in to comment.