diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupController.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupController.java index 5799b18..9c642df 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupController.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/challengegroup/ChallengeGroupController.java @@ -87,19 +87,13 @@ public ApiResponse getCha @Operation(summary = "챌린지 그룹 숏폼 페이징", description = "챌린지 숏폼 페이징 조회한다.") @GetMapping("/api/challengeGroups/shorts") public ApiResponse> getChallengeShortsPaging( - @RequestParam Long page + @Valid PagingRequest pagingRequest, + @AuthenticationPrincipal JwtUser jwtUser ) { - return ApiResponse.success( - PagingResponse.builder() - .hasNext(false) - .totalPage(1) - .data(List.of( - new ChallengeGroupRes.ChallengeGroupDto(1L, - "title", "thumbnailUrl", 12, - LocalDate.now(), LocalDate.now(), ChallengeCategory.VOLUNTEER) - )) - .build() - ); + var page = challengeGroupQueryService.getChallengeGroupsShortsPaging(pagingRequest.toPageable(), jwtUser.getId()); + var response = PagingResponse.from(page, ChallengeGroupRes.ChallengeGroupDto::from); + + return ApiResponse.success(response); } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java index 13a9d87..53359f4 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java @@ -26,4 +26,10 @@ public ChallengeGroupModel.Detail getChallengeGroupDetail(Long challengeGroupId) List challengeGroupImages = challengeGroupImageReader.getByChallengeGroupId(challengeGroupId); return ChallengeGroupModel.Detail.from(challengeGroup, challengeGroupImages); } + + @Transactional(readOnly = true) + public Page getChallengeGroupsShortsPaging(Pageable pageable, Long userId) { + Page challengeGroups = challengeGroupReader.getChallengeGroupsShortsPaging(pageable, userId); + return challengeGroups.map(ChallengeGroupModel.Info::from); + } } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java index cf36a1b..562a56e 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java @@ -9,4 +9,6 @@ public interface ChallengeGroupReader { ChallengeGroup getByIdWithChallenges(Long challengeGroupId); Page getChallengeGroupsPagingByCategory(Pageable pageable, ChallengeCategory category); + + Page getChallengeGroupsShortsPaging(Pageable pageable, Long userId); } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java index 5c9bbea..2bb67c8 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java @@ -1,5 +1,9 @@ package org.haedal.zzansuni.infrastructure.challengegroup; +import com.querydsl.core.types.OrderSpecifier; +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.core.types.dsl.Expressions; +import com.querydsl.core.types.dsl.NumberTemplate; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; import org.haedal.zzansuni.domain.challengegroup.ChallengeCategory; @@ -51,4 +55,29 @@ public Page getChallengeGroupsPagingByCategory(Pageable pageable return new PageImpl<>(page, pageable, count == null ? 0 : count); } + + @Override + public Page getChallengeGroupsShortsPaging(Pageable pageable, Long userId) { + Double seed = Double.valueOf(userId); + + // 해시 값을 기준으로 정렬 + NumberTemplate hashOrder = Expressions + .numberTemplate(Double.class, "SHA1(CONCAT({0}, id))", seed); + + + Long count = queryFactory + .select(QChallengeGroup.challengeGroup.count()) + .from(QChallengeGroup.challengeGroup) + .fetchOne(); + List page = queryFactory + .selectFrom(QChallengeGroup.challengeGroup) + .leftJoin(QChallengeGroup.challengeGroup.challenges).fetchJoin() + .orderBy(Expressions.numberTemplate(Double.class, "RAND({0})", userId).asc()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .fetch(); + + return new PageImpl<>(page, pageable, count == null ? 0 : count); + } + }