Skip to content

Commit

Permalink
feat: 용어집 내 용어 조회 시 용어 이름으로 정렬 적용 #36
Browse files Browse the repository at this point in the history
  • Loading branch information
seungyeop-lee committed May 11, 2024
1 parent 775046a commit 7400dff
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 19 deletions.
49 changes: 49 additions & 0 deletions api/src/main/java/vook/server/api/app/TermSearchRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package vook.server.api.app;

import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import vook.server.api.helper.QuerydslHelper;
import vook.server.api.model.QTerm;
import vook.server.api.model.Term;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class TermSearchRepository {

private final JPAQueryFactory queryFactory;

public List<Term> search(String glossaryUid, Pageable pageable) {
QTerm term = QTerm.term1;

// fetch join과 pagination을 같이 하면,
// HHH90003004: firstResult/maxResults specified with collection fetch; applying in memory
// 에러가 발생, 이를 피하기 위해 pagination이 포함된 쿼리를 서브 쿼리로 하여 수행
JPAQuery<Long> termIdQuery = queryFactory
.select(term.id)
.from(term)
.where(term.glossary.uid.eq(glossaryUid))
.offset(pageable.getOffset())
.limit(pageable.getPageSize());

QuerydslHelper
.toOrderSpecifiers(term, pageable)
.forEach(termIdQuery::orderBy);

JPAQuery<Term> dataQuery = queryFactory
.selectFrom(term)
.leftJoin(term.synonyms).fetchJoin()
.where(term.id.in(termIdQuery));

// In 절에 들어간 입력순대로 출력되지 않음으로 정렬 조건을 다시 한번 설정
QuerydslHelper
.toOrderSpecifiers(term, pageable)
.forEach(dataQuery::orderBy);

return dataQuery.fetch();
}
}
8 changes: 4 additions & 4 deletions api/src/main/java/vook/server/api/app/TermService.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package vook.server.api.app;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import vook.server.api.model.Glossary;
import vook.server.api.model.Term;
import vook.server.api.model.TermRepository;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TermService {

private final TermRepository repository;
private final TermSearchRepository searchRepository;

public List<Term> findAllBy(Glossary glossary) {
return repository.findAllByGlossary(glossary);
public List<Term> findAllBy(Glossary glossary, Pageable pageable) {
return searchRepository.search(glossary.getUid(), pageable);
}
}
3 changes: 0 additions & 3 deletions api/src/main/java/vook/server/api/model/TermRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface TermRepository extends JpaRepository<Term, Long> {
List<Term> findAllByGlossary(Glossary glossary);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
import vook.server.api.web.common.CommonApiResponse;

import java.util.List;
Expand Down Expand Up @@ -36,13 +38,16 @@ class RetrieveApiResponse extends CommonApiResponse<List<RetrieveResponse>> {
description = "성공",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = FindAllTermsApiResponse.class)
schema = @Schema(implementation = RetrieveTermsApiResponse.class)
)
),
})
CommonApiResponse<List<FindAllTermsResponse>> findAllTerms(String glossaryUid);
CommonApiResponse<List<RetrieveTermsResponse>> retrieveTerms(
String glossaryUid,
@ParameterObject Pageable pageable
);

class FindAllTermsApiResponse extends CommonApiResponse<List<FindAllTermsResponse>> {
class RetrieveTermsApiResponse extends CommonApiResponse<List<RetrieveTermsResponse>> {
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package vook.server.api.web.routes.glossary;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -25,8 +27,11 @@ public CommonApiResponse<List<RetrieveResponse>> retrieve() {

@Override
@GetMapping("/{glossaryUid}/terms")
public CommonApiResponse<List<FindAllTermsResponse>> findAllTerms(@PathVariable String glossaryUid) {
List<FindAllTermsResponse> result = service.findAllTerms(glossaryUid);
public CommonApiResponse<List<RetrieveTermsResponse>> retrieveTerms(
@PathVariable String glossaryUid,
@PageableDefault(size = Integer.MAX_VALUE, sort = "term") Pageable pageable
) {
List<RetrieveTermsResponse> result = service.retrieveTerms(glossaryUid, pageable);
return CommonApiResponse.okWithResult(result);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vook.server.api.web.routes.glossary;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vook.server.api.app.GlossaryService;
Expand All @@ -23,9 +24,9 @@ public List<RetrieveResponse> retrieve() {
return RetrieveResponse.from(glossaries);
}

public List<FindAllTermsResponse> findAllTerms(String glossaryUid) {
public List<RetrieveTermsResponse> retrieveTerms(String glossaryUid, Pageable pageable) {
Glossary glossary = glossaryService.findByUid(glossaryUid).orElseThrow();
List<Term> terms = termService.findAllBy(glossary);
return FindAllTermsResponse.from(glossary, terms);
List<Term> terms = termService.findAllBy(glossary, pageable);
return RetrieveTermsResponse.from(glossary, terms);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;

@Getter
public class FindAllTermsResponse {
public class RetrieveTermsResponse {

private String term;

Expand All @@ -23,14 +23,14 @@ public class FindAllTermsResponse {

private String createdBy;

public static List<FindAllTermsResponse> from(Glossary glossary, List<Term> terms) {
public static List<RetrieveTermsResponse> from(Glossary glossary, List<Term> terms) {
return terms.stream()
.map(term -> from(glossary, term))
.toList();
}

public static FindAllTermsResponse from(Glossary glossary, Term term) {
FindAllTermsResponse response = new FindAllTermsResponse();
public static RetrieveTermsResponse from(Glossary glossary, Term term) {
RetrieveTermsResponse response = new RetrieveTermsResponse();
response.term = term.getTerm();
response.synonyms = term.getSynonyms().stream().map(TermSynonym::getSynonym).toList();
response.meaning = term.getMeaning();
Expand Down

0 comments on commit 7400dff

Please sign in to comment.