diff --git a/src/main/java/com/example/whiplash/term/controller/TermsController.java b/src/main/java/com/example/whiplash/term/controller/TermsController.java index 7770b0b..6851da2 100644 --- a/src/main/java/com/example/whiplash/term/controller/TermsController.java +++ b/src/main/java/com/example/whiplash/term/controller/TermsController.java @@ -29,12 +29,6 @@ public class TermsController { private final TermService termService; - @PostMapping("/explain") - @Operation(summary = "용어 AI 설명요청", description = "용어에 대한 AI 설명을 요청한다.") - public ApiResponse getTermsExplain() { - return null; - } - @PostMapping("") @Operation(summary = "용어 저장", description = "용어를 나의 용어사전에 저장합니다.") public ApiResponse addTerms(@RequestBody TermAddDto termAddDto, @AuthenticationPrincipal UserPrincipal principal) { @@ -48,11 +42,14 @@ public ApiResponse addTerms(@RequestBody TermAddDto termAddDto, @Authenticati @GetMapping("") @Operation(summary = "용어 리스트 조회", description = "나의 용어사전에 저장된 용어 목록을 조회합니다.") - public ApiResponse getTerms(@AuthenticationPrincipal UserPrincipal principal) { + public ApiResponse getTerms(@AuthenticationPrincipal UserPrincipal principal, + @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "20") int size) { Long userId = principal.getUserId(); - List dicTermListResDto = termService.getTerms(userId); + Pageable pageRequest = PageRequest.of(page, size); + + Page dicTermListResDto = termService.getTerms(userId, pageRequest); return ApiResponse.onSuccess(dicTermListResDto); } diff --git a/src/main/java/com/example/whiplash/term/entity/UserTerms.java b/src/main/java/com/example/whiplash/term/entity/UserTerms.java index 61d356a..397da8a 100644 --- a/src/main/java/com/example/whiplash/term/entity/UserTerms.java +++ b/src/main/java/com/example/whiplash/term/entity/UserTerms.java @@ -14,7 +14,9 @@ uniqueConstraints = @UniqueConstraint( name = "uk_user_terms", columnNames = {"user_id", "terms_id"} - )) + ), + indexes = @Index(name = "idx_user_terms_user_id", columnList = "user_id") +) public class UserTerms extends BaseEntity { @Id diff --git a/src/main/java/com/example/whiplash/term/repository/UserTermsRepository.java b/src/main/java/com/example/whiplash/term/repository/UserTermsRepository.java index acf3c68..14b045f 100644 --- a/src/main/java/com/example/whiplash/term/repository/UserTermsRepository.java +++ b/src/main/java/com/example/whiplash/term/repository/UserTermsRepository.java @@ -14,7 +14,10 @@ public interface UserTermsRepository extends JpaRepository { - @Query("select ut from UserTerms ut join fetch ut.terms t where ut.user.id = :userId") + @Query(value = "select ut from UserTerms ut join fetch ut.terms t where ut.user.id = :userId", + countQuery = "select count(ut) from UserTerms ut where ut.user.id = :userId") + Page findByUserId(Long userId, Pageable pageable); + List findByUserId(Long userId); // 중복 체크용 @@ -36,10 +39,12 @@ List findByUserIdAndCreatedAtBetween( * ⭐ 용어명 부분 검색 (LIKE '%keyword%') * 예: "금" 검색 → "금리", "금융", "환금성" */ - @Query("SELECT ut FROM UserTerms ut " + + @Query(value = "SELECT ut.* FROM UserTerms ut " + + "INNER JOIN terms t ON ut.terms.id = t.id" + "WHERE ut.user.id = :userId " + - "AND LOWER(ut.terms.termName) LIKE LOWER(CONCAT('%', :keyword, '%')) " + - "ORDER BY ut.createdAt DESC") + "AND MATCH(t.term_name) AGANIST (:keyword IN BOOLEAN MODE) " + + "ORDER BY ut.createdAt DESC", + nativeQuery = true) Page searchByTermContaining( @Param("userId") Long userId, @Param("keyword") String keyword, @@ -65,7 +70,7 @@ List searchByTermStartsWith( */ @Query("SELECT DISTINCT ut.terms.termName FROM UserTerms ut " + "WHERE ut.user.id = :userId " + - "AND LOWER(ut.terms.termName) LIKE LOWER(CONCAT(:keyword, '%')) " + + "AND LOWER(ut.terms.termName) LIKE CONCAT(:keyword, '%') " + "ORDER BY ut.terms.termName ASC") List findTermSuggestions( @Param("userId") Long userId, diff --git a/src/main/java/com/example/whiplash/term/service/TermService.java b/src/main/java/com/example/whiplash/term/service/TermService.java index bf1dd58..400d311 100644 --- a/src/main/java/com/example/whiplash/term/service/TermService.java +++ b/src/main/java/com/example/whiplash/term/service/TermService.java @@ -68,23 +68,22 @@ public void addTermToDictionary(TermAddDto termAddDto, Long userId) { userTermsRepository.save(userTerms); // 비동기 퀴즈 생성 요청 - quizPreGenerationService.generateQuizAsync(userId, dicTerm.getTermName()); +// quizPreGenerationService.generateQuizAsync(userId, dicTerm.getTermName()); } /** * 용어 목록 조회 */ - public List getTerms(Long userId) { + public Page getTerms(Long userId, Pageable pageable) { - List dicTermList = userTermsRepository.findByUserId(userId); + Page dicTermPage = userTermsRepository.findByUserId(userId, pageable); - return dicTermList.stream() - .map(dicTerm -> DictionaryTermListResDto.builder() - .userTermId(dicTerm.getId()) - .termDescription(dicTerm.getTerms().getAiExplanation()) - .termName(dicTerm.getTerms().getTermName()) - .createdAt(dicTerm.getTerms().getCreatedAt()) - .build()).toList(); + return dicTermPage.map(dicTerm -> DictionaryTermListResDto.builder() + .userTermId(dicTerm.getId()) + .termDescription(dicTerm.getTerms().getAiExplanation()) + .termName(dicTerm.getTerms().getTermName()) + .createdAt(dicTerm.getTerms().getCreatedAt()) + .build()); } /** @@ -93,11 +92,13 @@ public List getTerms(Long userId) { public TermExplainResDto getTermExplanation(String term) { Optional findTerm = termsRepository.findByTermName(term); + // 기존에 누군가가 AI설명을 요청한 이력이 있다면 -> 바로 응답 if (findTerm.isPresent()) { Terms terms = findTerm.get(); return new TermExplainResDto(terms.getTermName(), terms.getAiExplanation()); } + // 없으면 서버 요청! return aiServerClient.getTermExplain(term); }