diff --git a/back/src/main/java/com/example/capstone/domain/help/repository/HelpRepositoryImpl.java b/back/src/main/java/com/example/capstone/domain/help/repository/HelpRepositoryImpl.java new file mode 100644 index 0000000000..07d22e0445 --- /dev/null +++ b/back/src/main/java/com/example/capstone/domain/help/repository/HelpRepositoryImpl.java @@ -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 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); + } +}