Skip to content

Commit

Permalink
♻️ refactor: 모든기록 조회 API 발생쿼리 개선, 중복데이터 조회문제 해결
Browse files Browse the repository at this point in the history
* ArgumentResolver에서 member검증을 진행하기에 서비스 레이어에선 검증쿼리 제거하기로 결정.
* 불필요한 join 삭제,커서 max값을 가져오는 쿼리를 동적쿼리로 변경해 쿼리횟수 1회 감소
  • Loading branch information
youngreal committed Nov 24, 2024
1 parent f3d290c commit 2062db4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import com.dnd.dndtravel.map.repository.dto.projection.RecordProjection;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -46,9 +47,11 @@ public List<RecordProjection> findAttractionRecords(long memberId, long cursorNo
attraction
))
.from(memberAttraction)
.join(member).on(memberAttraction.member.id.eq(memberId))
.join(attraction).on(memberAttraction.attraction.id.eq(attraction.id))
.where(memberAttraction.id.lt(cursorNo))
.where(
memberAttraction.member.id.eq(memberId),
ltCursorNo(cursorNo)
)
.orderBy(memberAttraction.id.desc())
.limit(displayPerPage)
.fetch();
Expand All @@ -61,4 +64,11 @@ public Long entireVisitCount(long memberId) {
.where(memberAttraction.member.id.eq(memberId))
.fetchOne();
}

private BooleanExpression ltCursorNo(Long cursorNo) {
if (cursorNo == null || cursorNo <= 0) {
return null; // 조건 없이 최신 데이터부터 조회
}
return memberAttraction.id.lt(cursorNo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public List<AttractionPhotoProjection> findByRecordDtos(List<RecordProjection> r
memberAttraction.id,
photo.url))
.from(photo)
.join(photo.memberAttraction, memberAttraction)
.where(photo.memberAttraction.id.in(memberAttractionIds))
.fetch();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@Getter
public class RecordProjection {
private final long memberAttractionId;
private String attractionName;
private final String memo;
private final LocalDate visitDate;
private final String region;
Expand All @@ -39,7 +38,7 @@ public void inputPhotoUrls(List<AttractionPhotoProjection> attractionPhotoProjec
}
}

public void inputAttractionNames() {
this.attractionName = this.attraction.getName();
public String getAttractionName() {
return this.attraction.getName();
}
}
18 changes: 6 additions & 12 deletions src/main/java/com/dnd/dndtravel/map/service/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,17 @@ public void recordAttraction(RecordDto recordDto, long memberId) {
// 모든 기록 조회
@Transactional(readOnly = true)
public AttractionRecordsResponse allRecords(long memberId, long cursorNo, int displayPerPage) {
//validation
Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberNotFoundException(memberId));
List<MemberAttraction> memberAttractions = memberAttractionRepository.findByMemberId(memberId);
if (memberAttractions.isEmpty()) {
//전체 방문 기록 수 조회
Long visitCount = memberAttractionRepository.entireVisitCount(memberId);

//방문기록이 없는경우
if (visitCount == 0) {
return new AttractionRecordsResponse(0, List.of());
}

// 첫 커서값인 경우
if (cursorNo <= 0) {
cursorNo = memberAttractionRepository.maxCursor(member.getId());
}
List<RecordProjection> attractionRecords = memberAttractionRepository.findAttractionRecords(memberId, cursorNo, displayPerPage);
Long visitCount = memberAttractionRepository.entireVisitCount(memberId);

// 명소명 저장
attractionRecords.forEach(RecordProjection::inputAttractionNames);
// 사진 URL 저장
//사진 URL 저장
setPhotoUrlsWithJoin(attractionRecords);

return new AttractionRecordsResponse(visitCount, attractionRecords.stream()
Expand Down

0 comments on commit 2062db4

Please sign in to comment.