Skip to content

Commit

Permalink
Merge pull request #90 from alsrudrl1220/refactor/#88
Browse files Browse the repository at this point in the history
[refactor] 동화 내용 API 리팩토링
  • Loading branch information
alsrudrl1220 authored Jun 5, 2024
2 parents 3d7870e + 0eba41f commit d79049e
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 122 deletions.
2 changes: 1 addition & 1 deletion BE/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ dependencies {

tasks.named('test') {
useJUnitPlatform()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.FTIsland.BE.controller;
package com.FTIsland.BE.book.content.controller;

import com.FTIsland.BE.dto.BookContentDTO;
import com.FTIsland.BE.dto.BookInfoDTO;
import com.FTIsland.BE.service.BookContentService;
import com.FTIsland.BE.book.content.service.BookContentService;
import com.FTIsland.BE.book.content.dto.BookContentRequest;
import com.FTIsland.BE.book.content.dto.BookContentResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
Expand All @@ -21,8 +21,8 @@ public void saveBookContent(){ // DB에 동화 정보 저장
}

@PostMapping("/book/content")
public List<BookContentDTO> getBookInfoById(@RequestBody BookContentDTO bookContentDTO){
return bookContentService.findByBookId(bookContentDTO); // 해당 id의 동화 내용 list
public List<BookContentResponse> getBookInfoById(@RequestBody BookContentRequest bookContentRequest){
return bookContentService.findByBookId(bookContentRequest); // 해당 id의 동화 내용 list
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.FTIsland.BE.book.content.dto;

import lombok.*;

@Getter
@NoArgsConstructor
public class BookContentRequest {
private Integer bookId; // BookInfo에 title 있음.
private String mainLan;
private String subLan;

@Builder
public BookContentRequest(Integer bookId, String mainLan, String subLan){
this.bookId = bookId;
this.mainLan = mainLan;
this.subLan = subLan;
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.FTIsland.BE.book.content.dto;

import com.FTIsland.BE.book.content.entity.BookContentEntity;
import com.FTIsland.BE.dto.ContentVocaDTO;
import lombok.*;

import java.util.List;

@Getter
@NoArgsConstructor
public class BookContentResponse {
private Integer bookId; //bookName도 추가?
private Integer page;
private String mainLan;
private String subLan;
private String korContents;
private String mainContents;
private String subContents;
private List<ContentVocaDTO> vocaList;
private String image;

@Builder
public BookContentResponse(Integer bookId, Integer page, String mainLan, String subLan, String korContents, String mainContents, String subContents, List<ContentVocaDTO> vocaList, String image){
this.bookId = bookId;
this.page = page;
this.mainLan = mainLan;
this.subLan = subLan;
this.korContents = korContents;
this.mainContents = mainContents;
this.subContents = subContents;
this.vocaList = vocaList;
this.image = image;
}

public BookContentEntity toBookContentEntity(){
return BookContentEntity.builder()
.bookId(bookId)
.page(page)
.korContents(korContents)
.image(image)
.build();
}

}



Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.FTIsland.BE.book.content.entity;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Getter
@Table(name = "bookcontent")
@NoArgsConstructor
public class BookContentEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

//@ManyToOne
//@JoinColumn(name = "book_id")
//private BookInfoEntity book;

@Column
private Integer bookId;

@Column
private Integer page;

@Column
private String korContents;

@Column
private String image;

@Builder
public BookContentEntity(Integer bookId, Integer page, String korContents, String image){
this.bookId = bookId;
this.page = page;
this.korContents = korContents;
this.image = image;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.FTIsland.BE.repository;
package com.FTIsland.BE.book.content.repository;

import com.FTIsland.BE.entity.BookContentEntity;
import com.FTIsland.BE.book.content.entity.BookContentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.FTIsland.BE.service;
package com.FTIsland.BE.book.content.service;

import com.FTIsland.BE.book.content.entity.BookContentEntity;
import com.FTIsland.BE.book.content.dto.BookContentRequest;
import com.FTIsland.BE.book.content.dto.BookContentResponse;
import com.FTIsland.BE.dto.BookContentDTO;
import com.FTIsland.BE.dto.BookInfoDTO;
import com.FTIsland.BE.dto.ContentVocaDTO;
import com.FTIsland.BE.entity.BookContentEntity;
import com.FTIsland.BE.entity.BookInfoEntity;
import com.FTIsland.BE.entity.VocaEntity;
import com.FTIsland.BE.repository.BookContentRepository;
import com.FTIsland.BE.repository.BookInfoRepository;
import com.FTIsland.BE.book.content.repository.BookContentRepository;
import com.FTIsland.BE.repository.VocaRepository;
import com.google.cloud.translate.*;
import com.FTIsland.BE.service.TranslationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
Expand All @@ -26,9 +24,9 @@ public class BookContentService {
private final TranslationService translationService;
private final VocaRepository vocaRepository;

public void save() { // 동화 내용 저장 (임시로 만든 method)
public void save() { // 동화 내용 저장 (임시로 만든 method) -> TODO: seed data 저장하는 구문 작성 후 메서드 삭제 예정 (toEntity 관련 에러 해결)
ArrayList<BookContentDTO> bookContentDTOS = new ArrayList<>();

// 금도끼 은도끼 저장
bookContentDTOS.add(new BookContentDTO(1, 0, "","","옛날 어느 마을에 정직하고 마음씨 착한 나무꾼이 살고 있었어요.","", "", "https://storage.googleapis.com/ft-island-image/goldsilver0.webp"));
bookContentDTOS.add(new BookContentDTO(1, 1, "","","어느 날 나무꾼이 나무를 베고 있었는데, 도끼가 연못에 빠지고 말았어요.","", "", "https://storage.googleapis.com/ft-island-image/goldsilver1.webp"));
Expand Down Expand Up @@ -64,42 +62,36 @@ public void save() { // 동화 내용 저장 (임시로 만든 method)
bookContentDTOS.add(new BookContentDTO(2, 3, "","","친화력이 좋은 치치는 늑대와 바로 친해졌고, 함께 그림자 놀이를 하며 놀았어요.","", "", "https://storage.googleapis.com/ft-island-image/shadow2.webp"));
bookContentDTOS.add(new BookContentDTO(2, 4, "","","여우를 사냥하려고 숲 주변에 있던 사냥꾼이, 치치와 늑대의 그림자를 보고 줄행랑 쳤답니다!","", "", "https://storage.googleapis.com/ft-island-image/shadow3.webp"));




for (BookContentDTO bookContentDTO : bookContentDTOS) {
BookContentEntity bookContentEntity = BookContentEntity.toBookContentEntity(bookContentDTO);
BookContentEntity bookContentEntity = BookContentEntity.toBookContentEntity(bookContentDTO); // TODO: seed data 저장 코드 작성 후 없애기
bookContentRepository.save(bookContentEntity);
}
}

public List<BookContentDTO> findByBookId(BookContentDTO requestDTO) {
public List<BookContentResponse> findByBookId(BookContentRequest requestDTO) {
// bookId를 통한 BookContentEntity 조회
Integer bookId = requestDTO.getBookId();

List<BookContentEntity> byBookId = bookContentRepository.findByBookId(bookId);
List<BookContentDTO> bookContentDTOS = new ArrayList<>();

String selectedMainLanguage = "ko";
String selectedSubLanguage = "ko";
// return값 정의
List<BookContentResponse> bookContentResponses = new ArrayList<>();

// 번역 로직 - 한 페이지(한 줄)씩 번역이라서 for문 사용
for(BookContentEntity ent : byBookId){
// 번역 로직

// 1. 주, 보조 언어 텍스트 둘 다 한국어를 기본으로 설정
// 1. 주, 보조 언어 텍스트(내용 한 줄) 둘 다 한국어를 기본으로 설정
String mainText = ent.getKorContents();
String subText = ent.getKorContents();

// 2. 요청한 언어에 맞게 번역
if (!requestDTO.getMainLan().equals("ko")){ // 주 언어 한국어가 아니라면 번역
selectedMainLanguage = requestDTO.getMainLan();
mainText = translationService.test(selectedMainLanguage, mainText);
mainText = translationService.test(requestDTO.getMainLan(), mainText);
}
if (!requestDTO.getSubLan().equals("ko")){ // 보조 언어가 한국어가 아니라면 번역
selectedSubLanguage = requestDTO.getSubLan();
subText = translationService.test(selectedSubLanguage, subText);
subText = translationService.test(requestDTO.getSubLan(), subText);
}

// 해당 페이지의 vocaId, word, subWord 가져오기
// 3. 해당 페이지의 vocaId, word, subWord 가져오기
var vocaEntities = vocaRepository.findByBookIdAndPage(ent.getBookId(), ent.getPage());
List<ContentVocaDTO> contentVocaDTOS = new ArrayList<>();

Expand All @@ -111,38 +103,35 @@ public List<BookContentDTO> findByBookId(BookContentDTO requestDTO) {
String subWord = entity.getWord();

if (!requestDTO.getMainLan().equals("ko")){ // 주 언어 한국어가 아니라면 번역
selectedMainLanguage = requestDTO.getMainLan();
mainWord = translationService.test(selectedMainLanguage, mainWord);
mainWord = translationService.test(requestDTO.getMainLan(), mainWord);
}
if (!requestDTO.getSubLan().equals("ko")){ // 보조 언어가 한국어가 아니라면 번역
selectedSubLanguage = requestDTO.getSubLan();
subWord = translationService.test(selectedSubLanguage, subWord);
subWord = translationService.test(requestDTO.getSubLan(), subWord);
}
contentVocaDTO.setWord(mainWord);
contentVocaDTO.setSubWord(subWord);
contentVocaDTOS.add(contentVocaDTO);
}



// DTO List에 추가
BookContentDTO bookContentDTO = new BookContentDTO(
ent.getBookId(),
ent.getPage(),
selectedMainLanguage,
selectedSubLanguage,
ent.getKorContents(),
mainText,
subText,
ent.getImage()
);
bookContentDTO.setVocaList(contentVocaDTOS);
bookContentDTOS.add(bookContentDTO);
BookContentResponse bookContentResponse = new BookContentResponse().builder()
.bookId(ent.getBookId())
.page(ent.getPage())
.mainLan(requestDTO.getMainLan())
.subLan(requestDTO.getSubLan())
.korContents(ent.getKorContents())
.mainContents(mainText)
.subContents(subText)
.vocaList(contentVocaDTOS)
.image(ent.getImage())
.build();

bookContentResponses.add(bookContentResponse);
}

// page 순으로 정렬
bookContentDTOS = bookContentDTOS.stream().sorted(Comparator.comparing(BookContentDTO::getPage)).collect(Collectors.toList());
bookContentResponses = bookContentResponses.stream().sorted(Comparator.comparing(BookContentResponse::getPage)).collect(Collectors.toList());

return bookContentDTOS;
return bookContentResponses;
}
}
39 changes: 20 additions & 19 deletions BE/src/main/java/com/FTIsland/BE/controller/QuizController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.FTIsland.BE.controller;

import com.FTIsland.BE.dto.BookContentDTO;
import com.FTIsland.BE.book.content.dto.BookContentRequest;
import com.FTIsland.BE.book.content.dto.BookContentResponse;
import com.FTIsland.BE.book.content.service.BookContentService;
import com.FTIsland.BE.dto.ChatGptResponse;
import com.FTIsland.BE.dto.QuizDTO;
import com.FTIsland.BE.entity.QuizEntity;
import com.FTIsland.BE.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -40,40 +41,40 @@ public List<QuizDTO> getQuiz(@RequestBody QuizDTO quizDTO){
// 책 - 사용자 레벨 쌍의 조합으로 저장되어있는 생각해보기 질문이 있는지 확인
List<String> savedQuiz = quizService.findQuiz(quizDTO.getBookId(), userLevel);

if(savedQuiz.isEmpty()){ // 없으면 새로 생성 후 저장
if(savedQuiz.isEmpty()){ // A. 없으면 새로 생성 후 저장
// 책의 contents 조회 - 동화 제목만으로는 버전이 다를 수 있기 때문에
// 1. bookContentService의 findByBookId method를 쓰기 위해 QuizDTO -> BookContentDTO 변환
BookContentDTO bookContentDTO = new BookContentDTO();
bookContentDTO.setBookId(quizDTO.getBookId());
bookContentDTO.setMainLan(quizDTO.getMainLan());
bookContentDTO.setSubLan(quizDTO.getSubLan());
// 2. content List 조회
List<BookContentDTO> bookContentDTOS = bookContentService.findByBookId(bookContentDTO);
List<String> bookContentList = new ArrayList<>();
for(BookContentDTO dto : bookContentDTOS){
bookContentList.add(dto.getKorContents());
BookContentRequest bookContentRequest = new BookContentRequest().builder()
.bookId(quizDTO.getBookId())
.mainLan(quizDTO.getMainLan())
.subLan(quizDTO.getSubLan())
.build();

// 2. Content list 조회 및 한 줄로 합쳐진 Content 생성
List<BookContentResponse> bookContentResponses = bookContentService.findByBookId(bookContentRequest);
List<String> bookContentList = new ArrayList<>(); // 퀴즈의 기반이 될 내용 모음 list
for(BookContentResponse dto : bookContentResponses){
bookContentList.add(dto.getKorContents()); // 조회한 내용 한 줄씩 추가
}
String bookContents = String.join(" ", bookContentList);
// System.out.println(bookContents);
String bookContents = String.join(" ", bookContentList); // list를 한 줄의 string으로 합치기.

// 퀴즈 생성
// 3. AI로 퀴즈 생성
ChatGptResponse chatGptResponse = null;
chatGptResponse = chatGptService.askQuestion(bookTitle, userLevel, bookContents);
String threeQuiz = chatGptResponse.getChoices().get(0).getMessage().getContent();
// System.out.println(threeQuiz);

// 생성된 퀴즈 3개를 파싱하고 저장
// 4. 생성된 퀴즈 3개를 파싱하고 저장
List<String> quizs = quizService.makeQuiz(threeQuiz);
quizService.saveQuiz(quizs, quizDTO.getBookId(), userLevel);

// 해당 퀴즈 리스트를 주언어, 서브언어로 번역 후 반환
// 5. 해당 퀴즈 리스트를 주언어, 서브언어로 번역 후 반환
return quizService.translationQuiz(
quizs,
quizDTO.getMainLan(),
quizDTO.getSubLan()
);

} else { // 있으면 조회한 질문을 그대로 번역 후 반환
} else { // B. 있으면 조회한 질문을 그대로 번역 후 반환
return quizService.translationQuiz(
savedQuiz,
quizDTO.getMainLan(),
Expand Down
Loading

0 comments on commit d79049e

Please sign in to comment.