Skip to content

Commit

Permalink
feat: #119-헬퍼 매칭글 리스트 필터 및 페이지네이션 Repository 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueBerrySoda committed May 2, 2024
1 parent 03e833f commit 1c83124
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.capstone.domain.help.repository;

import com.example.capstone.domain.help.dto.HelpListResponse;
import com.example.capstone.domain.help.dto.HelpSliceResponse;
import com.example.capstone.domain.help.entity.QHelp;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequiredArgsConstructor
public class HelpRepositoryImpl implements HelpListRepository {

private final JPAQueryFactory jpaQueryFactory;
private QHelp help = QHelp.help;

@Override
public HelpSliceResponse getHelpListByPaging(Long cursorId, Pageable page, Boolean isDone, Boolean isHelper, UUID isMine) {
List<HelpListResponse> helpList = jpaQueryFactory
.select(
Projections.constructor(
HelpListResponse.class,
help.id, help.title,
help.author, help.country,
help.createdDate, help.isDone, help.isHelper
)
)
.from(help)
.where(cursorId(cursorId),
doneEq(isDone),
helperEq(isHelper),
mineEq(isMine))
.orderBy(help.createdDate.desc())
.limit(page.getPageSize() + 1)
.fetch();

boolean hasNext = false;
if(helpList.size() > page.getPageSize()) {
helpList.remove(page.getPageSize());
hasNext = true;
}

Long lastCursorId = null;

if(hasNext && helpList.size() != 0) {
lastCursorId = helpList.get(helpList.size() - 1).id();
}

return new HelpSliceResponse(lastCursorId, hasNext, helpList);
}

private BooleanExpression cursorId(Long cursorId) {
return cursorId == null ? null : help.id.gt(cursorId);
}

private BooleanExpression doneEq(Boolean isDone) {
return isDone == null ? null : help.isDone.eq(isDone);
}

private BooleanExpression helperEq(Boolean isHelper) {
return isHelper == null ? null : help.isHelper.eq(isHelper);
}

private BooleanExpression mineEq(UUID isMine) {
return isMine == null ? null : help.uuid.eq(isMine);
}
}

0 comments on commit 1c83124

Please sign in to comment.