Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<DictionaryTermListResDto> dicTermListResDto = termService.getTerms(userId);
Pageable pageRequest = PageRequest.of(page, size);

Page<DictionaryTermListResDto> dicTermListResDto = termService.getTerms(userId, pageRequest);

return ApiResponse.onSuccess(dicTermListResDto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

public interface UserTermsRepository extends JpaRepository<UserTerms, Long> {

@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<UserTerms> findByUserId(Long userId, Pageable pageable);

List<UserTerms> findByUserId(Long userId);

// 중복 체크용
Expand All @@ -36,10 +39,12 @@ List<UserTerms> 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<UserTerms> searchByTermContaining(
@Param("userId") Long userId,
@Param("keyword") String keyword,
Expand All @@ -65,7 +70,7 @@ List<UserTerms> 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<String> findTermSuggestions(
@Param("userId") Long userId,
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/com/example/whiplash/term/service/TermService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<DictionaryTermListResDto> getTerms(Long userId) {
public Page<DictionaryTermListResDto> getTerms(Long userId, Pageable pageable) {

List<UserTerms> dicTermList = userTermsRepository.findByUserId(userId);
Page<UserTerms> 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());
}

/**
Expand All @@ -93,11 +92,13 @@ public List<DictionaryTermListResDto> getTerms(Long userId) {
public TermExplainResDto getTermExplanation(String term) {
Optional<Terms> findTerm = termsRepository.findByTermName(term);

// 기존에 누군가가 AI설명을 요청한 이력이 있다면 -> 바로 응답
if (findTerm.isPresent()) {
Terms terms = findTerm.get();
return new TermExplainResDto(terms.getTermName(), terms.getAiExplanation());
}

// 없으면 서버 요청!
return aiServerClient.getTermExplain(term);
}

Expand Down